问题描述
感谢您抽出宝贵的时间来帮助您!
我正在尝试以变量作为参数运行命令1次。事实是,变量有多行,我需要为每行作为参数运行1次命令。
示例脚本
VAR1=command1
"${VAR1}" | xargs -L1 -d "\n command2$i
VAR1的示例输出是
1111
2222
3333
4444
5555
我需要每行运行一次command2,所以
command2 1111
command2 2222
command2 3333
command2 4444
command2 5555
我也尝试过这个,
VAR1=command1
"${VAR1}" | while read line ; do command2$i
还有这个
VAR1=command1
"${VAR1}" | while read line ; do command2"${VAR1}"
感谢您的光临!
解决方法
对于xargs,您可以使用“此处字符串”:
xargs -L1 command2 <<< "$var1"
或者简单地
printf '%s\n' "$var1" | xargs -L1 command2
类似地,对于循环,但是您需要真正使用循环变量:
while read -r line ; do
command2 "$line"
done <<< "$var1"
或者再次
printf '%s\n' "$var1" | while read -r line ; do
command2 "$line"
done