博客
关于我
shell及脚本4——shell script
阅读量:661 次
发布时间:2019-03-16

本文共 4516 字,大约阅读时间需要 15 分钟。

一.格式

1.1 开头

必须以 "# !/bin/bash"  开头,告诉系统这是一个bash shell脚本注意#与!中间有空格。

二.语法

2.1 数值运算

可以用declare -i声明为数值类型,也可以用

var = $((数值运算)),注意是两个括号

2.3 善用判断式

2.3.1 test命令

test命令可以测试,可以利用测试的结果走后续流程。测试文件和文件属性还是比较方便的。

:~/test$ test -e nofile && echo "exist"||echo "not exit"not exit:~/test$ touch nofile:~/test$ test -e nofile && echo "exist"||echo "not exit"exist

 

2.3.2 []

  常与if搭配使用,跟test差不多。

  •   test命令的参数都可用;
  •   判断两个变量是否相等时,==与=效果一样,习惯上一般用==,与一般的编程语言一致。
  •   [ "$myname" == "VBird Tsai" ],[]两边必须有空格,一般都用引号, == 的两边也必须有空格。[]内部的每个组件都要用空格分割
:~/test$ [ -n "$HOME" ] && echo "exist"||echo "not exist"   #测试HOME变量是否为空,-n参数与test相同,test的参数都可用exist :~/test$ [ -n "$HOME1" ] && echo "exist"||echo "not exist" not exist :~/test$ myname="VBird Tsai" :~/test$ [ "$myname" == "VBird Tsai1" ] && echo "=="||echo "!=" != :~/test$ [ "$myname" == "VBird Tsai" ] && echo "=="||echo "!=" ==

2.3.2 shell script的输入参数

 shell script脚本可以像命令一样输入参数。在script中可以用一些特殊符号获取这些输入的参数。

./sh_test.sh input1 input2 input3     $0         $1     $2     $3 $0:文件名 $1:第一个参数 $n:第n个参数 $#:参数个数 $@:所有参数,“$1” “$2” "$n" $*:所有参数,“$1 $2 $n" ,中间有空格
:~/test$ cat sh_test.sh # !bin/bashecho "shell script file name: $0"echo "input pera num: $#"echo "all pera:$@"echo "1st pera $1"echo "2st pera $2"
:~/test$ ./sh_test.sh input1 input2 input3shell script file name: ./sh_test.shinput pera num: 3all pera:input1 input2 input31st pera input12st pera input2

 

 

shift具有去除变量的作用,向左移

:~/test$ cat sh_test.sh # !bin/bashecho "shell script file name: $0"echo "input pera num: $#"echo "all pera:$@"echo "1st pera $1"echo "2st pera $2"echo "2st pera $3"shiftecho "shell script file name: $0"echo "input pera num: $#"echo "all pera:$@"echo "1st pera $1"echo "2st pera $2"echo "2st pera $3"  shift 2echo "shell script file name: $0"                                                                echo "input pera num: $#"echo "all pera:$@"echo "1st pera $1"echo "2st pera $2"echo "2st pera $3" :~/test$ ./sh_test.sh input1 input2 input3 shell script file name: ./sh_test.sh input pera num: 3 all pera:input1 input2 input3 1st pera input1 2st pera input2 2st pera input3 shell script file name: ./sh_test.sh input pera num: 2 all pera:input2 input3 1st pera input2 2st pera input3 2st pera shell script file name: ./sh_test.sh input pera num: 0 all pera: 1st pera 2st pera 2st pera

 

 

2.4 条件判断

2.4.1 if...then

1.单层判断形式

 

if [条件判断式] then   指令 fi  #if倒着写,表示结束

 

2.多个判断条件

  可以用 或||,与&&逻辑连接,if [条件1]||[条件2]

多层判断形式

 

if [条件判断式] then   指令 else   指令 fi
if [条件判断式] then   指令 elif [条件判断式] then   指令 else   指令 fi
 

 

 

 

2.4.2 case...esac

esac是case反着写。

:~/test$ cat case_test.sh # !bin/bash case $1 in     "1")         echo "input value 1"         ;;     "2")         echo "input value 2"         ;;     *)         echo "input too large"         ;;    esac :~/test$ ./case_test.sh 1 input value 1 :~/test$ ./case_test.sh 2 input value 2 :~/test$ ./case_test.sh 3 input too large

2.5 函数function

与一般程序语言里的函数类似。

:~/test$ cat function_test.sh # !bin/bashfunction show_pere(){    echo "input value $1"    #函数也可以传输参数,注意,不是shell script的参数}case $1 in    "1")        show_pere 1        #1是函数的输入参数        ;;    "2")        show_pere 2        ;;    *)        echo "input too large"        ;;   esac :~/test$ ./function_test.sh  1 input value 1 :~/test$ ./function_test.sh  2 input value 2 :~/test$ ./function_test.sh  3 input too large

2.6 循环

2.6.1 while

$ cat while_test.sh # !bin/bashwhile [ "$input" != "YES" -a "$input" != "yes" ]  #while后面有空格,[]内部所有组件都有空格间隔do   read -p "Input yes/YES to stop this" input done echo "ok.bye" :~/test$ sh  while_util_test.sh Input yes/YES to stop this:no Input yes/YES to stop this:yes ok.bye

 

2.6.2 until

:~/test$ cat util_test.sh # !bin/bashuntil [ "$input" == "YES" -o "$input" == "yes" ]  #符合条件就不循环了do   read -p "Input yes/YES to stop this" input doneecho "ok.bye" :~/test$ sh util_test.sh Input yes/YES to stop thisno Input yes/YES to stop thisyes ok.bye

 

2.6.3 for

for有两种形式:

for var in var1 var2 var3  #循环时,var依次取var1、var2、var3do    代码段done :~/test$ cat for1_test.sh # !bin/bash for var in $(seq 1 5)  #注意var前面没有$ do     echo "$var" done :~/test$ sh for1_test.sh 1 2 3 4 5

 

for ((初始值;限定值;执行步阶))  #与C语言的几乎一样do    代码段done liuwanpeng@liuwanpeng-virtual-machine:~/test$ cat for2_test.sh # !bin/bash read -p "calculate sum for 1,2,....to your input:" n sum=0 for ((i=1;i<=$n;i=i+1)) do     sum=$(($sum+$i))  #sum前面没有$ done echo "sum=$sum"
:~/test$ sh for2_test.sh calculate sum for 1,2,....to your input:10 sum=55

 

 

2.7 shell script调试

 

sh [-xnv] script.sh -x:边执行,边显示已执行的脚本 -n:不执行,只检查语法 -v:执行前先显示脚本

 

shell script学习到此结束

转载地址:http://jedqz.baihongyu.com/

你可能感兴趣的文章
wxWidgets源码分析(9) - wxString
查看>>
[梁山好汉说IT] 梁山好汉和抢劫银行
查看>>
[源码解析] 消息队列 Kombu 之 基本架构
查看>>
[源码分析] 消息队列 Kombu 之 启动过程
查看>>
wx.NET CLI wrapper for wxWidgets
查看>>
Silverlight for linux 和 DLR(Dynamic Language Runtime)
查看>>
ASP.NET MVC Action Filters
查看>>
Powershell中禁止执行脚本解决办法
查看>>
OO_Unit2 多线程电梯总结
查看>>
git clone 出现fatal: unable to access ‘https://github 错误解决方法
查看>>
04_Mysql配置文件(重要参数)
查看>>
python 加密算法及其相关模块的学习(hashlib,RSA,random,string,math)
查看>>
JavaSE总结
查看>>
Python IO编程
查看>>
CSS入门总结
查看>>
使用 TortoiseGit 时,报 Access denied 错误
查看>>
基于 HTML5 WebGL 的污水处理厂泵站自控系统
查看>>
django-表单之模型表单渲染(六)
查看>>
c++之程序流程控制
查看>>
spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!
查看>>