Wildcard in bash script

最后都变了- 提交于 2021-02-05 09:28:27

问题


I have a bash script to retrieve files from ftp.

Now the files have one part a date string in the filename, but also undefined numbers that changes on every file. I want to download the files based on the date.

This is my code. I only need to do the wildcard trick, the ftp script is allready work.

filename=$(echo $TIMESTAMP'0***vel.radar.h5')

The stars are 3 digits with different numbers that i can't estimate, so i would use the wildcard for them.

Thank you


回答1:


It sounds like you want to handle multiple files, but your script can only handle one file at a time. Furthermore, because you specified FTP, it sounds like the files are on the FTP server, in which case local filename expansion will not help.

You probably want to use the ftp client's mget command to download multiple files matching a pattern on the remote side. You also want to include $TIMESTAMP as part of the pattern. I'd suggest something like this:

ftp remote-hostname <<EOF
cd path/to/log/files
prompt
mget ${TIMESTAMP}0???vel.radar.h5
bye
EOF

This uses a here-document (<<EOF to EOF on a line by itself) to supply input text to the ftp commmand. It will expand the variable $TIMESTAMP so it becomes part of the mget command, e.g. if $TIMESTAMP was 12345, the ftp command will be told mget 123450???vel.radar.h5.




回答2:


With the ? characters you include not only numbers, but any character. So if you want to include only numbers, you can replace the ??? characters with [0-9][0-9][0-9].

Example:

If you have the following files:

$ ls 0123vel.h5 0333vel.h5 033vel.h5 0pecvel.h5

with this ls you show the correct files:

$ ls 0[0-9][0-9][0-9]vel.radar.h5 0123vel.h5 0333vel.h5



来源:https://stackoverflow.com/questions/23047356/wildcard-in-bash-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!