美文网首页Shell脚本进阶
核心知识点(二)变量的高级用法

核心知识点(二)变量的高级用法

作者: Starlightskm | 来源:发表于2019-09-27 15:27 被阅读0次

    命令替换

    语法格式

    方法 语法格式
    方法一 command
    方法二 $(command)

    小例子一:取出系统中所有的用户并输出

    
    #!/bin/bash
    
    index=1
    
    users=`cat /etc/passwd | cut -d ":" -f 1`
    
    for user in $users
    
    do
    
        echo "this is $index user:$user"
    
        index=$((index+1))
    
    done
    
    

    小例子二: 根据系统时间计算今年或者明年

    
    [wanghui@www example_1]$ echo "This is $(date +%Y) year"
    
    [wanghui@www example_1]$ echo "This is $(($(date +%Y) +1 )) year"    ##时间计算+1
    
    [wanghui@www example_1]$ echo "$((20+30))"     #运算
    
    [wanghui@www example_1]$ num1=20
    
    [wanghui@www example_1]$ num2=70
    
    [wanghui@www example_1]$ echo "$(($num1 + $num2))"    #这样也可以echo "$((num1 + num2))"
    
    

    小例子三:计算今年过去了多少星期,还剩多少星期

    
    [wanghui@www example_1]$ vim example2.sh
    
    #!/bin/bash
    
    echo "This year has passed $(date +%j) days"
    
    echo "This year has passed $(($(date +%j)/7)) week"
    
    echo "There is $((365 - $(date +%j))) days before new year"
    
    echo "There is $(((365 - $(date +%j))/7)) weeks before new year"
    
    

    小例子四: 判定Nginx进程是否存在,不存在就自动拉起(https://segmentfault.com/a/1190000009723940

    
    #!/bin/bash
    
    nginx_process_num=$(ps -ef | grep nginx | grep -v grep | wc -l)
    
    if [ $nginx_process_num -eq 0 ];then
    
       systemctl restart nginx
    
    fi
    
    

    变量类型

    • declare=typeset
    image.png
    
    [wanghui@www shell_script]$ declare -r var2
    
    [wanghui@www shell_script]$ var2="hello java"
    
    ---------声明
    
    [wanghui@www shell_script]$ num1=10
    
    [wanghui@www shell_script]$ num2=$num1+10
    
    [wanghui@www shell_script]$ echo $num2
    
    10+10
    
    [wanghui@www shell_script]$ declare -i num3
    
    [wanghui@www shell_script]$ num3=$num1+10
    
    [wanghui@www shell_script]$ echo $num3
    
    [wanghui@www shell_script]$ declare -F   #展示系统函数
    
    [wanghui@www shell_script]$ declare -a array     #声明数组 
    
    [wanghui@www shell_script]$ array=("jones","mike","kobe","jordan")    #定义数组
    
    [wanghui@www shell_script]$ echo ${array[@]}    #列出所有元素
    
    

    声明环境变量

    
    [wanghui@www shell_script]$ num5=30
    
    [wanghui@www shell_script]$ echo $num5
    
    30
    
    [wanghui@www shell_script]$ vim test1.sh
    
    #!/bin/bash
    
    echo $num5
    
    [wanghui@www shell_script]$ echo $num5
    
    30
    
    [wanghui@www shell_script]$ bash test1.sh #不能引用num5
    
    [wanghui@www shell_script]$ declare -x num5  #声明即可
    
    [wanghui@www shell_script]$ bash test1.sh 
    
    30
    
    

    取消声明的变量

    
    declare +a
    
    declare +r
    
    declare +i
    
    declare +x
    
    

    expr计算

    image.png
    image.png
    
    [wanghui@www shell_script]$ num1=30
    
    [wanghui@www shell_script]$ num2=50
    
    [wanghui@www shell_script]$ expr $num1 \> $num2
    
    0
    
    [wanghui@www shell_script]$ num1=17
    
    [wanghui@www shell_script]$ num2=5
    
    [wanghui@www shell_script]$ expr $num1 + $num2
    
    22
    
    [wanghui@www shell_script]$ num3=$(expr $num1 + $num2)
    
    [wanghui@www shell_script]$ echo $num3
    
    22
    
    [wanghui@www shell_script]$ expr $num1 \* $num2
    
    85
    
    [wanghui@www shell_script]$ expr $num1 / $num2
    
    3
    
    [wanghui@www shell_script]$ expr $num1 % $num2
    
    2
    
    [wanghui@www shell_script]$ num3=$(($num1 + $num2))
    
    [wanghui@www shell_script]$ echo $num3
    
    22
    
    [wanghui@www shell_script]$ num3=$(($num1 * $num2))
    
    [wanghui@www shell_script]$ echo $num3
    
    85
    
    
    image.png

    练习

    提示用户输入一个正整数,然后计算1+2+3+4+....num,必须对num是否为正整数做出判断,不符合应当允许其再次输入

    
    [wanghui@www shell_script]$ vim sum.sh
    
    #!/bin/bash
    
    while true
    
    do
    
        read -p "Please input a positive number:" num
    
        expr $num + 1 &> /dev/null
    
        if [ $? -eq 0 ];then
    
            if [ `expr $num \> 0` -eq 1 ];then
    
                #echo "Yes,Positive Number"
    
                for ((i=1;i<=$num;i++))
    
                do
    
                   sum=`expr $sum + $i`
    
                done
    
                 echo "1+2+3+...+$num=$sum"
    
                 exit
    
            fi
    
        fi
    
        echo "error input unlegal.."
    
        continue
    
    done
    
    

    bc计算

    • bc是bash内建的运算器,支持浮点数运算

    • 内渐变量scale可以设置,默认是0

    image.png
    
    [wanghui@www shell_script]$ bc
    
    bc 1.06.95
    
    Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
    
    This is free software with ABSOLUTELY NO WARRANTY.
    
    For details type `warranty'. 
    
    1+2
    
    3
    
    3-2
    
    1
    
    3*8
    
    24
    
    9/3
    
    3
    
    10/3
    
    3
    
    scale=2
    
    10/3
    
    3.33
    
    [wanghui@www shell_script]$ echo "scale=9;2333.88/33" | bc    #脚本中使用方法
    
    70.723636363
    
    

    浮点数运算

    
    #!/bin/bash
    
    read -p "input a number:" num1
    
    read -p "input a number:" num2
    
    echo "scale=2;$num1/$num2" | bc
    
    

    相关文章

      网友评论

        本文标题:核心知识点(二)变量的高级用法

        本文链接:https://www.haomeiwen.com/subject/jztbuctx.html