从输入文件的单列执行命令
Execute commands from a single column of an input file
我对 bash 的经验很少,但我正在尝试构建一个从输入文件逐行读入和执行命令的管道。名为 "seeds.txt" 的输入文件是这样设置的
program_to_execute -seed_value 4496341759106 -a_bunch_of_arguments c(1,2,3) ; #Short_description_of_run
program_to_execute -seed_value 7502828106749 -a_bunch_of_arguments c(4,5,6) ; #Short_description_of_run
我用分号 (;) 将 #Short_descriptions 与命令分开,因为参数包含逗号 (,)。当我 运行 以下脚本时,我得到一个 "No such file or directory" 错误
#!/bin/bash
in="${1:-seeds.txt}"
in2=$(cut -d';' -f1 "${in}")
while IFS= read -r SAMP
do
$SAMP
done < $in2
我知道 seeds.txt 读得很好,所以我不确定为什么我会收到 file/directory 消息。谁能指出我正确的方向?
您可以尝试使用 eval
进行以下操作...虽然不是很安全,请参阅 this 了解更多信息
while read line; do eval "$line" ; done < <(cut -d';' -f1 seeds.txt)
使用 GNU Parallel 看起来像这样:
cut -d';' -f1 seeds.txt | parallel
以防万一你想避免eval
while read -ra line; do command "${line[@]}"; done < <(cut -d';' -f1 seeds.txt)
请注意,如果 program/utility 不是您的 PATH 中的可执行文件,则此解决方案不起作用,例如你想使用函数或别名。不确定 eval 解决方案是否也可以做到这一点。感谢 cut
解决方案!
我对 bash 的经验很少,但我正在尝试构建一个从输入文件逐行读入和执行命令的管道。名为 "seeds.txt" 的输入文件是这样设置的
program_to_execute -seed_value 4496341759106 -a_bunch_of_arguments c(1,2,3) ; #Short_description_of_run
program_to_execute -seed_value 7502828106749 -a_bunch_of_arguments c(4,5,6) ; #Short_description_of_run
我用分号 (;) 将 #Short_descriptions 与命令分开,因为参数包含逗号 (,)。当我 运行 以下脚本时,我得到一个 "No such file or directory" 错误
#!/bin/bash
in="${1:-seeds.txt}"
in2=$(cut -d';' -f1 "${in}")
while IFS= read -r SAMP
do
$SAMP
done < $in2
我知道 seeds.txt 读得很好,所以我不确定为什么我会收到 file/directory 消息。谁能指出我正确的方向?
您可以尝试使用 eval
进行以下操作...虽然不是很安全,请参阅 this 了解更多信息
while read line; do eval "$line" ; done < <(cut -d';' -f1 seeds.txt)
使用 GNU Parallel 看起来像这样:
cut -d';' -f1 seeds.txt | parallel
以防万一你想避免eval
while read -ra line; do command "${line[@]}"; done < <(cut -d';' -f1 seeds.txt)
请注意,如果 program/utility 不是您的 PATH 中的可执行文件,则此解决方案不起作用,例如你想使用函数或别名。不确定 eval 解决方案是否也可以做到这一点。感谢 cut
解决方案!