学习shell script

from 鸟哥linux私房菜 第13章

shell script类似于以前的批处理文件bat

注释#,第一行通常是#!/bin/bash声明这个script使用的bash的名称

执行方式:./*.sh和sh *.sh是创建子进程bash,source *.sh是在父进程中直接执行sh。区别之一是*.sh中的变量是否还在父进程中。

命令执行控制:

  每个command执行完后可以通过$?查看状态,0表示执行成功,1表示执行失败

  &&: command 1 && command 2; 当command 1执行成功时才执行command 2

  ||: command 1 || commnad 2; 当command 1执行失败时才执行command 2

  例子:command && echo "success" || echo "fail"

test命令测试文件的属性:-e -f -d -r -w -x。条件判断时用-a/-o代替&&/||

默认变量$#, $@, $*等

接收变量$variable的两种方式,直接执行式(在*.shh后加变量$1)和交互式(read -p "input a variable" variable)

简单判断符号[ ]

if then elif else fi语法

[dmtsai@study bin]$ cp ans_yn-2.sh ans_yn-3.sh
[dmtsai@study bin]$ vim ans_yn-3.sh
#!/bin/bash
# Program:
#       This program shows the user's choice
# History:
# 2015/07/16    VBird   First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

read -p "Please input (Y/N): " yn

if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ]; then
    echo "OK, continue"
elif [ "${yn}" == "N" ] || [ "${yn}" == "n" ]; then
    echo "Oh, interrupt!"
else
    echo "I don't know what your choice is"
fi

case in esac语法

[dmtsai@study bin]$ vim show123.sh
#!/bin/bash
# Program:
#    This script only accepts the flowing parameter: one, two or three.
# History:
# 2015/07/17    VBird    First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

echo "This program will print your selection !"
# read -p "Input your choice: " choice   # 暫時取消,可以替換!
# case ${choice} in                      # 暫時取消,可以替換!
case ${1} in                             # 現在使用,可以用上面兩行替換!
  "one")
    echo "Your choice is ONE"
    ;;
  "two")
    echo "Your choice is TWO"
    ;;
  "three")
    echo "Your choice is THREE"
    ;;
  *)
    echo "Usage ${0} {one|two|three}"
    ;;
esac

function语法

[dmtsai@study bin]$ vim show123-3.sh
#!/bin/bash
# Program:
#    Use function to repeat information.
# History:
# 2015/07/17    VBird    First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

function printit(){
    echo "Your choice is ${1}"   # 這個 $1 必須要參考底下指令的下達
}

echo "This program will print your selection !"
case ${1} in
  "one")
    printit 1  # 請注意, printit 指令後面還有接參數!
    ;;
  "two")
    printit 2
    ;;
  "three")
    printit 3
    ;;
  *)
    echo "Usage ${0} {one|two|three}"
    ;;
esac

循环loop

  while do done与until do done

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."
until [ "${yn}" == "yes" -o "${yn}" == "YES" ]
do
    read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."

  for do done

[dmtsai@study bin]$ vim cal_1_100-2.sh
#!/bin/bash
# Program:
#     Try do calculate 1+2+....+${your_input}
# History:
# 2015/07/17    VBird    First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
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+3+...+${nu}' is ==> ${s}"

shell script追踪与调试 shell -nvx *.sh

猜你喜欢

转载自www.cnblogs.com/ustcrliu/p/9037791.html