for循环 语法:for 变量名 in 条件; do …; done for循环会以空格作为分隔符 案例1 #!...循环 语法 while 条件; do … ; done 案例1 #!.../bin/bash while : do load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d....-f1` if [ $load -gt 10 ] then top|mail -s "load is high: $load" asldkfls@11.com fi.../bin/bash for i in `seq 1 5` do echo $i if [ $i == 3 ] then exit fi echo
/bin/bash while : //:为死循环的意思 do load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut...-f1` if [ $load -gt 10 ] then top|mail -s "load is high: $load" [email protected] fi sleep 30 done.../bin/bash for i in `seq 1 5` do echo $i if [ $i == 3 ] then break fi echo $i done echo aaa 执行步骤如下.../bin/bash for i in `seq 1 5` do echo $i if [ $i == 3 ] then continue fi echo $i done echo aaa 执行结果如下.../bin/bash for i in `seq 1 5` do echo $i if [ $i == 3 ] then exit fi echo $i done echo aaa 执行步骤如下:
/bin/bash a='/root/shell/look.txt' if [ -f $a ]; then echo "look.txt 存在。"...any other words." exit 1 fi if [ $n -lt 60 ] && [ $n -ge 0 ] then tag=1 elif [ $n -ge 60 ] && [ $n...ls $a fi done 10. while循环 语法 while 条件; do … ; done 10.1 案例1:每隔1分钟检查一下系统负载 每隔1分钟检查一下系统负载,当系统的负载大于10.../bin/bash while true //true 1 : 都代表真 do load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut...then exit fi echo B=$i done echo C=$i [root@linux-01 shell]# sh exit.sh A=1 B=1 A
rm f* 删除文件名中以 f 为字首的所有文件。 9.pwd: 列出当前所在的目录位置 10.cat: 查看文件内容 cat file1 以连续显示方式,查看文件名 file1 的内容。.../bin/bash if [ -f Af.php ];then echo 'file exist' else echo 'file not exist' fi exit 0 执行shell文件 bash...echo '正数' elif [ "$num" -lt 0 ];then #elif [ "$num" -lt 0 ];then echo '负数' else echo '零' fi exit 0...do if [ -f $v ];then echo "FILE: $v" fi done exit 0 计算输出 1-10 之间数字的总和 #!...; elif [ "$val" -eq "10" ];then break; else #输出值 echo "val:$val" #将数值加1 ((val++)) fi done exit 0 结果:
目录 一、语法 二、逻辑运算符解析 三、示例 1、测试数字大小 2、测试目录是否存在 3、多个条件测试判断 四、使用if条件语句编写SQL自动备份脚本 五、if条件综合Shell实战脚本编写 一、语法...if [表达式] 语句1 else 语句2 fi 二、逻辑运算符解析 -f 判断文件是否存在,如:if [-f filename] -d...服务" echo -e "\033[31mUsage:{ /bin/sh $0 1|2|3|4|help}\033[0m" exit fi if[[ "$1" -eq "help" ]];...\033[0m" exit 0 fi fi #install MySQL DB server if[[ "$1" =eq "2" ]];then wget -c $M_URL...\033[0m" exit 0 fi fi #install PHP server if[[ "$1" -eq "3"]];then wget -c $P_URL/$P_FILES
fi # Another alternate formif [ -f .bash_profile ]then echo "You have a .bash_profile....-d $1 ]; then echo "$1 is not a directory!" 1>&2 exit 1fi if [ !...1>&2 exit 1fi # Process each file in directory_1, comparing it to directory_2missing=0for filename...-f $TEMP_FILE exit $1} error_exit() { # Display error message and exit echo "${PROGNAME}:...-f "$1" ]; then error_exit "file $1 cannot be read"fi pr $1 > $TEMP_FILE || error_exit "cannot format
= 'root' ] then echo "ERROR: need to be root so that" exit 1 fi 三、if…else语句 适用范围==:两步判断,条件为真干什么,...source_pkg=nginx-1.19.2.tar.gz #1、软件包下载 wget http://nginx.org/download/$source_pkg #2、解压 if [ -f $source_pkg...];then tar xf $source_pkg && cd nginx-1.19.2 else echo "not found $source_pkg" exit 1 fi...( yum -y install pcre-devel zlib-devel );then echo "yum: install soft error" exit 1 fi #4...exit 1 fi else echo "make fail" exit 1 fi else echo "configure fail" exit
/bin/bash a=5 if [ $a -gt 3 ] #注意[]内的空格 then echo "ok" fi 格式2 if 条件1;then commond1;else commond2;.../bin/bash a=5 if [ $a -gt 3 ] #注意[]内的空格 then echo "ok" else echo "fault" fi 格式3 if 条件1;then commond1.../bin/bash n=`wc -l /tmp/test.txt` if [ $n -gt 20 ] then echo 1 else echo 0 fi 在该脚本中无语法错误,只是我们预设...exit 1 #“exit 1”表示执行该部分命令后的返回值 #即,命令执行完后使用echo $?...exit 1 #判断$n1不为空时(即$n不是纯数字)再次提示用户输入数字并退出 fi 如果用户输入的是纯数字则执行以下命令: if [ $n -lt 60 ] && [ $n -ge 0 ] then
else statement4 .... fi 测试脚本是否有语法错误: bash -n file.sh bash -x 脚本 单步执行命令 编写一个脚本...-e $FILE ];then echo "NO such file" fi if [ -f $FILE ];then echo "common file" elif [ -d $...-e $1 ];then echo "NO such file" fi if [ -f $1 ];then echo "common file" elif [ -d $1 ]...id $1 &> /dev/null;then echo "no user" exit 1 fi if [ `id -n -u $1` == `id -n -g $1` ];...-o $1 == quit -o $1 == Quit ];then echo "quiting" exit 1 fi let i=$[$i+1] let sum=
/bin/bash 执行脚本的shell #auto mail for system info 描述 /bin/date +%F >> /tmp/sysinfo 获取日期 echo "disk...-d $1 then …… fi test -d $1 ==== [-d $1] #example3 #检测web服务是否启动。 .../bin/bash if [$# -ne 2 ]; then echo "Not enough parameters" exit 0 fi .../bin/bash until [ -x /etc/inittab ] do /bin/ls -l /etc/inittab exit 0 done #example2...###sh -n script 不执行脚本只是检查语法模式,将返回所有语法错误。
break elif [ -f $file1 ] && [ -f $file2 ] then mv $file1 tempfile mv $file2 $file1 mv tempfile...exit fi if [ -d $2/ ] then echo "you choose the dir is $2" cd $2 wget $1 else echo "we didn't found...exit fi pc=`echo "$1" | sed 's/[0-9d]//g' |wc -m` if [ $pc -ne "1" ] then echo "pls just input a number...fi ##check if just input a one PARM if [ $# -eq "1" ] then if cat /etc/passwd | awk -F ':' '{print...exit 1 fi if [ -f "$1" ]; then vim "$1" exit 2 fi touch "$1" echo "#!
-nt f2 文件f1比文件f2新则为真, 根据文件的修改时间计算 f1 -ot f2 文件f1比文件f2旧则为真, 根据文件的修改时间计算 文件测试 [ 操作符 文件或目录 ] [root@...exit fi echo ccc # 案例2: [root@qfedu.com ~]# cat test03.sh # !...单分支 IF 条件语句 1、语法格式 if [ 条件判断式 ];then 条件成立时, 执行的程序 fi # if语句使用fi结尾和一般语言使用大括号结尾不同 # [条件判断式] 就是使用test...>> /~/restart.log fi 3、多分支语句 1、语法格式 if [ 条件判断式1 ] then 当条件判断式1成立时, 执行程序1 elif [ 条件判断式2 ] then...;; *) echo "Usage:$0{start|stop|restart}" exit 1 esac exit $retval 二、Shell 编程之循环结构 1、Shell 循环 for 语句
/bin/bash linecount="$(wc -l $1|cut -d' ' -f1)" echo "This file have ${linecount} lines" 数组 语法格式 语法格式...$address 目前不在线" fi done while循环 语法 while 测试条件;do 循环体 done 经典使用 #!...-d $DIR1 ];then # 判断用户输入的路径是否正确,错误则直接退出程序 echo "没有 $DIR1 这个目录,程序退出" exit fi count_1=`find $DIR1...-d $DIR2 ];then # 判断用户输入的路径是否正确,错误则直接退出程序 echo "没有 $DIR2 这个目录,程序退出" exit fi count_2=`find $DIR2...else echo "$DIR2 中的每个文件都与 $DIR1 中相同" fi rm -f /root/temp_{..}
[失败] 函数语法案例2 一、函数传参测试网址异常 1. 基本实现 1) 代码 cat /server/scripts/b5.sh #!.../bin/bash function usage () { echo "usage:$0 input one url" exit 1 } #错误提示函数 function check_url (...fi } #url测试函数,注意$1 function main () { if [ $# -ne 1 ] then usage fi check_url $1 } #主函数...改为命令行传参 1) 脚本 cat /server/scripts/b5.sh #!/bin/bash [ -f /etc/init.d/functions ] && ..../etc/init.d/functions || exit 1 function usage () { echo "usage:$0 input one url" exit 1 } function
"$BASH_ENV"; fi 2.4. 以 sh 命令启动 如果以 sh 命令启动 bash,bash 将模拟 sh 的行为。 如果作为交互登录 shell 启动,则会依次执行: 1....分支控制 — if、then、elif、else、fi 和 C 语言类似,在 Shell 中使用 if、then、elif、else、fi 几个命令实现分支控制,例如: if [ -f ~/.bashrc...exit 1 fi exit 0 13.3. &&、|| 与C语言类似,Shell 也提供 && 与 || test "$VAR" -gt 1 -a "$VAR" -lt 3 等价于。...-ne 0 ]; then echo "Cannot create directory $DIR" exit 1 fi fi done...exit 1 set +x fi 19.
语法如下 : if [ 条件判断式 ] ; then 程序 fi 单分之条件语句需要注意几个点 : if语句使用fi结尾,和一般语言使用大括号结尾不同 [ 条件表达式 ] 就是使用 test 命令判断.../bin/bash read -p "Please input a filename: " filename if [ -z $filename ] then echo "11111" exit...-e $filename ] then echo "2222222" exit 102 elif [ -f $filename ] then echo "$filename is putong...exit 11 #并退出程序,返回错误代码 11 fi else echo "qing shuru neirong" exit 12 fi echo "$num1 $ope $num2 :...for的语法有如下两种 : 语法一 : for 变量 in 值1 值2 值3 … do 程序 done 这种语法中 for 循环的次数,取决于in后面值的个数(空格分隔),有几个值就循环几次
/bin/bash 语法缩进:使用四个空格;多加注释说明 命名建议规则:变量名大写、局部变量小写,函数名小写,名字体现出实际作用 默认变量是全局的:在函数中变量local指定为局部变量,避免污染其他作用域...exit 1 fi US=$(vmstat |awk NR==3{print $13} ) SY=$(vmstat |awk NR==3{print $14} ) IDLE=$(vmstat |awk...exit 0 fi ;; stop) if [ -f $PID ]; then stop else...exit 0 fi ;; restart) if [ !...exit 1 fi SUM=$(($SUM+$INT)) [ $MIN -lt $INT ] && MIN=$INT [ $MAX -gt $INT ] && MAX=$
任何语言的语法规范至关重要,特别是在团队协作过程中。 以下以php的laravel框架为主,通过git的pre-commit钩子(hooks)来实现php语法规范检测。...1 fi # stolen from template file if git rev-parse --verify HEAD then against=HEAD else...[ "$FILES_TO_CHECK" == "" ]; then exit 0 fi # execute the code sniffer if [ "$PHPCS_IGNORE"..." == "1" ]; then IGNORE_WARNINGS="-n" else IGNORE_WARNINGS="" fi # Copy contents of...exit $RETVAL
case语法案例 制作nginx启停脚本 1.条件: 启动服务命令/application/nginx/sbin/nginx 停止服务命令/application/nginx/sbin/nginx -s.../bin/bash ....1 fi if [ $# -ne 2 ] then usage exit 2 fi case "$1" in -add|-a) shift #位置参数左移,$2替换$1...-eq 0 ] && action $"DEL $1" /bin/true chattr +i ${FILE_PATH} exit fi ;;...exit fi ;; *) usege exit ;; esac 注意本例中用到的grep三种精确过滤方法 grep -w "oldboy" /file
1. 知识点介绍 前面文章介绍了Shell脚本的变量、运算符、条件语句、循环语句的使用,这篇文章接着介绍Shell脚本里数组、函数、字符串处理相关语法。...后面列出了一些练习题,结合前面的介绍的基本语法知识做一些练习,巩固Shell的基本语法使用。 2. 数组的定义与使用 #!.../xxx.sh /work\n" exit fi #判断传入的参数是否是目录 if [ -d $1 ] then printf "工作目录:%s\n" $1 cd $1 #.../xxx.sh /work\n" exit fi max=0 file_name="" #扫描目录 for file in * do #判断是否是文件 if [ -f $file...${i}" fi i=`expr $i + 1` done
领取专属 10元无门槛券
手把手带您无忧上云