Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Background:

I have a script which takes a filename as a parameter, for example "mydb.sql". My script imports this DB and "cleans" it (removes user identifiable data, etc), exports it and then creates a "lite" version (strips all non-essential data out, leaving only main admin user, removing all votes, etc). These get generated into mydb.clean.sql and mydb.lite.sql. This script gets run on several folders.

I want to create a wildcard pattern to pass into find. Originally I just hard coded '*.sql' which returned the original, clean and lite (these were then passed into du to give me a file size summary).

Now I have altered my script to take a parameter; the filename. This allows me to run it on a single file.

The Question

So how do I get the pattern? I have this:

FNAME=$(basename "$1");
FNAME="${FNAME%.*}*.sql"
echo $FNAME

This converts FNAME from /home/user/backups/sitea/mydb.sql to mydb. I then want FNAME to be the string 'mydb*.sql', however bash is expanding that * out so I end up with FNAME as 'mydb.sql mydb.clean.sql mydb.lite.sql' (assuming it found them in the CWD).

I have tries backslashing the *, however I then end up with 'mydb\*.sql' which doesn't work. I tried using single quotes, but then bash doesn't expand the ${FNAME%.*} part.

Any thoughts?

Update

This better illustrates the issue...

[user@server temp]$ T="${FNAME}\*.sql"; echo $T
mydb\*.sql

[user@server temp]$ T="${FNAME}*.sql"; echo $T
mydb.clean.sql mydb.lite.sql mydb.sql

[user@server temp]$ T='${FNAME}*.sql'; echo $T
${FNAME}*.sql
share|improve this question

1 Answer 1

up vote 3 down vote accepted

It's being expanded at the last step.

echo "$FNAME"
share|improve this answer
    
Looks like you're right... T="${FNAME}*.sql"; echo "$T outputs: mydb*.sql –  Nick Jul 16 '12 at 13:27
    
Of course - it makes sense now. Unquoted variables get expanded. I simply had to add double quotes to the echo and the find calls and it worked perfectly. Thanks @Ignacio –  Nick Jul 16 '12 at 14:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.