zoukankan      html  css  js  c++  java
  • shell脚本补缺

    一、执行脚本的三种方法。

    chmod  +x  添加执行权限

    • ./example.sh          相对路径
    • root/test/example.sh  绝对路径
    • bash example.sh         不用添加执行权限也可 

    二、shell 变量

    临时变量:是 shell 程序内部定义的,使用范围仅限于定义它的程序,对其它程序不可见。包括:用户自定义变量、位置变量和预定变量。

    永久变量:是环境变量,其值不随 shell 脚本的执行结束而消失。

    [root@vmware ~]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/home/user/bin

    # 用作运行某个命令的时候,本地查不到某个命令或文件,会到这个声明的目录中查找。

    三、将一个命令的执行结构赋给变量。

    [root@vmware ~]# a=`date`
    [root@vmware ~]# echo $a
    2018年 05月 23日 星期三 08:33:46 PDT
    [root@vmware ~]# b=$(ls -l)
    [root@vmware ~]# echo $b
    总用量 12 -rw-r--r--. 1 root root 64 5月 23 04:01 a.sh drwxr-xr-x. 2 root root 6 5月 23 01:26 local -rwxr-xr-x. 1 root root 33 5月 23 02:58 t.sh -rw-r--r--. 1 root root 4 5月 23 01:41 users

    四、多个变量同时声明需要加双引号或者单引号,否则报错。

    [root@vmware ~]# name="steven tom mike"
    [root@vmware ~]# echo $name
    steven tom mike
    [root@vmware ~]# name=steven tom mile
    bash: tom: 未找到命令...

    单双引号区别:

      单引号之间的内容原封不动的指定给了变量

      双引号取消了空格的作用,特殊符号的含义保留。

    五、位置变量和特殊变量。

    位置变量:shell 解释执行用户的命令时,将命令行第一个字作为命令名,而其他字作为参数。由出现在命令行上的位置确定的参数成为位置参数。

    [root@vmware ~]# ./example.sh file1 file2 file3
    $0 这个程序文件名 example.sh
    $n 这个程序的第n个参数值,n=1...N

    特殊变量:一开始执行script 脚本时就会设定,且不能被修改,但我们不叫他只读的系统变量,而叫他特殊变量。这些变量当一执行程序时就有了,用户无法将一般的系统变量设定成只读的。

    $*  这个程序的所有参数

    $#  这个程序的参数个数

    $$  这个程序的 PID

    $!   执行上一个后台程序的 PID

    $?  执行上一个指令的返回值

    [root@vmware ~]# vim test.sh
    #!/bin/sh
    echo "$* 表示这个程序的所有参数:"
    echo "$# 表示这个程序的参数个数"
    
    touch /tmp/a.txt
    echo "$$ 比嗾使程序的进程ID"
    
    touch /tmp/b.txt &
    echo "$! 执行上一个后台指令的PID"
    ~
    
    
    [root@vmware ~]# bash test.sh
     表示这个程序的所有参数:
    0 表示这个程序的参数个数
    29458 比嗾使程序的进程ID
    29460 执行上一个后台指令的PID

    六、

    [root@vmware ~]# read a b c
    11 22 33
    [root@vmware ~]# echo $a $b $c
    11 22 33

    七、expr命令 : 运算

    [root@vmware ~]# expr 3 + 5
    8
    [root@vmware ~]# var1=8
    [root@vmware ~]# var2=2
    [root@vmware ~]# expr $var1 - 5
    3
    [root@vmware ~]# expr $var1 / $var2
    4
    [root@vmware ~]# expr $var1 * $var2
    16
    #!/bin/sh
    a=10
    b=20
    c=30
    value1=`expr $a + $b + $c`
    echo "the value of value1 is $value1"
    value2=`expr $a + $c / $b`
    echo "the value of value2 is $value2"
    [root@vmware ~]# bash test.sh
    the value of value1 is 60
    the value of value2 is 11

    七、变量测试语句:test

    格式:  test  测试条件

    测试范围:整数,字符串,文件

    字符串和变量:

    test str1==str2  是否相等

    test str1!=str2   是否不相等

    test str1  测试字符串是否不为空

    test -n  str1 测试字符串是否为空 或  test -z  str1 测试字符串为空

    测试整数:

    test int1 -eq int2

    test int2 -ge int2    >=

    test int1 -gt int2     >

    test int1 -le int2     <=

    test int1 -lt  int2     <

    test int1 -ne int2

    也可以省略写成:[int1 -lt int2]

    文件测试:

    test -d file   #测试是否为目录

    test -f  file   

    test -x file

    test -r file

    test -w file

    test -e file   测试文件是否存在

    test -s file   测试大小是否为空

    八、流程控制语句:

    语法:

    if  条件

    then

    语句

    fi

    扩展   ;  分好,表示两个两个命令写在一行。互不影响。

    [root@vmware curl-7.50.3]# cd /opt ; ls
    rh
    [root@vmware ~]# cat test.sh
    #!/bin/sh
    
    echo "if test"
    if [ -x /bin/ls ] ; then
    /bin/ls
    fi

    2、多流程控制:

    if 条件1   ;  then

      命令1

    else

      命令2

    fi

    3、多个条件的联合

    -a 或  &&  :  逻辑与,仅当两个条件都成立时,结果为真

    -o 或 ||   :   逻辑或。两个条件有一个成立,结果为真。

    4、复杂的if语句

    语法:

    if  条件1  ;  then

      命令1

    elif  条件2   ;  then

      命令2

    else

      命令n

    fi

    #!/bin/sh
    
    echo "input a file name:"
    read file_name
    
    if [ -d $file_name ] ; then
        echo "$file_name is a dir"
    elif [ -f $file_name ] ; then
        echo "$file_name is file"
    elif [ -c $file_name -o -b $file_name ] ; then
        echo "$file_name is a device file"
    else
        echo "$file_name is an unknow file"
    fi
  • 相关阅读:
    Firebase REST API
    python 获取一个网页里的a 标签
    python 抓一下 循环的访问也可以
    mezzanine 历险记
    django cms 5月第一弹
    数据中心虚拟化技术
    linux ftp 服务器 ,web 服务器 配置
    linux DHCP 服务器
    linux DNS 服务器 配置
    lib.form.php
  • 原文地址:https://www.cnblogs.com/sunshine-long/p/9082059.html
Copyright © 2011-2022 走看看