shell脚本初步

变量,判断,重复动作:
位置参数:
        $#   传递到shell脚本的参数个数. 一般与shift联合使用,实现参数的逐个处理。
        $* ,$@ 在没有外加双引号的情况下,两者一样,都为列举shell的所有参数。并传递给程序。
        "$*" 将所有参数视作单个字符。
        "$@"将所有参数作为单个个体,并保留参数的内嵌空格。
特殊变量:
    POSIX的内置shell变量:
        #         目前参数个数
        @         传递给当前命令的命令参数,在双引号内,会展开给别参数,并保留空格。
        *        当前进程的参数。在双引号内,则将参数作为单一字符。
        -         在引用时给与shell的选项。
        ?        前一个命令的退出状态。
        $         shell进程的进程编号。processID
        0         shell程序名称。
        !        最近一个后台命令的processID
        PATH        命令查找路径。
        PS1         主要的命令提示符 普通用户为$ root用户为# (默认)
        PS2         行继续的提示符 >
        LC_COLLATE     用来排序当前的local名称。
参数展开:
    替换运算符:
    <1>${varname : -word} varname非空,则为varname 若空 则为word, word为默认值。
    <2>${varname:=word} varname存在且不是null 则返回varname值,否则,则设置word,并返回其值。
    <3>${varname:?message}varname存在且非空,返回其值,否则,显示varname:message并退出当前脚本,省略message则会出现默认信息:parameter null or not set
    <4>${varname:+word}varname存在且非空。返回word 否则返回null
    模式匹配运算符:
    ${variable#pattern}匹配pattern在variable里的最短部分 去除并返回剩余部分。
    ${variable##pattern}匹配pattern在variable里的最长部分 去除并返回剩余部分。
    ${variable%pattern}从结尾开始,匹配pattern在variable里的最短部分 去除并返回剩余部分

    ${variable%%pattern}从结尾开始,匹配pattern在variable里的最长部分 去除并返回剩余部分
    Examples.     path=/home/alleria/men/log.file.name
        运算符                    结果
        ${path#/*/}             alleria/men/log.file.name
        ${path##/*/}     log.file.name
        ${path%.*}     /home/alleria/men/log.file
        ${path%%.*}    /home/alleria/men/log
test命令:
用法:
    test [ -f file ]
    [ -f file ]
    if [ -f file ] && [ -n file1 ]
    [ -f file -o -n file1 ]    -o  为 逻辑 or  -a 为逻辑and
    测试否定 可加!  如 [ ! -f file ]
    所有的参数展开都需要用引号括起来,如 if [ -f "$file" ]
    字符串的比较时,通常在各个字符串的前面添加一个字母,如x   例如: if [ "x$file" = "x$file1" ]  以此来避免字符串的开始为-,将字符串误判为参数的情况发生
    test 的数字比较只用于整数,浮点数不可。
选项:
    -b file     file是设备文件
    -c file     file是字符文件
    -d file     file是目录
    -e file      file存在
    -f file     file是一般文件
    -g file     file有设置setgid位
    -u file     file有设置setuid
    -h file     file是一符号连接
    -L file     file是符号连接 等同-h
    -n string     string是非null    
    -p file    file是一个命名的管道
    -r file     file是可读的
    -w file    file是可写入的
    -S file    file是socket
    -s file     file不是空的
    -t n    文件描述符n指向一终端
    -x file    file可执行或者可查找路径
    -z string    string为null
    s1 = s2    两字符串相同
    s1 != s2    两字符串不同
    n1 -eq n2 两整数相等
    n1 -ne n2 两整数不等
    n1 -lt n2     n1 小于 n2
    n1 -gt n2 n1 大于 n2
    n1 -le n2 n1 小于或等于n2
    n1 -ge n2 n1 大于或等于n2
范例:
    if [ -f "$file" ]
        echo $file is a regular file
    elif [ -d "$file" ]
        echo $file is a directory
    fi
    if [ -x "$file" ]
        echo $file is not executable
    fi
if-elif-else-fi
    if pipeline
        [pipeline]
    then
        if-statements-true
    elif
        [pipeline]
    then
        statemens-elif-true
    else
        statmens-others-fails
    fi
case语句:
    case $1 in
        -f)
            ....
            ;;
        -d | --directory )
            ....
            ;;
        *)
            ....
            exit 1            
            ;;
        
    esac
for 语句:
    for  statement
    do    
        statement-true
    done
在statement-true处可使用while ease 语句

    ##################################################################################################



shell 在编写过程中的错误:1.在参数设置部分 将` ......`  写成了‘ ......’ (单引号)
               error_code:     line 45 : test : integer expression expected
                 2.在参数测验 null errorcode : unary operater expended
                 3. shift  SPELLING WRONG。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值