Expand `*` inside of variable value in bash -


can create bash variable * in value , have * expanded shell upon use?

e.g.

sourcefiles=/path/*.phtml* filesfound=0  f in $sourcefiles;   echo "found file named: $f";   mv $f /other/path/"$f"   (($filesfound++)) done 

this running part of cron job, , getting error in email saying:

mv: cannot stat `/path/*.phtml*': no such file or directory 

so seems me * not expanding, perhaps when doesn't find matches...

correct: not expanding default behavior when there no matches!

this lets

ls *.txt 

return error akin to

ls: no file '*.txt' found 

instead of falling default behavior of listing files (as if given no arguments).


if want evaluate empty list, use:

shopt -s nullglob 

...or check if results exist:

for f in $sourcefiles;   [[ -e $f ]] || continue   echo "found file named: $f";   mv "$f" /other/path/"$f"   ((++filesfound)) done 

alternately, consider:

shopt -s nullglob sourcefiles=( /path/*.phtml* ) filesfound=${#sourcefiles[@]}  # print entire list, names quoted make hidden characters &c readable printf 'found file named: %q\n' "${sourcefiles[@]}"  # warning: works if list short enough fit on 1 command line mv -- "${sourcefiles[@]}" /other/path/ 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -