Bash: Pattern matchin - how to do matching on a variable?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
always produces one 'record' per file per line of output, and anything(!) after the date/timestamp in each rec is the filename, with or without spaces in it.
So the point is just that multiple filenames don't show up in rows? Well, I suppose as someone else noted above, ls -1 (not -l) would avoid that.
Last edited by jhwilliams; 12-08-2009 at 07:32 PM.
In the case you have given, this would be sufficient:
Code:
filename=foo*
If there is no filename matching the glob foo*, then $filename will contain the literal string 'foo*'. So you might want to check to see that the glob has actually matched a file:
Code:
[ -f "$filename" ] || echo "$filename" is not a file.
If you are expecting multiple matches on a glob (as is probably more reasonable) you might combine the above approach with the Bash array capability.
Code:
# Get an array, with one filename per entry.
filenames=(foo*)
# Either you have 0 filenames, or 1..n filenames.
# Check the first entry of the array to see if it is a filename.
# If not, error out.
[ -f "${filenames[0]}" ] || echo "no matches" && exit 1
# Do whatever it is you have to do -- perhaps with a for loop.
for item in "${filenames[@]}"; do
echo "$item";
done
Last edited by jhwilliams; 12-08-2009 at 08:44 PM.
If there are no files matching foo* then the shell will expand foo* to exactly that, foo*, unless shopt nullglob is set. It is unset (displayed as "off" by the shopt command) by default on Slackware 13.0.
Well, the issue of having an \n embedded in a filename is not really a legitimate concern. Anyone who were to do this would surely be fired, and their files would be renamed by their replacement.
Wow! [irony]Nice organsation.[/irony] Typically files with "pathological" characters in their names are not created by people but by software bugs or file system corruption. There are quite a few threads on LQ posted by people needing to delete or rename such files -- and they usually have no idea how those file names were created.
If there are no files matching foo* then the shell will expand foo* to exactly that, foo*, unless shopt nullglob is set. It is unset (displayed as "off" by the shopt command) by default on Slackware 13.0.
yes that's right. nullglob should be set if that's the case (or specifically use -f to test for file existence). However that's not the point, since OP is using ls with wildcard expansion produces error as well.
Code:
for file in foo*
do
if [ -f "$file" ];then
echo "found..continue processing here"
fi
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.