我需要运行这个命令
trjconv -s run.tpr -f run.xtc -pbc mol -ur compact -o unwrap
对于所有遵循命名模式的子目录us_0.0, us_-0.2, us_-0.4 ... us_-3.8
。运行该命令后,它还会要求我进行选择,答案也将为 0。我究竟应该如何在 bash 中编写此脚本?
答案1
如果你的意思是你需要将每个目录传递到 stdin:
convAll ()
{
while read line
do
echo -e "$line\n0" | trjconv -s run.tpr -f run.xtc -pbc mol -ur compact -o unwrap
done
}
ls | grep -E 'us_-?[0-9]\.[0-9]+' | convAll
# NOTE: There's probably an even shorter one-liner version of this that uses `xargs`, but
# I'll leave that as an exercise to the reader.
如果你的意思是你需要将每个目录作为附加参数传递:
ls | grep -E 'us_-?[0-9]\.[0-9]+' | tr '\n' '\0' | \
xargs -0 -n 1 trjconv -s run.tpr -f run.xtc -pbc mol -ur compact -o unwrap
如果你的意思是你需要cd
进入每个目录:
convAll ()
{
while read line
do
(\
cd "$line" && \
trjconv -s run.tpr -f run.xtc -pbc mol -ur compact -o unwrap || \
echo "Command failed for '$line'." >&2 \
)
done
}
ls | grep -E 'us_-?[0-9]\.[0-9]+' | convAll