zoukankan      html  css  js  c++  java
  • shell script的简单使用

    shell script的简单介绍

    shell变量

    1.命名规则

    • 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头
    • 中间不能有空格,可以使用下划线(_)
    • 不能使用标点符号。
    • 不能使用bash里的关键字

    2.定义变量:name=value
    3.使用变量:$name
    4.只读变量:readonly name
    5.删除变量:uset name
    6.变量类型

    • 局部变量:当前shell有效
    • 环境变量:所有的应用程序有效

    shell字符串

    1. 双引号:可解析变量,可以出现转义字符
    2. 单引号:不解析变量,原样输出
    3. 字符串拼接:
    name="hello"
    # 使用双引号拼接
    greeting="hello, "$name" !"
    greeting_1="hello, $name} !"
    echo $greeting  $greeting_1
    # 使用单引号拼接
    greeting_2='hello, '$name' !'
    greeting_3='hello, ${name} !'
    echo $greeting_2  $greeting_3
    

    输出结果为:

    hello, hello ! hello, hello} !
    hello, hello ! hello, ${name} !
    
    1. 获取字符串长度
    string="abcd"
    echo ${#string} #输出 4
    
    1. 提取子字符串
    string="hello world"
    echo ${string:1:4}#输出 ello
    # 字符串下标从0开始
    
    1. 查找子字符串
    string="hello world"
    echo `expr index $string e`#输出2
    

    7.反引号和$()一样,里面的语句当作命令执行

    shell数组

    1. 数组定义:arrname=(value0 value1 value3)
    2. 数组读取:${arrname[]},中括号内为数组下标,从0开始
    3. 获取数组长度:length=${#arrname[@]},*号也可以
    4. 或许数组单个元素的长度:lengthn=${#arrname[n]},n为数组下标

    shell注释

    1. 单行注释:#
    2. 多行注释:<<EOF以EOF结束,EOF可以是任意字母

    shell传递参数

    1. 向脚本传参
    • $0:执行的文件名
    • $1:为第一个参数
    • $2:为第二个参数 以此类推
    1. 特殊参数
    • $#:传递到脚本的参数个数
    • $$:脚本运行的当前进程ID
    • $!:后台运行的最后一个进程ID
    • $?:回传码,显示最后命令的退出状态
    • $@,$*:显示所有传递的参数

    shell运算符

    1. 算数运算符

    假设变量a=10,b=20

    运算符 说明 举例
    + 加法 expr $a + $b 结果为 30。
    - 减法 expr $a - $b 结果为 -10。
    * 乘法 expr $a * $b 结果为 200。
    / 除法 expr $b / $a 结果为 2。
    % 取余 expr $b % $a 结果为 0。
    = 赋值 a=$b 将把变量 b 的值赋给 a。
    == 相等 用于比较两个数字,相同则返回 true。 [ $a == $b ] 返回 false。
    != 不相等 用于比较两个数字,不相同则返回 true。 [ $a != $b ] 返回 true。
    1. 关系运算符
    运算符 说明 举例
    -eq 检测两个数是否相等,相等返回 true。 [ $a -eq $b ] 返回 false。
    -ne 检测两个数是否不相等,不相等返回 true。 [ $a -ne $b ] 返回 true。
    -gt 检测左边的数是否大于右边的,如果是,则返回 true。 [ $a -gt $b ] 返回 false。
    -lt 检测左边的数是否小于右边的,如果是,则返回 true。 [ $a -lt $b ] 返回 true。
    -ge 检测左边的数是否大于等于右边的,如果是,则返回 true。 [ $a -ge $b ] 返回 false。
    -le 检测左边的数是否小于等于右边的,如果是,则返回 true。 [ $a -le $b ] 返回 true。
    1. 逻辑运算符
    运算符 说明 举例
    && 逻辑的 AND [[ $a -lt 100 && $b -gt 100 ]] 返回 false
    || 逻辑的 OR [[ $a -lt 100 || $b -gt 100 ]] 返回 true
    1. 布尔运算符
    运算符 说明 举例
    ! 非运算,表达式为 true 则返回 false,否则返回 true。 [ ! false ] 返回 true。
    -o 或运算,有一个表达式为 true 则返回 true。 [ $a -lt 20 -o $b -gt 100 ] 返回 true。
    -a 与运算,两个表达式都为 true 才返回 true。 [ $a -lt 20 -a $b -gt 100 ] 返回 false
    1. 字符串运算符
      假设变量a=hello,b=world
    运算符 说明 举例
    = 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
    != 检测两个字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。
    -z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。
    -n 检测字符串长度是否不为 0,不为 0 返回 true。 [ -n "$a" ] 返回 true。
    $ 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true
    1. 文件测试运算符
    操作符 说明 举例
    -b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
    -c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -c $file ] 返回 false。
    -d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。
    -f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。
    -g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。
    -k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。
    -p file 检测文件是否是有管道,如果是,则返回 true。 [ -p $file ] 返回 false。
    -u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。
    -r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
    -w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
    -x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
    -s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
    -e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true

    流程控制

    1. if 语句语法格式:
    if condition
    then
        command1 
        command2
        ...
        commandN 
    fi
    
    1. if else 语法格式:
    if condition
    then
        command1 
        command2
        ...
        commandN
    else
        command
    fi
    
    1. if else-if else 语法格式:
    if condition1
    then
        command1
    elif condition2 
    then 
        command2
    else
        commandN
    fi
    

    for循环一般格式为:

    for var in item1 item2 ... itemN
    do
        command1
        command2
        ...
        commandN
    done
    
    1. while 循环一般格式
    while condition
    do
        command
    done
    
    1. until 循环一般格式
    until condition
    do
        command
    done
    
    1. case in格式
    case 值 in
    模式1)
        command1
        command2
        ...
        commandN
        ;;
    模式2)
        command1
        command2
        ...
        commandN
        ;;
    esac
    
    1. 跳出循环:
    • break跳出所有循环
    • continue跳出当前循环

    函数定义

    输入输出重定向

    1. 文件描述符
    • 标准输入 0
    • 标准输出 1
    • 标准错误输出 2
    1. stdin<file 标准输入重定向
    2. stdout>file 标准输出重定向
    3. stderr>>file 标准错误输出重定向,以追加的方式

    shell文件包含

    1. .filename
    2. source filename
  • 相关阅读:
    Python SyntaxError: Missing parentheses in call to 'print'
    Python SyntaxError: Missing parentheses in call to 'print'
    Ubuntu virtualenv 创建 python3 虚拟环境 激活 退出
    Ubuntu virtualenv 创建 python3 虚拟环境 激活 退出
    Python 类似 SyntaxError: Non-ASCII character 'xc3' in file
    Python 类似 SyntaxError: Non-ASCII character 'xc3' in file
    238 ES5新增方法:forEach()、map()、filter()、some()、every(),some、forEach、filter区别,from,of,find,findIndex
    237 借用原型对象,继承【父类】方法 【寄生组合继承】
    236 子构造函数继承父构造函数中的属性
    235 继承 之 call()
  • 原文地址:https://www.cnblogs.com/emptyCup/p/13033824.html
Copyright © 2011-2022 走看看