shell脚本简介+编写

简介: shell脚本简介+编写

一、变量

1、系统预定义变量

常用系统变量

$HOME$PWD$SHELL$USER$PATH等。

[root@VM-0-9-centos ~]# echo $HOME
/root
[root@VM-0-9-centos ~]#

显示当前所有Shell变量:set

[root@VM-0-9-centos ~]# set
BASH=/bin/bash
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()

2、自定义变量

基本语法

  • 定义变量:变量名=变量值
  • 撤销变量:unset 变量名
  • 声明静态变量:readonly变量,注意:不能unset
# 定义变量
A=5
# 撤销变量
unset A
# 静态变量
readonly B=3

静态变量,不能unset

[root@VM-0-9-centos ~]# readonly B=2
[root@VM-0-9-centos ~]# unset B
-bash: unset: B: cannot unset: readonly variable

静态变量,不能重新赋值

[root@VM-0-9-centos ~]# readonly C=3
[root@VM-0-9-centos ~]# echo $C
3
[root@VM-0-9-centos ~]# C=4
-bash: C: readonly variable
[root@VM-0-9-centos ~]# echo $C
3
[root@VM-0-9-centos ~]#

变量默认为字符串,无法进行数值计算

[root@VM-0-9-centos ~]# D=1+2
[root@VM-0-9-centos ~]# echo $D
1+2
[root@VM-0-9-centos ~]#

有空格,需要使用双引号或单引号括起来

D="I love banzhang"

全局变量

export 变量名
export E=3

3、特殊变量:n 、 n、n#、∗ 、 *、@、$?

1. $n

$n(功能描述:n为数字,$0代表该脚本名称,$1-9 代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如 9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如{10})

新建脚本parameter.sh

#!/bin/bash
echo '==========$n=========='
echo $0 # 文件名
echo $1 # 第1个入参
echo $2 # 第2个入参

执行脚本,并传入参数

[root@VM-0-9-centos ~]# ./parameter.sh 1 2 3 4
==========$n==========
./parameter.sh
1
2
[root@VM-0-9-centos ~]#

2. $#

$#:获取所有输入参数个数,常用于循环,判断参数的个数是否正确以及加强脚本的健壮性。

echo '==========$#=========='
echo $#

输入参数

[root@VM-0-9-centos ~]# ./parameter.sh a b c d e f 
==========$#==========
6
[root@VM-0-9-centos ~]#

3. ∗ 、 *、@

$*:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体

$@:这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待

echo '==========$*=========='
echo $*
echo '==========$@=========='
echo $@

输入参数

[root@VM-0-9-centos ~]# ./parameter.sh a b c d e f 
==========$*==========
a b c d e f
==========$@==========
a b c d e f
[root@VM-0-9-centos ~]#

4. $?

$?

最后一次执行的命令的返回状态。

如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了

[root@VM-0-9-centos ~]# echo "hello"
hello
[root@VM-0-9-centos ~]# echo $?
0
[root@VM-0-9-centos ~]#

二、运算符

基本语法

“$((运算式))” 或 “$[运算式]”
[root@VM-0-9-centos ~]# echo $[(2+3)*4]
20
[root@VM-0-9-centos ~]#

三、条件判断

1、两个整数之间比较

  • -eq:等于(equal)
  • -ne:不等于(not equal)
  • -lt:小于(less than)
  • -le:小于等于(less equal)
  • -gt:大于(greater than)
  • -ge:大于等于(greater equal)
# 判断23是否大于10
[root@VM-0-9-centos ~]# [ 23 -ge 10 ]
[root@VM-0-9-centos ~]# echo $?
0
[root@VM-0-9-centos ~]#

2、文件权限判断

  • -r:有读的权限(read)
  • -w:有写的权限(write)
  • -x:有执行的权限(execute
# 判断是否有写权限(主要用空格间隔)
[root@VM-0-9-centos ~]# [ -w nohup.out ]
[root@VM-0-9-centos ~]# echo $?
0
[root@VM-0-9-centos ~]#

3、文件类型判断

  • -e:文件存在(existence)
  • -f:文件存在并且是一个常规的文件(file)
  • -d:文件存在并且是一个目录(directory)
# 查询文件是否存在
[root@VM-0-9-centos ~]# [ -e /root/nohup.out ]
[root@VM-0-9-centos ~]# echo $?
0
[root@VM-0-9-centos ~]#

4、多条件判断

&&:表示前一条命令执行成功时,才执行后一条命令

||:表示上一条命令执行失败后,才执行下一条命令

# 执行成功
[root@VM-0-9-centos ~]# [ atguigu ] && echo OK || echo notOK
OK
# 执行失败
[root@VM-0-9-centos ~]# [ ] && echo OK || echo notOK
notOK

四、流程控制

1、if 判断

注意事项:

  • [ 条件判断式 ],中括号和条件判断式之间必须有空格。
  • if后要有空格。

语法:单分支

# 格式一
if [ 条件判断 ]; then
  程序
fi
# 格式二
if [条件判断]
then
  程序
fi

语法:多分支

if [ 条件判断式 ]
then 
  程序
elif [ 条件判断式 ]
then 
  程序
else
  程序
fi

案例:

#! /bin/bash
if [ $1 -eq 1 ]
then
        echo "条件一"
elif [ $1 -eq 2 ]
then
        echo "条件二"
fi

执行

[root@VM-0-9-centos shell]# sh ./if.sh 1
条件一
[root@VM-0-9-centos shell]# sh ./if.sh 2
条件二
[root@VM-0-9-centos shell]#

2、case 语句

注意事项:

  • case行尾必须为单词in,每一个模式匹配必须以右括号结束。
  • 双分号;;表示命令序列结束,相当于java中的break
  • 最后的*)表示默认模式,相当于java中的default

基本语法:

case $ 变量名 in
"值1")
  如果变量=1,则执行程序1
;;
"值2")
  如果变量=2,则执行程序2
;;
*)
如果都不符合以上,则执行此程序
;;
esac

案例

#! /bin/bash
case $1 in
"1")
        echo "变量一"
;;
"2")
        echo "变量二"
;;
*)
        echo "其它"
;;
esac

执行

[root@VM-0-9-centos shell]# sh case.sh 1
变量一
[root@VM-0-9-centos shell]# sh case.sh 2
变量二
[root@VM-0-9-centos shell]# sh case.sh 3
其它
[root@VM-0-9-centos shell]#

3、for 循环

1. 基本语法—1

for ((初始值;循环控制变量;变量变化))
do 
  程序
done

案例

#! /bin/bash
sum=0
for((i=0;i<=100;i++))
do
        sum=$[$sum+$i]
done
echo $sum

执行

[root@VM-0-9-centos shell]# sh ./for1.sh 
5050
[root@VM-0-9-centos shell]#

2. 基本语法—2

for 变量 in 值1 值2 值3...
do 
  程序
done

案例

#! /bin/bash
# 打印数字
for i in 变量1 变量2 变量3
do
        echo "ban zhang : $i"
done

执行

[root@VM-0-9-centos shell]# sh ./for2.sh 
ban zhang : 变量1
ban zhang : 变量2
ban zhang : 变量3
[root@VM-0-9-centos shell]#

案例—比较加""区别

#! /bin/bash
echo '======================$*======================'
for i in $*
do
        echo "one: $i"
done
echo '======================$@======================'
for j in $@
do
        echo "two $j"
done
# 加"",则$*会看成1个整体。
echo '===================$*+ ""====================='
for ii in "$*"
do
        echo "one: $ii"
done
# 加"",会将参数分开执行。
echo '===================$@+ ""====================='
for jj in "$@"
do
        echo "two $jj"
done

执行

[root@VM-0-9-centos shell]# sh ./for3.sh cls mly wls
======================$*======================
one: cls
one: mly
one: wls
======================$@======================
two cls
two mly
two wls
===================$*+ ""=====================
one: cls mly wls
===================$@+ ""=====================
two cls
two mly
two wls
[root@VM-0-9-centos shell]#

4、while循环

语法

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

案例

#! /bin/bash
sum=0
i=1
while [ $i -le 100 ]
do
        sum=$[ $sum+$i ]
        i=$[ $i+1 ]
done
echo $sum

执行

[root@VM-0-9-centos shell]# sh ./while.sh 
5050
[root@VM-0-9-centos shell]#

五、read读取控制台输入

语法

read  (选项)  (参数)
①选项:
-p:指定读取值时的提示符;
-t:指定读取值时等待的时间(秒)如果-t不加表示一直等待
②参数
  变量:指定读取值的变量名

案例:

#! /bin/bash
read -t 7 -p "Enter your name:" NN
echo $NN

执行

[root@VM-0-9-centos shell]# sh ./read.sh 
Enter your name:Mytest
Mytest
[root@VM-0-9-centos shell]#

六、函数

1、系统函数

1.1 basename

语法:

输出最后一个/之后内容

suffix:去除内容

basename [string / pathname] [suffix]

案例

[root@VM-0-9-centos ~]# basename /data/test/my.txt
my.txt
[root@VM-0-9-centos ~]# basename /data/test/my.txt .txt
my
[root@VM-0-9-centos ~]# basename /data/test/my.txt xt
my.t
[root@VM-0-9-centos ~]#
1.2 dirname

取文件绝对路径

从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分)

[root@VM-0-9-centos ~]# dirname /data/test/my.txt
/data/test
[root@VM-0-9-centos ~]#

2、自定义函数

  • 必须在调用函数地方之前,先声明函数。
  • 函数返回值:通过$?系统变量获得。
  • 未指定返回值,则以最后1条执行为准。
  • 制定返回值:return
[ function ] funname[()]
{
  Action;
  [return int;]
}

案例

#! /bin/bash
function sum()
{
        s=0
        s=$[$1+$2]
        echo "$s"
# 自定义函数返回值
return 5;
}
# 调用sum函数
sum  1 2;

执行

[root@VM-0-9-centos shell]# sh ./fun.sh 
3
[root@VM-0-9-centos shell]# echo $?
5
[root@VM-0-9-centos shell]#

七、匹配正则

在Linux中,grep,sed,awk等命令都支持通过正则表达式进行模式匹配。

cat /etc/passwd |grep r..t

八、案例

1、jar包启动脚本

#!/bin/bash
# 指定启动环境
ENV=prod
nohup /et/profile/jdk-1.8/java -jar -Xms8g -Xmx8g -Dfile.encoding=utf-8 demo-start-jar.jar  --spring.profiles.active=$ENV >> nohup.out 2>&1 &
  • nohup .... &:后台,不挂断地运行命令。
  • -Xms8g :堆最小内存
  • -Xmx8g:堆最大内存
  • -Dfile.encoding=utf-8
  • --spring.profiles.active=$ENV
  • 2>&1:错误输出2重定向到标准输出1。
Linux系统预留可三个文件描述符:0、1和2,他们的意义如下所示:
0——标准输入(stdin)
1——标准输出(stdout)
2——标准错误(stderr)
目录
相关文章
|
1天前
|
弹性计算 运维 监控
|
1天前
|
存储 弹性计算 运维
自动化收集员工信息的Shell脚本
【4月更文挑战第30天】
6 0
|
2天前
|
弹性计算 运维 Shell
使用shell 脚本打印图形3
【4月更文挑战第29天】
9 0
|
2天前
|
存储 弹性计算 运维
使用shell 脚本打印图形2
【4月更文挑战第29天】
9 0
|
2天前
|
弹性计算 运维 Shell
使用shell 脚本打印图形1
【4月更文挑战第29天】
9 0
|
2天前
|
存储 弹性计算 运维
调整虚拟机内存参数的shell 脚本
【4月更文挑战第29天】
8 0
|
2天前
|
弹性计算 运维 Shell
一键申请多个证书 shell 脚本
【4月更文挑战第29天】
9 1
|
2天前
|
弹性计算 运维 Shell
从shell脚本发送邮件
【4月更文挑战第29天】
9 0
|
3天前
|
弹性计算 运维 Shell
使用 shell 脚本打印图形
【4月更文挑战第29天】
10 1
|
3天前
|
弹性计算 运维 Shell