[Shell] Writing Shell Scripts in Linux Operating Systems

Shell script files are suffixed with .sh, and the first line of the script is generally "#!/bin/bash", which is a special line of script declaration, indicating that the following statements are interpreted and executed by the /bin/bash program.
Statements beginning with # indicate comment information.

Conditional statements

Single branch if statement:

if [条件测试语句] //条件测试语句返回值为0则表示条件成立,若返回值不为0则跳过then
then
    需要执行的命令
fi

double branch if statement

if [条件测试语句]
then
    需要执行的命令
else
    需要执行的命令
fi

multi-branch if statement

if [条件测试语句]
then
    需要执行的命令
elif [条件测试语句]    
then
    需要执行的命令
else
    需要执行的命令
fi

case statement

case "$变量名" in
变量值1
      需要执行的命令
      ;;
变量值2
      需要执行的命令
      ;;
*) //*表示可匹配任意值,若找不到任何匹配的值则执行默认模式“*)”
      需要执行的命令
esac

Example 1:

1. Write a service script named demo (take starting, stopping, and restarting the sleep process as an example, which can be replaced with other service processes in practice)

[root@localhost ~]# vi demo
#!/bin/bash
case "$1" in
start)  //启动进程
  echo "正在启动sleep服务..."
  if sleep 7200 & //默认情况下,进程都是属于前台进程,若要设置进程为后台进程,则在启动参数后面加 & 
  then
     echo "OK"
  fi
  ;;
stop) //杀死进程
  echo "正在停止sleep服务..."
  kill "sleep" &> /dev/null //"&>"表示将正常执行时的信息和出现错误时的错误提示信息保存在空文件/dev/null中。">"是覆盖原文件, ">>"是在原文件上追加内容。
  echo "OK"
  ;;
status) //判断并提示进程状态
  if grep "sleep"
  then
     echo "sleep服务已经启动"
  else
     echo "sleep服务已经停止"
  fi
  ;;
restart)//重启服务,先停止再启动
  $0 stop  //$0是预定义变量,会输出当前执行的脚本的名称
  $0 start
  ;;
*)
  echo "用法:$0 {start|stop|status|restart}"
esac   
[root@localhost ~]#chmod +x demo  //添加可执行权限

2. Execute the demo script

[root@localhost ~]# ./demo start
正在启动sleep服务...OK
[root@localhost ~]# ./demo stop
正在停止sleep服务...OK
[root@localhost ~]# ./demo status
sleep服务已经停止
[root@localhost ~]# ./demo reload
用法:./demo {start|stop|status|restart}

loop statement

for 变量名 in 取值列表
do
   需要执行的命令
done
while [条件测试语句]
do
  需要执行的命令
done     

Example 2

1. Writing a script

[root@localhost ~]#vi name.txt  //取值列表文件
Tom
Pat
Tim
[root@localhost ~]#vi chkname.sh
#!/bin/bash
HLIST=$(cat name.txt)     //需正确填写name.txt文件所在路径;cat命令用于连接、显示文件内容
for NAME in $HLIST
do 
  echo "The name is $NAME"
done
[root@localhost ~]#chmod +x demo.sh

2. Execute the script

[root@localhost ~]# ./demo.sh
The name is Tom
The name is Pat
The name is Tim

predefined variables

The following variables in the script are pre-defined special variables, used in the script to represent a special meaning.

  • $#: Indicates the number of positions of commands executed in the command line
    For example :
    [root@localhost ~]# ./bak.sh /root/local /etc/init.d
    $# indicates (output) 2

  • $*: indicates the content of the location of the command executed in the command line
    For example :
    [root@localhost ~]# ./bak.sh /root/local /etc/init.d
    then $* indicates (output) /root/local /etc/init.d

  • $? : Indicates the return status after the execution of the previous command, the return value of 0 means the execution is correct, and the return value of non-0 means the execution is abnormal

  • $0: Indicates the name of the currently executed script
    For example :
    [root@localhost ~]# ./bak.sh /root/local /etc/init.d
    $0 means (output) ./bak.sh

Use exit in the script

Adding exit 0 at the end of the command body executed in the script means that the program executes normally and exits. If adding exit 1, it means that the program is abnormal and exits; when using the echo $? command, the value of exit will be returned.

"Linux Network Services and Shell Scripting Guide" by Xiao Rui-

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325875467&siteId=291194637