• Shell 语法 if 、 case 、for 、 while、 until 、select 、repeat、子函数


    if语法 :
     
    if [ expression ] 
       then
       commands
    elif [ expression2 ]
       then
       commands
    else
       commands
    fi
     
     
     
    case 语法:
     
    case string1 in
       str1)
        commands;;
       str2)
        commands;;
       *)
        commans;;
    esac
     
     
     
    循环语句 for 语法: 
     
    for  var in list

      do

        commands
      done
     
      在此形式时,对在list中的每一项,for语句都执行一次,list可以使包括几个单词的、由空格隔开的变量,也是可以直接输入的几个值,每执行一次循环,var都被赋予list中的当前值,直到最后一个为止;
     
     
    第二种形式:
     
    for var
    do
      commands;
    done 
    使用这种形式时,对变量var中的 每一项,for语句都执行一次,此时,外壳程序假定变量 var
    中包含外壳程序在命令行的所有位置参数;
     
     
     
     
    while 循环语句:
     
    while expression
     do 
       commands;
     done
     
     
     
    until循环语句:
     
    只是条件为假的时候,才会去执行until语句

    until expression
    do
      commands 
    done 
     
     
     
    shift 语句:
      shift 命令用来将存储在位置参数中的当前值左移一个位置;
     
      
       $1=-r  $2=file1  $3=file3
     执行shift命令:
       shift
     你会得到:
    $1 = file1 $2 = file2 
     
    也可以指定每次偏移的位数:
     shift 2     // 意味着每次移动两个位置;
     
     
    while [ "$1" ]
    d o
    if [ "$1" = "-i" ] then
      infile = " $ 2 "
       shift 2
    elif  [ "$1" = "-o" ]
       then   
       outfile = " $ 2 "
       shift 2
    else
       echo "Program $0 does not recognize option $1"
    fi
    done
      tr a-z A-Z <$infile >$outfile      // 将小写转化诶大写, 输入的文字是;  // 
     
     
     
     
    select  选择语句:
     
     
    select menuitem [in list_of_items]
    do
       commands;
    done
     
    当select语句执行时,shell为在list_of_items 中的每一项创建一个标有数字的菜单项;;
    list_of_items 可以是包含几个条目的变量, 就像是choose1 choose2,或者直接在命令中输入的选择项;
     
    select menuitem in choice1 choice2 choice3
     
    如果没有list_of_items ,select语句则使用命令行的位置参数,就像for表达式一样;
     
     
     
     
    repeat 语句:  
      
     repeat 语句只存在于tcsh中,在pdksh 和bash中没有相似的语句。 repeat 语句用来使一个单
    一的语句执行指定的次数,erpeat语句如下:
       repeat count commands;
    下面给出repeat语句的一个例子。它读取命令行后的一串数字。并根据数字在屏幕上分行输出句号.
     


    #
    foreach num ($*)
       repeat $num echo -n "."
       echo ""
    end
    任何repeat 语句都可以用while 或 f o r语句重写。  repeat语句只是更加方便而已。

     
     
     
    子函数: 
      外壳函数可以定义自己的函数,就像C或是其他语言一样,使用函数的最大好处
    就是使得程序更为清晰,可读, 
    fname(){
       shell commands;
    }
     
     
    使用函数的时候:
    fname  per1 per2 per3 
     
  • 相关阅读:
    WAF与IPS的区别总结
    web后门排查与高效分析web日志技巧
    如何做一名好的web安全工程师?
    从“黑掉Github”学Web安全开发
    DNS劫持
    万网上如何将IP和申请的域名绑定
    如何申请网站域名
    什么是域名?什么网站名?什么是URL?
    myeclipse svn 插件去除已经保存的密码方法
    SVN中检出 和 导出 的区别
  • 原文地址:https://www.cnblogs.com/vagabond/p/4996336.html
Copyright © 2020-2023  润新知