文章目录
1 标头
#!/bin/sh
指明了脚本中命令的解释器
2 执行脚本文件
./*.sh(需要X执行权限chmod u+x *.sh)
source *.sh
sh *.sh(不需要X执行权限)
sh -x *.sh(查看sh执行过程)
#echo语句输出字符串echo HelloWorld
#创建变量x=666y=777#expr命令计算给定表达式并显示其相应的输出expr$x"+"$y#遍历多个值forinfoin Wang Long Li Zhang
doecho"welcome $info"done
3 数组
array=("Wang""Zhao""Xiao""Min")#输出数组中的单个元素echo${array[0]}echo${array[1]}#输出数组中的所有元素echo${array[@]}#输出数组大小echo"array size: ""${#array[@]}"#输出数组区间的元素echo${array[@]:1:3}
4 传递参数
echo"Shell pass values during execution:"echo"executor: $1"echo"The first parameter: $2"echo"The second parameter: $3"echo"The third parameter: $4"
5 运算符 && 分支语句
a=10b=20if[$a==$b];thenecho"a equals b"elif[$a -gt $b];thenecho"a greater than b"elif[$a -lt $b];thenecho"a less than b"elseecho"There is no match"fi
! 非运算
-o 或运算
-a 与运算
&& 逻辑AND
|| 逻辑OR
-eq 相等判断
-ne 不相等判断
-gt 大于(>)判断
-lt 小于(<)判断
-ge 大于等于(>=)判断
-le 大于等于(<=)判断
= 字符串是否相等
!= 字符串是否不相等
-z 字符串长度是否为 0
-n 字符串长度是否不为 0
if [ $a ] 字符串判空
6 循环语句
6.1 for循环
echo"for statement:"for(( i=1;i<=10;i++));doecho$(expr $i "*" $i "+"1);done
6.2 while循环
echo"while statement:"i=1while(( $i<=5))doecho$ilet"i++"done
6.3 until循环
echo"until statement:"a=0until[!$a -lt 10]doecho$alet"a++"done
6.4 case语句 && read输入
echo'Enter a number between 1 and 4, you entered:'read Num
case$Numin1)echo'you choose 1';;2)echo'you choose 2';;3)echo'you choose 3';;4)echo'you choose 4';;
*)echo'you did not enter a number between 1 and 4';;esac
6.5 循环控制
continue代表停止当前循环,进入下次循环。
break代表终止循环。
7 函数
demoFun(){echo"This is my first Shell Function!"}echo"-----Function start-----"
demoFun
echo"-----Function end-----"
8 文件、文件夹、字符串判断 && 示例
#!/bin/shpath="build"if[ -d $path];thenecho"clean old cache ($path) ..."rm -r $pathfiforfilein ./*.sa525m.update;doif[ -f $file];thenfilename=${file##*/}echo"clean old output ($filename) ..."rm$filefibreak;doneforfilein ./*exe;doif[ -f $file];thenfilename=${file##*/}echo"clean old output ($filename) ..."rm$filefibreak;doneif[[$1="clean"]];thenexit0fimkdir$path&&cd$path
cmake ..makecd..# file=$(find . -type f -name "*.sa525m.update")file=$(find. -type f \( -name "*.sa525m.update" -o -name "*_exe"\))#同时查找两种if[ -n "$file"];thenfilename=${file##*/}echo"make success!"echo"output: $filename"filetype=$(file"$filename")echo"$filetype"#输出文件类型elseecho"make failed!"fi
#!/bin/shfolder=$(find. -type d -name "_build_*")if[ -n "$folder"];thenfoldername=${folder##*/}echo"clean old cache ($foldername) ..."rm -r $folderfiforfilein ./*.sa525m.update;doif[ -f $file];thenfilename=${file##*/}echo"clean old output ($filename) ..."rm$filefibreak;doneif[[$1="clean"]];thenexit0fidefine=$(find. -type f -name "*.adef")if[ -n "$define"];thendefinename=${define##*/}echo"mkapp $definename ..."
mkapp -t sa525m $definename -C "-O2" -X "-O2"elseecho"Not found *.def to mkapp!"exit1fifile=$(find. -type f -name "*.sa525m.update")if[ -n "$file"];thenfilename=${file##*/}echo -e "mkapp success!\r\noutput: $filename"filetype=$(file"$filename")echo"$filetype"#输出文件类型elseecho"mkapp failed"fi
if [ -e filename ] ; then
-e :判断文件或目录是否存在
-d :判断是不是目录,并是否存在
-f :判断是否是普通文件,并存在
-r :判断文档是否有读权限
-w :判断是否有写权限
-x :判断是否可执行
-n:判断一个变量是否有值
#从头部开始匹配删除 删除第一个 / 前的所有内容
${variable#/}
#从头部开始匹配删除 删除最后一个 / 前的所有内容
${variable##/}
#从尾部开始匹配删除 删除第一个 / 前的所有内容
${variable%/}
#从尾部开始匹配删除 删除最后一个 / 前的所有内容
${variable%%/}
版权归原作者 执行x 所有, 如有侵权,请联系我们删除。