#!/bin/bash i=0 while [ $i -lt 4 ]; do if echo "in first if, i = $i"; false ; then echo bar else echo "in else, i = $i" i=$((i + 1)) | echo "Will tray again..." # (1) echo "after echo i = $i" i=$((i + 1)) echo "after 2nd echo i = $i" fi ...
echo "The name of this script is \"`basename $0`\"." echo if [ -n "$1" ] # 测试变量被引用. then echo "Parameter #1 is $1" # 需要引用才能够转义"#" fi if [ -n "$2" ] then echo "Parameter #2 is $2" fi if [ -n "${10}" ] # 大于$9的参数必须用{}括起来. then ...
if [ "1" = "0" ];then :fiecho "if 语句返回值: $?"[ "1" = "0" ] && statementecho "&& 语句返回值: $?"if 语句返回值: 0&& 语句返回值: 1 因此,当脚本开启 errexit 或者在写 travis-ci 的 script 部分时要注意一下这方面的差别。
(2) 如果参数为“start”, 则创建空文件/var/lock/subsys/script.sh,并显示“starting script.sh successfully.”; (3) 如果参数为“stop”,则删除空文件/var/lock/subsys/script.sh,并显示“stopping script.sh successfully.”; (4) 如果参数为“restart”,则删除空文件/var/lock/subsys/script.sh,并显示...
if [[ -n $1 ]]; then echo "The non option arguments are:" $@ fi EOF chmod +x /tmp/demo-equals-separated.sh /tmp/demo-equals-separated.sh -e=log -s=/var/log pos3 pos4 其中${i#*=}用于删除参数$i从左边开始匹配的第一个=及其左边的所有字符。
if grep "^$" $fileName &> /dev/null; then #用grep判断变量fileName文件中是否有空白行,如果有就执行下面语句1 linesCount=`grep "^$" $fileName | wc -l` #用wc -l命令读取grep命令判断fileName中有多少空白行 echo "$fileName has $linesCount space lines." ...
if [ $num -lt 0 ]; then echo "Number $num is negative" elif [ $num -gt 0 ]; then echo "Number $num is positive" else echo "Number $num is zero" fi 让我运行它来涵盖这里的所有三种情况: Running a script with bash elif statement ...
[root@localhost ~]# bash ./script 2 3 //其中2 3 为位置参数表示脚本中的$1,$2 put two arg 5 [root@localhost ~]# 位置参数轮替: 格式:shift [n] //一次将n个参数踢出,可以不加n,表示默认一个 如: #!/bin/bash echo " firsh pos argus :$1,$3" ...
/bin/bash># This script is a value test for 1 and 2>#2016-0828 author chawan>#>if[1-lt2];then>echo"2 is bigger">fi>EOF[root@localhost test]# chmod +x if11[root@localhost test]# ./if112is bigger 1. 2. 3. 4. 5. 6....
if [ $[$i%2] -eq 1 ];then continue fi let sum+=$i done echo "sum is $sum" 2.2 break 提前跳出循环 while CONDITION1;do CMD1 if CONDITION2;then break fi done 创建死循环 while true;do 循环体 done 退出方式:某个测试条件满足之后,让循环体执行break命令 ...