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");
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?


This better illustrates the issue...

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

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

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

It's being expanded at the last step.

echo "$FNAME"
  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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