zoukankan      html  css  js  c++  java
  • shell学习(三)

    1、位置变量

    脚本:

    [root@localhost sh]# cat /opt/sh/test1.sh
    #/bin/bash
    echo "你执行的脚本名称及路径为: "$0
    echo "脚本的第一个参数为: "$1
    echo "脚本的第二个参数为: "$2
    echo "脚本的第三个参数为: "$3
    echo "脚本的第四个参数为: "$4
    echo "脚本的第五个参数为: "$5
    echo "脚本的参数个数为 : "$#
    echo "脚本的五个参数分别为: "$@

    执行结果

    [root@localhost sh]# /opt/sh/test1.sh 1 2 3 4 5 6 7 8
    你执行的脚本名称及路径为: /opt/sh/test1.sh
    脚本的第一个参数为: 1
    脚本的第二个参数为: 2
    脚本的第三个参数为: 3
    脚本的第四个参数为: 4
    脚本的第五个参数为: 5
    脚本的参数个数为 : 8
    脚本的五个参数分别为: 1 2 3 4 5 6 7 8

    2、if语句

    脚本:

    [root@localhost sh]# cat test2.sh
    #!/bin/bash
    a=$1

    if [ $a -eq 0 ];then

    echo "a is 0"

    elif [ $a -eq 1 ];then
    echo "a is 1"

    elif [ $a -eq 2 ];then
    echo "a is 2"
    else
    echo "a is not b"

    fi

    执行结果

    [root@localhost sh]# ./test2.sh 0
    a is 0
    [root@localhost sh]#
    [root@localhost sh]# ./test2.sh 1
    a is 1
    [root@localhost sh]# ./test2.sh 2
    a is 2
    [root@localhost sh]# ./test2.sh 3
    a is not b

    3、while循环

    脚本

    [root@localhost sh]# cat test3.sh
    #!/bin/bash

    i=0
    while [ $i -ne 10 ]
    do
    i=$(($i+1))

    echo $i
    done

    执行结果

    [root@localhost sh]# ./test3.sh
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    4、for循环

    用法一:

    #!/bin/bash  

    for((i=1;i<=10;i++));  

    do   

          echo $(expr $i * 3 + 1);  

    done  

    用法二:

    #!/bin/bash  

    for i in $(seq 1 10)  

    do   

    echo $(expr $i * 3 + 1);  

    done  

  • 相关阅读:
    IronPython 0.7.6 released
    《用 .NET 开发的轻量级 UI 测试自动化》 的VS2005版本
    VistaDB 2.1 database for .NET has been released
    ORMapper or Object DataBase
    为什么要用Generic
    我的新文章:NTier Server/(Smart)Client 应用程序的设计和开发
    Advanced .NET Remoting, Second Edition
    Windows Mobile 5.0发布!
    Ajax对构架影响的思考
    SmartCode 的源代码
  • 原文地址:https://www.cnblogs.com/heruiguo/p/8820509.html
Copyright © 2011-2022 走看看