问题
I am writing a bash script
My files are like:
file="${nodeID}_00000_19700101010${ts}_udp_filtered.pcap"
. Is it possible to instead of 00000
use any 5digit number? I thought about using
file="${nodeID}_
*_19700101010${ts}_udp_filtered.pcap"
sometimes I have 00001, sometimes 00004, etc.
回答1:
Something like
echo "${nodeID}"_[0-9][0-9][0-9][0-9][0-9]_19700101010"${ts}"_udp_filtered.pcap
Note, *
and [something]
won't expand in quotes, so only the variables are quoted above.
回答2:
for match in ${nodeID}_*_19700101010${ts}_udp_filtered.pcap
do
#echo $match
#do something with $match
#file=$match
done
This will find all the matches in the directory and allow you to do something with them. The asterisk will match any string, not just 5 digits. You can be more specific in the regex, but if you know the format of the filenames, you can determine whether the * is safe or needs to be more specific.
来源:https://stackoverflow.com/questions/24352431/how-to-use-wildcard-in-the-middle-on-the-string-bash