zoukankan      html  css  js  c++  java
  • linux sheel script demo

    demo1 (输入/输出)

    1.1. target :

        输入姓、名, 输出姓名

    1.2. create directory

         mkdir ~/bin

    1.3. create & edit sheel script

        vim fullname.sh

        note:  more comment is useful

    #!/bin/bash
    #Program
    #       User inputs his first name and last name . Program shows his full name.
    #History :
    #2017/04/10     logan   First lease
    PATH=/bin:$PATH #将当前目录加入系统环境
    export PATH
    read -p "Please input your firstname:" firstname
    read -p "Please input your lastname:"   lastname 
    echo -e "
    Your full name is :" ${firstname} ${lastname}

     1.4. run

       [rocky@localhost bin]$ sh fullname.sh

    demo2 (计算PI)

     2.1  target

           输入小数点后位数 输出PI

     2.2  create & edit

           vim cal_pi.sh

    #!/bin/bash
    #Program:
    #       Usre input a scale number to calculate pi number.
    #History:
    #2017/04/10 logan       First lease
    PATH=/bin:PATH
    export PATH
    echo -e "This program will calculate pi value. 
    "
    echo -e "You should input a float number to calculate pi value. 
    "
    read -p "This scale number (10-10000) ? " checking
    num=${checking:-"10"}
    echo -e "Starting calculate pi value. Be patient."
    time echo "scale=${num}; 4*a(1)"|bc -lq

     2.3 run

          输入 5

          输出  3.14156

    demo3 ($#  $@  $* 的使用)

     3.1 target

           输入参数, 显示相关结果

     3.2  create & edit sheel

           vim how_paras.sh

    #!/bin/bash
    #Program:
    #       Program shows the script name, parameters
    #History:
    #2017/04/11     logan   First release
    PATH=/bin:$PATH
    export PATH
    
    echo "The script name is        ==> ${0}"
    echo "Total parameter number is ==> $#"
    [ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." && eexit 0
    echo "Your whole parameter is  ==> '$@'"
    echo "The 1st parameter         ==> ${1}"
    echo "The 2nd parameter         ==> ${2}"

      3.3  run

    [logan@localhost bin]$ sh how_paras.sh zhang li wang
    The script name is    ==> how_paras.sh
    Total parameter number is ==> 3
    Your whole parameter is  ==> 'zhang li wang'
    The 1st parameter        ==> zhang
    The 2nd parameter        ==> li

     demo4 (shift 使用)

    4.1 vim how_shift.sh

    .....#省略注释 PATH......
    echo "The script name is        ==> ${0}"
    echo "Total parameter number is ==> $#"
    [ "$#" -lt 6 ] && echo "The number of parameter is less than 6. Stop here." && eexit 0
    echo "Your whole parameter is  ==> '$@'"
    shift
    echo "------------------shift one--------"
    echo "Total parameter number is ==>$#" 
    echo "The whole parameter is  $@"
    shift 3
    echo "------------------shift three------"
    echo "Total parameter number is ==>$#"
    echo "the whole parameter is ==> $@"

    4.2 run

    [logan@localhost bin]$ sh how_shift.sh one two three four five six
    The script name is    ==> how_paras.sh
    Total parameter number is ==> 6
    Your whole parameter is  ==> 'one two three four five six'
    ------------------shift one--------
    Total parameter number is ==>5
    The whole parameter is  two three four five six
    ------------------shift three------
    Total parameter number is ==>2
    the whole parameter is ==> five six
    [logan@localhost bin]$ vim how_paras.sh

    demo5 (if 条件判断式)

    5.1 vim  if_fi.sh

    #!/bin/bash
    #Program:
    #       This program shows the user's choice
    #History:
    #20170411       logan   first release
    PATH=/bin:$PATH
    export PATH
    
    read -p "Please input (Y/N): " yn
    
    if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ];then
            echo "OK, continue"
            exit 0
    fi
    
    if [ "${yn}" == "N" ] || [ "${yn}" == "n" ];then
            echo "Oh, interrupt!"
            exit 0
    fi
    
    echo "I don't know what your choice is" && exit 0

    note:  中括号内 变量及字符两边需要空格符,否则报错 '找不到命令'

    5.2 run

         sh if_fi.sh

    demo6 (日期计算)

    6.1 vim cal_retired.sh

    #!/bin/bash
    #Program:
    #       You input your demobilization date, I calculate how many days before you demobilize.
    #History:
    #2017/04/12     logan           first release
    PATH=/bin:$PATH
    export PATH
    
    #1. tell user the function of the shell script, and the format of the date
    echo "This program will try to calculate:"
    echo "How many days before your demobilization date..."
    read -p "Please input your demobilization date (YYYYMMDD ex>20131230): " date2
    
    #2. test the comment you just input with the Pattern
    date_d=$(echo ${date2} |grep '[0-9]{8}') # check length whether 8 of the input num
    if [ "${date_d}" == "" ];then
            echo "You input the wrong date format...."
            exit 1
    fi
    
    #3. begin to calculate the date
    declare -i date_dem=$(date --date="${date2}" +%s) #seconds count of the input date
    declare -i date_now=$(date +%s)                   #seconds count date now
    declare -i date_total_s=$((${date_dem}-${date_now})) #differ from the two date
    declare -i date_d=$((${date_total_s}/60/60/24))     #transfer to days
    if [ "${date_total_s}" -lt "0" ];then
            echo "You had been demobization before: " $((-1*${date_d})) " days ago"
    else
            declare -i date_h=$(($((${date_total_s}-${date_d}*60*60*24))/60/60))
            echo "You will demobilize after ${date_d} days and ${date_h} hours."
    fi

     6.2 run

        sh cal_retired.sh

       

    demo7 (case..esac)

    7.1 vim hello-3.sh

    #!/bin/bash
    #Program:
    #       Show "Hello" from $1.... by using case .... esac
    #History:
    #2017/04/12     logan   First lease
    PATH=/bin:$PATH
    export PATH
    
    case ${1} in
       "hello")
            echo "Hello, how are you?"
            ;;
       "")
            echo "You MUST input parameters, ex> {${0} someword}"
            ;;
       *)
            echo "Usage ${0} {hello}"
            ;;
    
    esac
          

    7.2 run

      sh hello-3.sh

    demo8 (function使用)

    vim demo.sh

    #!/bin/bash
    #Program:
    #       Show "one two three" from $1.... by using case .... esac
    #History:
    #2017/04/12     logan   First lease
    PATH=/bin:$PATH
    export PATH
    
    function printit(){
            echo -n "Your choice is " #-n means not enter to next line
    }
    
    case ${1} in
       "one")
            printit; echo ${1} | tr 'a-z' 'A-Z' #transfer to Upper
            ;;
       "two")
            printit; echo ${1} | tr 'a-z' 'A-Z' #transfer to Upper
            ;;
       "three")
            printit; echo ${1} | tr 'a-z' 'A-Z' #transfer to Upper
            ;;
       *)
            echo "Usage ${0} {one|two|three}"
            ;;
    
    esac

    8.2 run

     sh demo.sh one

    demo9

    9.1 vim while_do_done.sh

    #!/bin/bash
    #Program:
    #       Repeat question until user input correct answer.
    #History:
    #2017/04/12     logan   first release
    PATH=/bin:$PATH
    export PATH
    
    while [ "${yn}" != "yes" -a "${yn}" != "YES" ]
    do
            read -p "Please input yes/YES to stop this program: " yn
    done
    echo "OK! you input the correct answer."

    9.2 run

     sh while_do_done.sh

    demo10

    10.1 vim add_1_to_100.sh

    #!/bin/bash
    #Program:
    #       calculate the sum of num 1,2...100
    #History:
    #2017/04/12     logan   first release
    PATH=/bin:$PATH
    export PATH
    
    s=0
    i=0
    while [ "${i}" != "100" ]
    do
            i=$(($i+1))
            s=$(($s+i))
    done
    echo "The result of '1+2+3...+100' is ==> $s"

    10.2 run

     sh add_1_to_100.sh

    10.3 vim add_1_to_num.sh

    #!/bin/bash
    #Program:
    #       calculate the sum of num 1,2...100
    #History:
    #2017/04/12     logan   first release
    PATH=/bin:$PATH
    export PATH
    
    s=0
    i=0
    j=0
    while   [ "${j}" -le "0" ]
    do
            read -p "Please input the num: " j
    done
    while [ "${i}" != "${j}" ]
    do
            i=$(($i+1))
            s=$(($s+i))
    done
    echo "The result of '1+...+ ${j}' is ==> $s"

     demo11

    11.1 vim for_test.sh

    #!/bin/bash
    PATH=/bin:$PATH
    export PATH
    
    for animal in dog cat elephant
    do
            echo "There are ${animal}s..."
    done

    11.2 sh for_test.sh

    demo12

    12.1 target

    ping 局域网内 192.168.1.1~100的看通不通, seq(sequence)指令的使用

    12.2 vim pingip.sh

    #!/bin/bash
    #Program
    #       Use ping command to check the network's PC state
    #History
    #2017/04/14     logan   first release
    PATH=/bin:$PATH
    export PATH
    
    network="192.168.1"
    for sitenu in $(seq 1 100) #seq 为 sequence(连续)的缩写
    do
            ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && result=0 || result=1
            if [ "${result}" == 0 ];then
                    echo "Server ${network}.${sitenu} is UP."
            else
                    echo "Server ${network}.${sitenu} is DOWN."
            fi
    done

    12.3 run

     sh pingip.sh

    demo13

    13.1 target

    判断式(test)的使用

    13.2 vim dir_perm.sh

    #!/bin/bash
    #Program
    #       User input dir name, I find the permission of files.
    #History:
    #2017/04/14     logan   first release
    PATH=/bin:$PATH
    export PATH
    
    read -p "Please input a directory: " dir
    if [ "${dir}" == "" -o ! -d "${dir}" ]; then
            echo "The ${dir} is NOT exist in your system."
            exit 1
    fi
    
    filelist=$(ls ${dir})
    for filename in ${filelist}
    do
            perm=""
            test -r "${dir}/${filename}" && perm="${perm} readable"
            test -w "${dir}/${filename}" && perm="${perm} writable"
            test -x "${dir}/${filename}" && perm="${perm} executable"
            echo "The file ${dir}/${filename}'s permission is ${perm} "
    done

    13.3 run

      sh dir_perm.sh

    demo14

    14.1 description

         for循环的使用

    14.2 vim cal_1_nu.sh

    #!/bin/bash
    #Program
    #       Try do calculate 1+2+..+ ${your_input}
    #History:
    #2017/04/14     logan   first release
    PATH=/bin:$PATH
    export PATH
    
    read -p "Please input a number, I will count for 1+2..+ your_input:" nu
    
    s=0
    for (( i=1; i<=${nu}; i=i+1))
    do
            s=$((${s}+${i}))
    done
    echo "The result of '1+2+...${nu}' is ==> ${s}"

    demo15

    15.1 target

      随机选择3种菜

    15.2 vim what_to_eat3.sh

    #!/bin/bash
    #Program
    #       Try do tell you what you may eat.
    PATH=/bin:$PATH
    export PATH
    
    eat[1]="zhang san ji"
    eat[2]="li si ji"
    eat[3]='wang wu ji'
    eat[4]="zhang liu ji"
    eat[5]="qian qi ji"
    eat[6]="hao 1"
    eat[7]="hao 2"
    eat[8]="hao 3"
    eat[9]="hao 4"
    num=9
    
    count=0
    while [ "${count}" -lt 3 ]; do
            check=$(( ${RANDOM} * ${num} / 32767 + 1 ))
            flag=0
            if [ "${count}" -ge 1 ];then
                    for i in $(seq 1 ${count} )
                    do
                            if [ "${selectedNum[$i]}" == $check ];then
                                    flag=1
                            fi
                    done
            fi
            if [ ${flag} == 0 ];then
                    echo "You mat eat ${eat[${check}]}"
                    count=$((${count} + 1 ))
                    selectedNum[${count}]=${check}
    
            fi
    done

    demo16 (计算生日)

    16.1 vim birthday_howlong.sh

    #!/bin/bash
    #Program
    # how many days your birthday remain
    #History
    #2017/04/17     logan   first release
    read -p "Please input your birthday (MMDD, ex>0709): " bir
    now=`date +%m%d`
    if [ "$bir" == "$now" ];then
            echo "Happy birthday to you!!!"
    elif [ "$bir" -gt "$now" ];then
            year=`date +%Y`
            total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
            echo "Your birthday will be $total_d later"
    else
            year=$((`date +%Y`+1))
            total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
            echo "Your birthday will be $total_d later"
    fi

    demo17 (show accounts)

    17.1 vim show_account.sh

    #!/bin/bash
    accounts=`cat /etc/passwd | cut -d':' -f1`
    for account in $accounts
    do
            declare -i i=$i+1
            echo "The $i account is "$account" "
    done
  • 相关阅读:
    从一个小程序跳到另一个小程序的写法
    小程序input组件失焦的使用
    小程序弹框wx.showModal的使用
    小程序在选择某一个东西的时候,可以用if,else 来做
    reverse啥时候可以用
    js中array.some()的用法
    element ui的table的头部自定义
    逻辑表达式
    cookie
    命名
  • 原文地址:https://www.cnblogs.com/rocky-fang/p/6689592.html
Copyright © 2011-2022 走看看