0


shell脚本简介+编写


1、常用Linux命令
2、Linux下脚本编写
3、windows下CMD常用命令


文章目录


一、变量

1、系统预定义变量

常用系统变量

  1. $HOME

  1. $PWD

  1. $SHELL

  1. $USER

  1. $PATH

等。

  1. [root@VM-0-9-centos ~]# echo $HOME
  2. /root
  3. [root@VM-0-9-centos ~]#

显示当前所有

  1. Shell

变量:

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

2、自定义变量

基本语法

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

静态变量,不能unset

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

静态变量,不能重新赋值

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

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

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

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

  1. D="I love banzhang"

全局变量

  1. export 变量名
  2. exportE=3

3、特殊变量:

  1. n
  2. n
  3. n、#、
  4. *、
  5. ∗、@、$?

1. $n

  1. $n

(功能描述:n为数字,$0代表该脚本名称,$1-

  1. 9
  2. 代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如
  3. 9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如
  4. 9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如{10})

新建脚本

  1. parameter.sh
  1. #!/bin/bashecho'==========$n=========='echo$0# 文件名echo$1# 第1个入参echo$2# 第2个入参

执行脚本,并传入参数

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

2. $#

  1. $#

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

  1. echo'==========$#=========='echo$#

输入参数

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

**3.

  1. *、
  2. ∗、@**
  1. $*

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

  1. $@

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

  1. echo'==========$*=========='echo$*echo'==========$@=========='echo$@

输入参数

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

4. $?

$?

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

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

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

二、运算符

基本语法

  1. $((运算式))” $[运算式]”
  1. [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)
  1. # 判断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
  1. # 判断是否有写权限(主要用空格间隔)[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)
  1. # 查询文件是否存在[root@VM-0-9-centos ~]# [ -e /root/nohup.out ][root@VM-0-9-centos ~]# echo $?0[root@VM-0-9-centos ~]#

4、多条件判断

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

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

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

四、流程控制

1、if 判断

注意事项:

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

语法:单分支

  1. # 格式一if[ 条件判断 ];then
  2. 程序
  3. fi# 格式二if[条件判断]then
  4. 程序
  5. fi

语法:多分支

  1. if[ 条件判断式 ]then
  2. 程序
  3. elif[ 条件判断式 ]then
  4. 程序
  5. else
  6. 程序
  7. fi

案例:

  1. #! /bin/bashif[$1-eq1]thenecho"条件一"elif[$1-eq2]thenecho"条件二"fi

执行

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

2、case 语句

注意事项:

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

基本语法:

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

案例

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

执行

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

3、for 循环

1. 基本语法—1

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

案例

  1. #! /bin/bashsum=0for((i=0;i<=100;i++))dosum=$[$sum+$i]doneecho$sum

执行

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

2. 基本语法—2

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

案例

  1. #! /bin/bash# 打印数字foriin 变量1 变量2 变量3
  2. doecho"ban zhang : $i"done

执行

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

案例—比较加

  1. ""

区别

  1. #! /bin/bashecho'======================$*======================'foriin$*doecho"one: $i"doneecho'======================$@======================'forjin$@doecho"two $j"done# 加"",则$*会看成1个整体。echo'===================$*+ ""====================='foriiin"$*"doecho"one: $ii"done# 加"",会将参数分开执行。echo'===================$@+ ""====================='forjjin"$@"doecho"two $jj"done

执行

  1. [root@VM-0-9-centos shell]# sh ./for3.sh cls mly wls======================$*======================
  2. one: cls
  3. one: mly
  4. one: wls
  5. ======================$@======================
  6. two cls
  7. two mly
  8. two wls
  9. ===================$*+ ""=====================
  10. one: cls mly wls
  11. ===================$@+ ""=====================
  12. two cls
  13. two mly
  14. two wls
  15. [root@VM-0-9-centos shell]#

4、while循环

语法

  1. while[ 条件判断式 ]do
  2. 程序
  3. done

案例

  1. #! /bin/bashsum=0i=1while[$i-le100]dosum=$[$sum+$i]i=$[$i+1 ]doneecho$sum

执行

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

五、read读取控制台输入

语法

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

案例:

  1. #! /bin/bashread-t7-p"Enter your name:" NN
  2. echo$NN

执行

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

六、函数

1、系统函数

1.1 basename

语法:

输出最后一个

  1. /

之后内容

suffix:去除内容

  1. basename[string / pathname][suffix]

案例

  1. [root@VM-0-9-centos ~]# basename /data/test/my.txt
  2. my.txt
  3. [root@VM-0-9-centos ~]# basename /data/test/my.txt .txt
  4. my
  5. [root@VM-0-9-centos ~]# basename /data/test/my.txt xt
  6. my.t
  7. [root@VM-0-9-centos ~]#

1.2 dirname

取文件绝对路径

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

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

2、自定义函数

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

案例

  1. #! /bin/bashfunctionsum(){s=0s=$[$1+$2]echo"$s"# 自定义函数返回值return5;}# 调用sum函数sum12;

执行

  1. [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等命令都支持通过正则表达式进行模式匹配。

  1. cat /etc/passwd |grep r..t

在这里插入图片描述

八、案例

1、jar包启动脚本

  1. #!/bin/bash# 指定启动环境ENV=prod
  2. 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。
  1. Linux系统预留可三个文件描述符:012,他们的意义如下所示:
  2. 0——标准输入(stdin
  3. 1——标准输出(stdout
  4. 2——标准错误(stderr
标签: linux bash 运维

本文转载自: https://blog.csdn.net/weixin_44624117/article/details/128926048
版权归原作者 ha_lydms 所有, 如有侵权,请联系我们删除。

“shell脚本简介+编写”的评论:

还没有评论