我正在尝试编写一个在FreeBSD上执行2个脚本的for循环.我不在乎它是用sh还是csh写的.我想要的东西:
for($i=11; $i<=24; $i++)
{
exec(tar xzf 'myfile-1.0.' . $i);
// detect an error was returned by the script
if ('./patch.sh')
{
echo "Patching to $i failed\n";
}
}
Run Code Online (Sandbox Code Playgroud)
有人知道怎么做吗?
谢谢
在sh中执行此操作的典型方法是:
for i in $(seq 11 24); do
tar xzf "myfile-1.0$i" || exit 1
done
Run Code Online (Sandbox Code Playgroud)
请注意,这seq不是标准.根据工具的可用性,您可以尝试:
jot 14 11 24
Run Code Online (Sandbox Code Playgroud)
要么
perl -E 'say for(11..24)'
Run Code Online (Sandbox Code Playgroud)
要么
yes '' | nl -ba | sed -n -e 11,24p -e 24q
Run Code Online (Sandbox Code Playgroud)
我做了一些更改:如果tar失败并且不发出错误消息,我会中止,因为tar应该发出错误消息而不是脚本.
哇!没有BASH.可能没有Kornshell:
i=11
while [ $i -le 24 ]
do
tar xzf myfile-1.0.$i
i=`expr $i + 1`
if ./patch.sh
then
echo "patching to $i failed"
fi
done
Run Code Online (Sandbox Code Playgroud)
写在纯粹的Bourne shell中就像上帝所预期的那样.
请注意,您必须使用该expr命令添加1 $i.Bourne shell不做数学.反引号意味着执行命令并将STDOUT从命令中输入$i.
Kornshell和BASH使这一过程变得更加容易,因为它们可以进行数学运算并为循环做更复杂的事情.
csh 循环很好,问题是您正在使用 exec,它在同一进程中用不同的程序替换当前程序(即 shell) 。由于其他人提供了 sh 版本,因此这里是一个 csh 版本:
#!/bin/csh
设 i = 11
而 ($i < 25)
tar xzf“myfile-1.0.$i”
# 检测脚本返回的错误
如果({./patch.sh})那么
echo "修补 $i 失败"
万一
@我 = $i + 1
结尾
不确定./patch.sh您是否正在测试它的存在或正在运行它?我在这里运行它,并测试结果 - true 意味着它返回零。或者:
# 检测脚本返回的错误
if (!{tar xzf "myfile-1.0.$i"}) then
echo "修补 $i 失败"
万一
| 归档时间: |
|
| 查看次数: |
48174 次 |
| 最近记录: |