The study summarizes the Linux Shell Programming

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Weixiaohuai/article/details/91350284

I. Introduction

It refers to a shell application that provides an interface through which the user interface to access the operating system kernel services. Multiple shell commands in syntax grouped together and saved in a text file to obtain what we call the shell script. shell script can easily interact with the system, complete systems management and batch processing tasks. shell is between the operating system and applications, play the role of a bridge. shell programming, as long as one can write code in a text editor and a script interpreter to explain the implementation of it. This article will summarize some of the syntax of shell programming, such as loop control, condition control, environment variables, custom variables, and so relevant content.

Two, shell programming

[A] shell script programming format requirements

  • Script must ! # / Bin / bash at the beginning, used to indicate the execution of the script interpreter;
  • Scripts need to be executable x (executable permission can be given as a shell script using the chmod command);
  • Script-defined variables, try to use all capital letters;
  • shell script .sh suffix as far as possible,

 

[B] Getting Started shell script

vim test01.sh
#!/bin/bash
echo "hello shell script!"
chmod 744 test01.sh

Given executable permissions:

This program is one of the most simple shell example, in fact, there are some Linux shell script command composed.

[C] execute a shell script

(1) absolute path ways: from the root directory using an absolute path to the current directory.

. (2) relative to the trail: shell scripts path relative to the current directory.

 

[D] shell variables
shell script variables can be divided into systematic variables and custom variables, system variables are variables that can be used globally, custom variables can be used only for the current shell script.

. (1) System variables:

echo $PATH

echo $HOME

(2) custom variables:

vim test02.sh

#!/bin/bash
NAME=weixiaohuai
echo $NAME

chmod 744 test02.sh
./test02.sh

(3) revocation of variables

Note that only non-static (readonly modified) variables in order to be revoked. The following example:

注意readonly修饰的变量不能被unset, 会报错:unset: HOBBY: cannot unset: readonly variable。

定义变量的一些规则:

  • 变量名称可以由字母、 数字和下划线组成, 但是不能以数字开头
  • 等号两侧不能有空格
  • 变量名称一般习惯为大写
     

【e】将命令的返回值赋给变量

vim test03.sh
#/bin/bash

#/bin/bash
#将ls -l /home命令执行的结果赋值给RES
RES=$(ls -l /home)
echo $RES

RES2=`cat /test03.sh`
echo $RES2

chmod 744 test03.sh
./test03.sh
  •  反引号, 运行里面的命令, 并把结果返回给变量。 
  • $(xxx)
     

 

【f】设置环境变量

Linux中的环境变量定义是需要在/etc/profile文件中,并且需要export出来,定义完环境变量如果需要马上生效的话,需要执行source /etc/profile用于刷新配置文件。

vim /etc/profile

NAME=weixiaohuai
export NAME

source /etc/profile

 

【g】位置参数变量

当我们执行一个 shell 脚本时, 如果希望获取到命令行的参数信息, 就可以使用到位置参数变量.。

  • $n : n为数字, $0 代表命令本身, $1 - $9 代表第一到第九个参数, 十个以上的参数需要用大括号包含, 如${10};
  • $* : 表示命令行中所有的参数, $*把所有的参数看成一个整体,如./test04.sh 1 2 3, $*输出为1 2 3这一个整体参数;
  • $@: 表示命令行中所有的参数, 不过$@把每个参数区分对待,如./test04.sh 1 2 3, $*输出为1,2, 3这三个参数;
  • $#: 表示所有参数的个数;
vim test04.sh

#/bin/bash

#获取到命令行命令本身
echo "命令行本身: $0"
echo "命令行第一个参数: $1"
echo "命令行第二个参数: $2"
echo "命令行第三个参数: $3"

chmod 744 test04.sh

./test04.sh 1 2 3

echo "整体命令行参数 : $*"
echo "分开命令行参数: $@"
echo "参数个数: $# "

 

【h】预定义变量

预定义变量,就是指事先已经定义好的变量, 我们可以直接在 shell 脚本中使用。

  • $$ : 当前进程的进程号(PID);
  • $! : 后台运行的最后一个进程的进程号(PID);
  • $? : 最后一次执行的命令的返回状态。 如果这个变量的值为 0, 证明上一个命令正确执行; 如果为非 0, 则证明上一个命令执行不正确了;
echo "当前进程的进程号:$$"
echo "后台运行的最后一个进程的进程号:$!"
echo "最后一次执行的命令的返回状态:$?"

 

【i】shell运算符

(1). 第一种方式:$((运算式)),注意每个子公式计算出来的结果都需要使用()包起来;

(2). 第二种方式:$[运算式];

vim test05.sh

#!/bin/bash


#$((运算公式))
echo $(((((1+2)*3))*4))

#$[运算公式]
echo $[(1+2)*3*4]

chmod 744 test05.sh
./test05.sh 

 

【j】条件判断语句

(1). 语法:

[ 条件表达式 ],注意[  ]左右有空格。非空返回 true。

if [  ]; then
echo "非空"
else
echo "空"
fi



if [ weixiaohuai ]; then
echo "非空"
else
echo "空"
fi

(2). 常用判断条件

  • 两个整数的比较

= 字符串比较
-lt 小于
-le 小于等于
-eq 等于
-gt 大于
-ge 大于等于
-ne 不等于

 

  • 按照文件权限进行判断

-r 有读的权限 [ -r 文件 ]
-w 有写的权限
-x 有执行的权限

 

  • 按照文件类型进行判断

-f 文件存在并且是一个常规的文件
-e 文件存在
-d 文件存在并是一个目录
 

if [ weixiaohuai ]; then
echo "非空"
else
echo "空"
fi

#判断wsh是否等于wsh
if [ "wsh" = "wsh" ]; then
echo "相等"
else
echo "不相等"
fi

#判断10是否大于0
if [ 10 -gt  0  ]; then
echo "大于"
else
echo "小于等于"
fi

#判断/home/wsh是否存在并且是目录
if [ -d /home/wsh  ]; then
echo "存在并且是一个目录"
else

(3). 示例:通过获取命令行输入的参数,如果输入的参数, 大于等于 90, 则输出 "优秀", 如果大于等于 80,则输出"良好",否则输出"不合格"。

vim test06.sh

#!/bin/bash
if [ $1 -gt 90 ]
then
        echo "优秀"
elif [ $1 -le 60 ]
then
        echo "不合格"
fi



chmod 744 test06.sh
./test06.sh

 

【k】case 语句

(1).基础语法
 

case $变量名 in
"值 1")
如果变量的值等于值 1, 则执行程序 1
;;
"值 2")
如果变量的值等于值 2, 则执行程序 2
;;
…省略其他分支…
*)
如果变量的值都不是以上的值, 则执行此程序
;;
esac

(2).示例:当命令行参数是 male 时, 输出 "男", 是 female 时, 就输出"", 其它情况输出 "保密"。

#/bin/bash
#当命令行参数是 male 时, 输出 "男", 是 female 时, 就输出"", 其它情况输出 "保密"
case $2 in
"male")
echo "男"
;;
"female")
echo "女"
;;
*)
echo "保密"
;;
esac

 

【m】for循环

(1). 语法:

语法一:
for 变量 in 值 1 值 2 值 3…
do
程序
done

语法二:
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done

(2). 示例:使用for循环输出命令行输入的参数

#!/bin/bash
for  i in $@
do
        echo "$i"
done

(3).示例:使用for循环计算0....9相加的和

#!/bin/bash

SUM=0
for (( i = 0; i < 10; i++ ))
do
        SUM=$[$SUM+$i]
done
echo "SUM = $SUM"

 

【n】while循环

(1). 语法:

while [ 条件判断式 ]
do
程序
done

(2). 示例:从命令行输入一个数 n, 统计从 1+..+ n 的值是多少。

#!/bin/bash

SUM=0
I=0

while [ $I -le $1 ]
do
        SUM=$[$SUM+$I]
        I=$[$I+1]
done

echo "SUM = $SUM"

 

【o】read 读取控制台输入

(1). 语法

read -p xxx PAR
-p xxx: 指定读取值时的提示符;


read -t 10 -p xxx PAR
-t 10: 指定读取值时等待的时间10(秒) , 如果没有在指定的时间内输入,那么控制台就停止锁定状态;
参数

变量PAR: 指定接收控制台输入的变量名称

(2). 示例

read -p "请输入:" PAR1
echo "你输入了: $PAR1"


#读取控制台输入,如果五秒之内没有在控制台输入,那么停止锁定命令行
read -t 5 -p "请输入:" PAR2
echo "你输入了: $PAR2"

-t指定输入的时间: 

 

【p】系统函数

(1). basename :返回完整路径最后 / 的部分, 常用于获取文件名。

(2). dirname:返回完整路径最后 / 的前面的部分, 常用于返回路径部分。

 

【q】自定义函数

#!/bin/bash
function sum(){
 val1=$1
 val2=$2
 val3=$(($1+$2))
 echo $val3
}

ret_val=$(sum 10 20)
echo $ret_val

注意:必须在调用函数地方之前,事先声明好函数,因为shell脚本是逐行运行。

 

三、总结 

本文通过详细的示例讲解的Linux中的Shell编程关于条件控制、循环控制、自定义函数的使用方法,在实际工作中,用到Shell的地方有很多,比如结合crontab定时任务调度定期备份我们的数据库;定期清理大文件等等,本文是笔者在学习shell编程的一些总结,希望对大家的学习有所帮助。

 

 

Guess you like

Origin blog.csdn.net/Weixiaohuai/article/details/91350284