
shell及脚本4——shell script
发布日期:2021-05-19 03:27:45
浏览次数:10
分类:博客文章
本文共 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学习到此结束
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年04月20日 03时32分59秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
判断远程文件是否存在
2019-03-16
升级java11后,maven命令打包报错
2019-03-16
JAVA入门[4]-IntelliJ IDEA配置Tomcat
2019-03-16
springboot redis key乱码
2019-03-16
Win10禁用自带的笔记本键盘
2019-03-16
insmod模块的几种常见错误
2019-03-16
shell及脚本4——shell script
2019-03-16
写时复制集合 —— CopyOnWriteArrayList
2019-03-16
什么是redis的缓存雪崩, 穿透, 击穿?
2019-03-16
数据帧CRC32校验算法实现
2019-03-16
【转载】DSP基础--定点小数运算
2019-03-16
idea thymeleaf页面变量报错解决
2019-03-16
云游戏,打响5G第一战
2019-03-16
Docker 拉取镜像速度太慢
2019-03-16
关于window匿名通道的使用以及所遇到的问题
2019-03-16
逆向工程初步160个crackme-------3
2019-03-16
WM_PAINT 与 WM_ERASEBKGND消息的深入分析
2019-03-16
初探MFC
2019-03-16
代码段段间跳转流程
2019-03-16
HUAWEI防火墙通过IKE方式协商IPSec隧道(采用预共享密钥认证)
2019-03-16