暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

【Linux/Unix】csh中循环方法

微思研 2022-09-27
4168

1 简介

C shell(csh)以及升级版(tcsh)都是Unix shell,最初于20世纪70年代末期被加州大学伯克利分校的Bill Joy创建的。

2 语法

    while ( condition )
    # do something
    # command 1
    # command 2
    end


    或者

      set i = 1
      while ( i < 5 )
      # do something till i < 5
      # command 1
      # command 2
      @ i++ # 注意这里的 @
      end

      或者

        foreach n ( 1 2 3 4 5 )
        #command1
        #command2
        end

        3 示例

        3.1 示例1

          # 代码
          #!/bin/csh
          # demoloop.csh - Sample loop script
          set j = 1
          while ( $j <= 5 )
          echo "Welcome $j times"
          @ j++
          end


          # 运行
          chmod +x demoloop.csh
          ./demoloop.csh


          # 结果
          Welcome 1 times
          Welcome 2 times
          Welcome 3 times
          Welcome 4 times
          Welcome 5 times

          3.2 示例2

            #代码
            #!/bin/csh
            echo "Setting name servers...."
            foreach i ( ns1.cyberciti.biz ns2.cyberciti.biz )
            echo $i
            end


            # 运行结果
            Setting name servers....
            ns1.cyberciti.biz
            ns2.cyberciti.biz

            3.3 示例3

              #!/bin/csh
              foreach i (*)
              if (-f $i) then
              echo "$i is a file."
              endif
              if (-d $i) then
              echo "$i is a directory."
              endif
              end


              # 输出结果
              mycal.pl is a file.
              skl is a directory.
              x is a file.
              x.pl is a file.
              y is a file.

              4 说明

              除非是使用的内容是使用csh编写的,否则推荐使用shell(sh)。说明原文如下:
              Please note that csh was popular for many innovative features but csh has never been as popular for scripting. If you are writing system level rc scripts avoid using csh. You may want to use bin/sh for any scripts that might have to run on other systems.


              CSDN

              https://blog.csdn.net/weixin_44237659?spm=1011.2124.3001.5343

              文章转载自微思研,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

              评论