• shell基础之bash


    一直单单知道部署服务器等命令,shell语言还没有用心学习过,简单的学习下以供不时之需

    .sh:bash脚本文件

    很多时候需要多个命令来完成一项工作,而这个工作又常常是重复的,这个时候我们自然会想到将这些命令写成sh脚本,下次执行下这个脚本一切就都搞定了,下面就是发布代码的一个脚本示例。

    需要注意的是,BASH 程序被执行后,实际上 Linux 系统是另外开设了一个进程来运行的。故,BASH 程序是在一个新的进程中运行的,所以该程序中的变量定义和赋值不会改变其他进程或原始 Shell 中同名变量的值,也不会影响他们的运行。

    #在demo中编辑脚本执行命令
        vi demo.sh
            #声明文件类型,并且该文件是一个bash程序,需要由/bin目>录下的bash程序执行
            #!/bin/bash    
    
            #This is a very simple example
    
            #echo输出字符串,注意后面没有分号,-e开启转义
            echo -e 'Hello Word
    '
    
    #注意,需要权限
        chmod 777 demo.sh
    #执行脚本
        ./demo.sh             

    查找:

    #三个命令找出 bash 所在的位置
        1.locate bash 
        2.find / -name bash 2> /dev/null 
        3.whereis bash

    简单变量:

    #!/bin/bash    
    echo str   #输出null 
    str='hello word'
    echo $str  #hello word  

    局部变量:

    #!/bin/bash    
    HELLO=Hello
    function hello(){
    local HELLO=World
    echo HELLO } echo HELLO
    hello
    echo $HELLO
    

      上述代码是错误的,报错:syntax error: unexpected end of file。原因是函数的最后一个命令须要以分号‘;’结尾(有的是因为文件格式错误,vi打开后:set ff须要是unix。因为这是在windows中编译好文件再复制到linux的,set ff=unix就可以了 )。当然即使敲上分号依然报错:

    ./shell_demo: line 10: syntax error near unexpected token `echo'
    ./shell_demo: line 10: `echo HELLO; } echo HELLO'
    

      这就是语法错误了,正确语法应该是:

    #!/bin/bash    
    HELLO=Hello
    function hello(){
    local HELLO=World
    echo $HELLO;
    }
    echo HELLO
    hello
    echo $HELLO
    

      输出

    HELLO
    World
    Hello
    

      可以看到,全局变量HELLO的值并没有改变,hello执行的只是函数中的值。

    基本流程控制语法:

    if...then...else:

    #!/bin/bash    
    a=1
    b=10
    c=0
    if [ $a -gt $b ] 
    then 
        echo 'Good,1'
    elif [[ $a == 1 && $a == $b ]]
    then 
        echo 'a等于1 且 a等于b'
    elif [ $a -lt $b ]
    then 
        echo 'OK,1'
    else
        echo 'Bad,'$a
    fi
    
    if [ -z $c ]
    then
        echo '字符c为0'
    else
        echo '字符c不为0'
    fi

      变成一行就是

    #!/bin/bash
    a=1
    b=10
    c=0
    if [ $a -gt $b ]; then echo 'Good,1'; elif [[ $a == 1 && $a == $b ]]; then echo 'a等于1 且 a等于b'; elif [ $a -lt $b ]; then echo 'OK,1'; else echo 'Bad,'$a; fi
    
    if [ -z $c ]
    then
        echo 'c字符长度为0,返回true'
    else
        echo 'c字符长度不为0'
    fi 

      输出

    OK,1
    c字符长度不为0

      note:if后要有空格;变量前后要有空格;符号‘[’前后也要有空格;符号‘]’后要有分号‘;’且,then echo后也要有‘;’;最后要以fi结尾,即倒写关键字。到处都是空格,不然command not found。初次调试,找了好久...

      另,关系运算符还有-eq、-ne、-ge、-le、!=就不举例子了。布尔运算符还有||。字符运算符还有-n(字符串长度是否为0,不为0返回 true)、str(字符串是否为空,不为空返回 true)。

    for...in...:

    #!/bin/bash
    for day in Sun Mon Tue Wed Thu Fri Sat
    do
        echo $day done
    done
    

      输出

    Sun done
    Mon done
    Tue done
    Wed done
    Thu done
    Fri done
    Sat done
    

    while:  

    #!/bin/bash
    a=1
    while [ $a -lt 5 ]
    do
        echo $a done
        let 'a++'
    done
    

      输出

    1 done
    2 done
    3 done
    4 done
    

    until:

    #!/bin/bash
    a=0
    until [ ! $a -lt 5 ]
    do
        echo $a
        a=`expr $a + 1`
    done
    

      输出

    0
    1
    2
    3
    4
    

      until 也是循环控制语句,不过条件返回为true时才停止循环

    case:  

    #!/bin/bash
    echo '请输入键盘上任意字符:'
    echo '你输入的字符为:'
    read Keypress
    case "$Keypress" in
        [a-z]) echo "Lowercase letter";;
        [A-Z]) echo "Uppercase letter";;
        [0-9]) echo "Digit";;
        *) echo "Punctuation,whitespace,or other";;
    esac
    

      输出

    请输入键盘上任意字符:
    你输入的字符为:
    6
    Digit
    

      "read Keypress" 一句中的 read 语句表示从键盘上读取输入

      待续...

    break:退出循环

    #!/bin/bash
    a=1
    while [ $a -lt 5 ]
    do
    if [ $a == 3 ]
    then
        echo $a done
        break
    fi
    let 'a++'
    done
    

      输出

    3 done

    continue:退出当前循环,进入下个循环

    #!/bin/bash
    for day in Sun Mon Tue Wed Thu Fri Sat
    do
    if [ $day == Tue ]
    then
        continue
    else
        echo $day done
    fi
    done
    

      输出

    Sun done
    Mon done
    Wed done
    Thu done
    Fri done
    Sat done
    

    函数的使用:

    function fuc(){
        echo '我是fuc函数';
    }
    
    fuc2(){
        echo '我是fuc2函数'
    }
    fuc
    fuc2
    

      输出

    我是fuc函数
    我是fuc2函数
    

    文件包含:

    . shell_demo2
    ./shell_demo2
    source ./shell_demo2
    

      输出

    我是shell_demo2文件
    我是shell_demo2文件
    我是shell_demo2文件
    

    expr表达式计算:

    a=`expr 2 + 2`
    echo $a
    

      输出

    4
    

      note:注意符号‘`’。其他的还有-、*、/、%就不举例了

    test检查某个文件是否存在:

    if test -e ./bush
    then
        echo "文件存在"
    else
        echo "文件不存在"
    fi
    

      输出

    文件不存在
    

    printf打印输出:

    printf "%-10s %-8s %-4.2f
    " 郭靖 男 66.1234
    

      输出

    郭靖     男      66.12
    

      note:“-”表示左对齐,没有表示右对齐,10表示字符宽度,s表示显示字符串,4.2f表示4位float数字保留两位小数。

    数组:

    array=(1 9 66 78 as8 -)
    echo '默认读取第一个:'${array}
    echo '读取下标为0的:'${array[0]}
    echo '读取下标为1的:'${array[1]}
    echo '读取所有的:'${array[*]}
    echo '还是读取所有的:'${array[@]}
    echo '获取数组长度:'${#array[@]}
    echo '还是获取数组长度:'${#array[*]}
    

      输出

    默认读取第一个:1
    读取下标为0的:1
    读取下标为1的:9
    读取所有的:1 9 66 78 as8 -
    还是读取所有的:1 9 66 78 as8 -
    获取数组长度:6
    还是获取数组长度:6
    

      note:bash只支持一维数组,不支持多维数组,并且没有限定数组的大小

    文件测试运算符: 

    #!/bin/bash
    file="/home/demo"
    
    if [ -r $file ]
    then
       echo "文件可读"
    else
       echo "文件不可读"
    fi
    
    if [ -w $file ]
    then
       echo "文件可写"
    else
       echo "文件不可写"
    fi
    
    if [ -x $file ]
    then
       echo "文件可执行"
    else
       echo "文件不可执行"
    fi
    
    if [ -f $file ]
    then
       echo "文件为普通文件"
    else
       echo "文件为特殊文件"
    fi
    
    if [ -d $file ]
    then
       echo "文件是个目录"
    else
       echo "文件不是个目录"
    fi
    
    if [ -s $file ]
    then
       echo "文件不为空"
    else
       echo "文件为空"
    fi
    
    if [ -e $file ]
    then
       echo "文件存在"
    else
       echo "文件不存在"
    fi
    

      输出

    文件可读
    文件可写
    文件可执行
    文件为特殊文件
    文件是个目录
    文件不为空
    文件存在
    

      当然还有很多,这里只是一些常用的

    参数传递:

    echo '参数1:'$1
    echo '参数2:'$2
    echo '参数3:'$3
    

      输出

    [root@localhost demo]# ./shell_demo 我是参数1 我是参数2 我是参数3
    参数1:我是参数1
    参数2:我是参数2
    参数3:我是参数3  

     敲了这么多,有一点要切记,空格狠重要。空格狠重要。空格狠重要。

    还有很多,常用的应该就这么多了,待续...

     demo传送

  • 相关阅读:
    2018.12.1 区块链论文翻译
    2018.11.29 区块链论文翻译
    jshell 一个代码片段测试工具(jdk9后新增的功能)
    java 的var 定义变量有点鸡肋...
    小心Math.abs(-2147483648)的坑
    java获取同级目录下的文件
    java获取formdata里的所有参数
    No enclosing instance of type VolatleTest is accessible. Must qualify the allocation with an enclosing instance of type VolatleTest
    if else太多怎么代替,太难维护?可以使用spring-plugin 插件系统
    设计一个泛型的获取数组最大值的函数.并且这个方法只能接受Number的子类并且实现了Comparable接口
  • 原文地址:https://www.cnblogs.com/two-bees/p/10525962.html
Copyright © 2020-2023  润新知