文件名包含表单00035023030的模式,该模式仅在最后两位数中从...30更改为...35。但是,如果从30到35 (比如31 )遗漏了一个数字,它就会抛出错误。如何绕过此错误并在循环中运行以下命令列表?
在这里,barycorr是一个程序,它要求我输入三个内容:
/home/dinesh/Test/output00035023030/sw00035023030xwtw2po_cl.evt/home/dinesh/Test/output00035023030/sw00035023030xwtw2po_cl_bary.evt/home/dinesh/Test/00035023032/auxil/sw00035023032sao.fits.gz我为每个文件创建的要运行的脚本文件:
echo -e "/home/dinesh/Test/output00035023030/sw00035023030xwtw2po_cl.evt
/home/dinesh/Test/output00035023030/sw00035023030xwtw2po_cl_bary.evt
/home/dinesh/Test/00035023030/auxil/sw00035023030sao.fits.gz" | barycorr ra=253.467570 dec=39.760169 &>log
echo -e "/home/dinesh/Test/output00035023031/sw00035023031xwtw2po_cl.evt
/home/dinesh/Test/output00035023031/sw00035023031xwtw2po_cl_bary.evt
/home/dinesh/Test/00035023031/auxil/sw00035023031sao.fits.gz" | barycorr ra=253.467570 dec=39.760169 &>log
echo -e "/home/dinesh/Test/output00035023032/sw00035023032xwtw2po_cl.evt
/home/dinesh/Test/output00035023032/sw00035023032xwtw2po_cl_bary.evt
/home/dinesh/Test/00035023032/auxil/sw00035023032sao.fits.gz" | barycorr ra=253.467570 dec=39.760169 &>log
echo -e "/home/dinesh/Test/output00035023033/sw00035023033xwtw2po_cl.evt
/home/dinesh/Test/output00035023032/sw00035023033xwtw2po_cl_bary.evt
/home/dinesh/Test/00035023032/auxil/sw00035023032sao.fits.gz" | barycorr ra=253.467570 dec=39.760169 &>log发布于 2021-06-18 11:38:37
您的脚本将不会停止并运行所有命令。你所要做的就是忽略这个错误。但是,您确实可以使它跳过丢失的文件。例如,您可以像这样重写脚本(假设bash第4版或更高版本,用于支撑展开中的零填充):
#!/bin/bash
for num in {00035023030..00035023033}; do
dir=/home/dinesh/Test/output"${num}"
file1="$dir/sw${num}xwtw2po_cl.evt"
file2="$dir/sw${num}xwtw2po_cl_bary.evt"
file3="/home/dinesh/Test/$num/auxil/sw${num}sao.fits.gz"
if [[ -e "$file1" && -e "$file2" && -e "$file3" ]]; then
printf '%s %s %s' "$file1" "$file2" "$file3" |
barycorr ra=253.467570 dec=39.760169 &>>log
else
echo "Some files missing for $num" >> log
fi
donefor num in {00035023030..00035023033}; do:{start..end}表示法称为“大括号扩展”,将扩展到从start到end的所有数字:$ echo { 00035023030 .. 00035023033 } 00035023030 00035023031 00035023032 00035023033for variable in something是一个for循环,它将变量(在本例中为$num)的值设置为每个"somethings“。这意味着这个循环将对00035023030到0035023033的数字进行迭代。
${num}”,以便shell能够理解sw${num}xwtw2po_cl_bary.evt这样的东西,因为在sw$numxwtw2po_cl_bary.evt中,shell将无法知道变量的名称是$num而不是$numxwtw2po_cl_bary.evt。if [[ -e "$file1" && -e "$file2" && -e "$file3" ]]; then:这个if只检查所有三个文件是否都存在。-e正在检查文件是否存在。因此,只有当所有三个文件都存在时,if才会成功。https://unix.stackexchange.com/questions/654775
复制相似问题