Basic syntax of shell script (all cases in this article can be run directly)

**Concept** : Shell is a command line interpreter, which provides users with an interface system-level program that sends requests to the Linux kernel to run programs. Users can use Shell to start, suspend, stop, and even write some programs .Shell is a command line interpreter, which provides users with an interface system-level program that sends requests to the Linux kernel to run the program. The user can use Shell to start, suspend, stop, and even write some programs.

-**Execution method**
-The first one:
1. Assign executable permissions to the compiled shell script (-x)

[root@fan102 ~]# sudo chmod 744 myshell.sh

2. Execute the script

#绝对路径
[root@fan102 ~]# /Users/fanbuer/myshell.sh
#相对路径
[root@fan102 ~]# ./myshell.sh

-The second type (not recommended)
  does not require authorization
  to pass directly (sh+space+script)

#绝对路径
[root@fan102 ~]# sh /Users/fanbuer/myshell.sh
#相对路径
[root@fan102 ~]# ./myshell.sh


-**Shell variables** The variables in
  Linux Shell are divided into **system variables** and user **custom variables**.
    -System variables

#$PATH、#HOME、$PWD、$SHELL、$USER
#注意必须大写,并且=左右不能有空格
echo "path=$PATH"

  -Definition of shell variables
       1. Basic syntax      

#定义变量:变量=值
#撤销变量:unset 变量
#声明静态变量:readonly 变量,注意:不能 unset

#自定义变量
#输出:B=10
A=100
echo "B=$A"

#撤销变量A
#输出:B=
unset A
echo "B=$A"

     2. Rules for defining variables
        1) Variable names can be composed of letters, numbers and underscores, but cannot start with numbers.
         2) There can be no spaces on both sides of the equal sign
         3) The variable name is generally used to uppercase


      3. Assign the return value of the command to a variable (emphasis)

#第一种
# A=`ls -la` 反引号,运行里面的命令,并把结果返回给变量 A 
RESULT=`ls -l /home`
echo $RESULT

#第二种
# A=$(ls -la) 等价于反引号
MY_DATE=$(date)
echo "date=$MY_DATE"

   -Set environment variables

#1) export 变量名=变量值 (功能描述:将 shell 变量输出为环境变量)
#2) source 配置文件 (功能描述:让修改后的配置信息立即生效)
#3) echo $变量名 (功能描述:查询环境变量的值)

#实例:在/etc/profile 文件中定义 TOMCAT_HOME 环境变量,并查看环境变量TOMCAT_HOME 的值

#1.在/etc/profile 文件中定义 TOMCAT_HOME 环境变量
TOMCAT_HOME=/opt/tomcat
export TOMCAT_HOME

#2.使配置文件生效
source /etc/profile

#3.查看环境变量(可在任何目录下操作)
echo $TOMCAT_HOME

    -Positional parameter variables       

#$n (功能描述:n 为数字,$0 代表命令本身,$1-$9 代表第一到第九个参数,十以上的参数,十 以上的参数需要用大括号包含,如${10}) 
#$* (功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体) 
#$@(功能描述:这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待) 
#$#(功能描述:这个变量代表命令行中所有参数的个数)
#$?# 执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误)

#案例:编写一个 shell 脚本 myshell.sh , 在脚本中获取到命令行的各个参数信息
echo "$0 $1 $2"
echo "$*"
echo "$@"
echo "参数个数=$#"
# 控制台测试
[root@fan102 ~]# /.myshell.sh 10 20
/.myshell.sh 10 20
10 20
10 20
参数个数=3

   -Predefined variables

#$$ (功能描述:当前进程的进程号(PID)) 
#$! (功能描述:后台运行的最后一个进程的进程号(PID)) 
#$? (功能描述:最后一次执行的命令的返回状态。 0:上一个命令 正确执行;非0(具体是哪个数,由命令自己来决定),执行不正确了。)

echo "当前进程号=$$" 
echo "最后执行的进程号=$!" 
echo "执行值=$?"


-**Operator**

#1) “$((运算式))”或“$[运算式]”
#2) expr m + n 注意 expr 运算符间要有空格
#3) expr m - n
#4) expr *, /, % 乘,除,取余

#案例1:计算(2+3)x4的值
#1)$((运算式))
RESULT1=$(((2+3)*4))
echo "result1=$RESULT1"

#2)$[运算式](推荐)
RESULT2=$[(2+3)*4]
echo "result2=$RESULT2"

#3)expr
TEMP=`expr 2 + 3`
RESULT3=`expr $TEMP \* 4`
echo "result3=$RESULT3"

#案例 2:请求出命令行的两个参数[整数]的和
SUM=$[$1+$2]
echo "SUM=$SUM"
# 控制台测试
[root@fan102 ~]# /.myshell.sh 10L 20L
result1=20
result2=20
result3=20
SUM=30


 -**Condition Judgment**

#[ condition ](注意 condition 前后要有空格)
#常用判断条件
#1) 两个整数的比较 = 字符串比较
#-lt 小于
#-le 小于等于
#-eq 等于
#-gt 大于
#-ge 大于等于
#-ne 不等于
#2) 按照文件权限进行判断
#-r 有读的权限 [ -r 文件 ]
#-w 有写的权限
#-x 有执行的权限
#3)按照文件类型进行判断
#-f 文件存在并且是一个常规的文件
#-e 文件存在
#-d 文件存在并是一个目录
#案例1:判断连个字符串是否相等
if [ "abc" = "abc" ]
then
        echo "相等"
fi
#案例2:判断两个数大小
if [ 23 -gt 21 ]
then
        echo "大于"
fi
#案例3:/root/install.log 目录中的文件是否存在
if [ -e /root/install.log ]
then
        echo "存在"
fi

 

-**Flow control**
    1. if statement    

#基本语法
#第一种
if [ 条件判断式 ];then
     程序 
fi 

#第二种(推荐)
if [ 条件判断式 ] 
then 
     程序 
elif [条件判断式]
then 
     程序 
fi
#案例:请编写一个 shell 程序,如果输入的参数,大于等于 60,则输出 "及格 了",如果小于 60, 则输出 "不及格"
if [ $1 -ge 60 ] 
then 
    echo "及格" 
elif [ $1 -lt 60 ]
then 
    echo "不及格" 
fi

    2. case statement

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

    3. The for statement

基本语法 1 
for 变量 in 值 1 值 2 值 3…
do
    程序 
done
基本语法 2
for ((初始值;循环控制条件;变量变化))
do
    程序 
done

    4. While statement

基本语法
while [ 条件判断式 ]
do
    程序 
done

 

 -**read read console input**

#read(选项)(参数) 
#选项:
#-p:指定读取值时的提示符;
#-t:指定读取值时等待的时间(秒),如果没有在指定的时间内输入,就不再等待 
#参数 变量:指定读取值的变量名

#案例 1:读取控制台输入一个 num 值
read -p "情输入一个数:" NUM1
echo "你输入的数为:$NUM1"

#案例 2:读取控制台输入一个 num 值,在 10 秒内输入。
read -t "情输入一个数:" NUM2
echo "你输入的数为:$NUM2"

 

-**Function**
    1. System function

basename 基本语法
	功能:返回完整路径最后 / 的部分,常用于获取文件名
		basename [pathname] [suffix]
		basename [string] [suffix]  (功能描述:basename 命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来。
	选项: 
	    suffix 为后缀,如果 suffix 被指定了,basename 会将 pathname 或 string 中的 suffix 去掉。
		dirname 基本语法
	功能:返回完整路径最后 / 的前面的部分,常用于返回路径部分 
		dirname 文件绝对路径 (功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的 部分),然后返回剩下的路径(目录的部分))

    2. Custom functions

自定义函数
基本语法 
[ function ] funname[()] 
{ 
    Action; 
    [return int;]
}
调用直接写函数名:funname [值]

 

 -**Notes**

# 表示单行注释

:<<!
多行注释的内容
!

+++++++++++++++++++++++++++++++++++++++++
+ If you have any questions, you can +Q: 1602701980 Discuss together +
+++++++++++++++++++++++++++++++++++++++++

Guess you like

Origin blog.csdn.net/shenyuye/article/details/107716912