如果文件名不匹配,则bash命令跳过执行

时间:2021-02-27 20:27:10

I use a bash script to execute a command in shell for several times:

我使用bash脚本在shell中执行多次命令:

for i in $(seq 0 202); do
    xqcdio.convert.to.general /global/homes/s/sufian/job-run/output/c2.avg.00$(($i*40+495)).data.save binarydata/c2.avg.00$(($i*40+495)).bin < input$(($i*40+495)).txt
done

The problem is if some files are missing, such as c2.avg.008305.data.save and several others then the command is immediately terminated.

问题是如果缺少某些文件,例如c2.avg.008305.data.save和其他几个文件,则命令会立即终止。

Is there any way if I can skip executing the command if a file is not found and move to the next file?

如果找不到文件并且移动到下一个文件,我是否可以跳过执行命令?

2 个解决方案

#1


Use continue:

[[ ! -f missing_file_path ]] && continue;
#put this immediately after do

to skip execution and move to the next iteration.

跳过执行并转到下一次迭代。

#2


Use test to see if the file exists first.

使用test查看文件是否存在。

for i in ... ; do [ -f "somefile$i" ] && ... ; done

#1


Use continue:

[[ ! -f missing_file_path ]] && continue;
#put this immediately after do

to skip execution and move to the next iteration.

跳过执行并转到下一次迭代。

#2


Use test to see if the file exists first.

使用test查看文件是否存在。

for i in ... ; do [ -f "somefile$i" ] && ... ; done