zoukankan      html  css  js  c++  java
  • shell 学习笔记二

    一.break命令

    break命令允许跳出所有循环(终止执行后面的所有循环)。

    下面的例子中,脚本进入死循环直至用户输入数字大于5。要跳出这个循环,返回到shell提示符下,就要使用break命令。

    #!/bin/bash
    while :
    do
    echo -n "Input a number between 1 to 5: "
    read Num
    case $Num in
    1|2|3|4|5) echo "Your number is $Num!";;
    
    *) echo "You do not select a number between 1 to 5, game is over!"
    break
    ;;
    esac
    done

    在嵌套循环中,break 命令后面还可以跟一个整数,表示跳出第几层循环。例如:
    break n
    表示跳出第 n 层循环。


    下面是一个嵌套循环的例子,如果 var1 等于 2,并且 var2 等于 0,就跳出循环:

    #!/bin/bash
    for var1 in 1 2 3
    do
    for var2 in 0 5
    do
    if [ $var1 -eq 2 -a $var2 -eq 0 ]
    then
    break 2
    else
    echo "$var1 $var2"
    fi
    done
    done

    如上,break 2 表示直接跳出外层循环。运行结果:
    1 0
    1 5

    二.continue命令

    continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

    对上面的例子进行修改:

    #!/bin/bash
    while :
    do
    echo -n "Input a number between 1 to 5: "
    read aNum
    case $aNum in
    1|2|3|4|5) echo "Your number is $aNum!";;
    
    *) echo "You do not select a number between 1 to 5!"
    continue
    echo "Game is over!";; 
    esac
    done

    运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句
    echo "Game is over!"
    永远不会被执行。

    同样,continue 后面也可以跟一个数字,表示跳出第几层循环。

    再看一个 continue 的例子:

    #!/bin/bash
    
    NUMS="1 2 3 4 5 6 7"
    for NUM in $NUMS
    do
    Q=`expr $NUM % 2`
    if [ $Q -eq 0 ];then
    echo "Number is an even number!!"
    continue
    fi
    echo "Found odd number"
    done

    三.函数
    函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高。像其他编程语言一样,Shell 也支持函数。Shell 函数必须先定义后使用。

    Shell 函数的定义格式如下:
    function_name() {
    代码
    }

    如果你愿意,也可以在函数名前加上关键字 function:
    function function_name() {
    代码
    }

    例:

    lucy() {
    echo "Hello, Lucy"
    }
    
    mary() {
    echo "Hello,mary"
    lucy
    }
    
    mary 


    与C语言不同的是调用函数不加()直接写函数名即可

    函数返回值,可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。

    Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。

    如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值。
    例:

    #!/bin/bash
    funWithReturn(){
    echo "The function is to get the sum of two numbers..."
    echo -n "Input first number: "
    read Num1
    echo -n "Input another number: "
    read Num2
    echo "The two numbers are $Num1 and $Num2 !"
    return $(($Num1+$Num2))
    }
    funWithReturn
    ret=$?
    echo "The sum of two numbers is $ret !"

    运行结果:
    The function is to get the sum of two numbers...
    Input first number: 25
    Input another number: 50
    The two numbers are 25 and 50 !
    The sum of two numbers is 75 !

    函数返回值在调用该函数后通过 $? 来获得。

    在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...

    给函数传递参数

    chuancan() {
    echo "The paramiters is: $*"
    echo "The number of parameters is: $#"
    echo "The script name is: $0"
    echo "The script's pid is: $$"
    echo "The first parameter is: $1"
    echo "The second parameter is: ${12}"
    }

    chuancan a b c d e f g h i j k l m n
    注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。

    四.加载函数库文件到脚本中

    1、定义函数vim /opt/function.sh

    #!/bin/bash
    read_lab() {
    read -p "Enter a number: " num    #这是一个读取用户输入的函数
    }

    read_lab //调用函数


    2、在要执行的shell脚本中加载函数文件

    #!/bin/bash
    i=0
    while [ $i -lt 10 ]
    do
    . /opt/function.sh    #加载函数文件
    if [ $num -ne 9 ];then
    array[$i]=$num
    elif [ $num -eq 9 ];then
    break
    fi
    let i++
    done
    
    echo ${array[@]}    #输出函数元素

    五.重定向

    一般情况下,每个 Unix/Linux 命令运行时都会打开三个文件:
    · 标准输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据。
    · 标准输出文件(stdout):stdout 的文件描述符为1,Unix程序默认向stdout输出数据。
    · 标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。

    默认情况下,command > file 将 stdout 重定向到 file,command < file 将stdin 重定向到 file。

    如果希望 stderr 重定向到 file,可以这样写:
    $command 2 > file

    如果希望 stderr 追加到 file 文件末尾,可以这样写:
    $command 2 >> file
    表示标准错误文件(stderr)。

    如果希望将 stdout 和 stderr 合并后重定向到 file,可以这样写:
    $command > file.txt 2>&1       或     $command  &> file.txt

    $command >> file.txt 2>&1

    如果希望对 stdin 和 stdout 都重定向,可以这样写:
    $command < file1 >file2
    command 命令将 stdin 重定向到 file1,将 stdout 重定向到 file2。

    全部可用的重定向命令列表

    command > file
    将输出重定向到 file。
    command < file
    将输入重定向到 file。
    command >> file
    将输出以追加的方式重定向到 file。
    n > file
    将文件描述符为 n 的文件重定向到 file。
    n >> file
    将文件描述符为 n 的文件以追加的方式重定向到 file。
    n >& m
    将输出文件 m 和 n 合并。
    n <& m
    将输入文件 m 和 n 合并。
    << tag
    将开始标记 tag 和结束标记 tag 之间的内容作为输入。

    六.Here Document

    Here Document 目前没有统一的翻译,这里暂译为”嵌入文档“。Here Document 是 Shell 中的一种特殊的重定向方式,它的基本的形式如下:

    1. command << delimiter

    2.     document

    3. delimiter

    它的作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。

    注意:

    · 结尾的delimiter 一定要顶格写,前面不能有任何字符,后面也不能有任何字符,包括空格和 tab 缩进。

    · 开始的delimiter前后的空格会被忽略掉。


    下面的例子,通过 wc -l 命令计算 document 的行数:

    $wc -l << EOF    This is a simple lookup program    for good (and bad) restaurants    in Cape Town.EOF3$

    也可以 将 Here Document 用在脚本中,例如:

     #!/bin/bash

    cat << EOF

    This is a simple lookup program

    for good (and bad) restaurants

    in Cape Town.

    EOF

    运行结果:

    This is a simple lookup programfor good (and bad) restaurantsin Cape Town.


    下面的脚本通过 vi 编辑器将 document 保存到 test.txt 文件:

    #!/bin/sh

    filename=test.txt

    vi $filename <<EndOfCommands

    i

    This file was created automatically from

    a shell script

    ^[

    ZZ

    EndOfCommands

    运行脚本:

    $ sh test.shVim: Warning: Input is not from a terminal$

    打开 test.txt,可以看到下面的内容:

    $ cat test.txtThis file was created automatically froma shell script$

    七. /dev/null 文件

    如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null:

    $ command > /dev/null

    /dev/null 是一个特殊的文件,写入到它的内容都会被丢弃;如果尝试从该文件读取内容,那么什么也读不到。但是 /dev/null 文件非常有用,将命令的输出重定向到它,会起到”禁止输出“的效果。

    如果希望屏蔽 stdout 和 stderr,可以这样写:

    $ command > /dev/null 2>&1

    等同于

    $ command &> /dev/null 

    像其他语言一样,Shell 也可以包含外部脚本,将外部脚本的内容合并到当前脚本。

    Shell 中包含脚本可以使用:

    . filename

    source filename

    两种方式的效果相同,简单起见,一般使用点号(.),但是注意点号(.)和文件名中间有一空格。

    例如,创建两个脚本,一个是被调用脚本 subscript.sh,内容如下:

    url="http://see.xidian.edu.cn/cpp/view/2738.html"

    一个是主文件 main.sh,内容如下:

    #!/bin/bash

    . ./subscript.sh

    echo $url

    执行脚本:

    $chomd +x main.sh./main.shhttp://see.xidian.edu.cn/cpp/view/2738.html$

    注意:被包含脚本不需要有执行权限。

  • 相关阅读:
    elementui 表格格式化
    安装mongoDB出现的问题:无法启动
    学习笔记-angular 使用uuid
    cordova app打包apk签名
    ionic3 清除navpush的堆栈 (android真机返回键bug)
    ionic3 StatusBar 不显示问题
    ionic3+angular 倒计时效果
    windows下一台机器运行多个tomcat
    JDBC连接数据库小结
    工具类---提供精确的浮点数运算
  • 原文地址:https://www.cnblogs.com/dannylinux/p/8144748.html
Copyright © 2011-2022 走看看