加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – csh / sh for循环 – 怎么样?

发布时间:2020-12-16 01:19:44 所属栏目:安全 来源:网络整理
导读:我正在尝试编写一个在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
我正在尝试编写一个在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 failedn";
   }
}

有人知道怎么做吗?

谢谢

csh做的循环很好,问题是你使用的是exec,它在同一个进程中用不同的程序替换当前程序(也就是shell).由于其他人提供了sh版本,这里有一个csh:
    #!/bin/csh
    set i = 11
    while ($i &lt 25)
        tar xzf "myfile-1.0.$i"

        # detect an error was returned by the script   
        if ({./patch.sh}) then     
            echo "Patching to $i failed"   
        endif
        @ i = $i + 1
    end

不确定./patch.sh你是在测试它的存在还是运行它?我在这里运行它,并测试结果 – true表示它返回零.或者:

        # detect an error was returned by the script   
        if (!{tar xzf "myfile-1.0.$i"}) then     
            echo "Patching to $i failed"   
        endif

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读