pog*_*bas 0 bash shell-script echo
我有几个列表,想对它们运行一些命令。由于列表很长,我想并行运行这些命令,因此使用nohup.
对于我尝试echo包含 nohup 命令的循环的每个项目,但它不起作用 -cat another_list_of_names读取到 stdout,但不读取到./tools. First catinfor a in $(cat list_of_names)将列表发送到循环,但echo'edfor b in $(cat another_list_of_names)将其发送到 stdout。
如何设置并行运行的 nohup 命令(是否可以nohup使用echo)?
for a in $(cat list_of_names)
do
ID=`echo $a`
mkdir ${ID}
echo "
nohup sh -c '
for b in $(cat another_list_of_names)
do
./tools $b $a >> ${ID}/output
done' &
"
done
Run Code Online (Sandbox Code Playgroud)
小智 5
我对您的代码进行了一些改进:
# This sort of loop is generally preferable to the one you had.
# This will handle spaces correctly.
while read a
do
# There's no need for the extra 'echo'
ID="$a"
# Quote variables that may contain spaces
mkdir "$ID"
# This is a matter of taste, but I generally find heredocs to be more
# readable than long echo commands
cat <<EOF
nohup sh -c '
while read b
do
# Quotation marks
./tools \$b $a >> "${ID}/output"
done < another_list_of_names' &
EOF
done < list_of_names
Run Code Online (Sandbox Code Playgroud)