标签:direct 分割 extension while default 转义字符 shell命令 提示 qbc
#!/bin/bash myUrl="http://see.xidian.edu.cn/cpp/shell/" readonly myUrl myUrl="http://see.xidian.edu.cn/cpp/danpianji/"
#!/bin/bash myurl="http://www.baidu.com" echo ${myurl}
#!/bin/bash a=10 echo -e "Value of a is $a \n"
#!/bin/bash DATE=`date` echo "Date is $DATE" USERS=`who | wc -l` echo "Logged in user are $USERS" UP=`date ; uptime` echo "Uptime is $UP"
$ ./test.sh Date is 2017年04月12日 10:20:51 Logged in user are 0 ./test.sh: line 6: uptime: command not found Uptime is 2017年04月12日 10:20:51
#!/bin/bash echo ${var:-"Variable is not set"} echo "1 - Value of var is ${var}" echo ${var:="Variable is not set"} echo "2 - Value of var is ${var}" unset var echo ${var:+"This is default value"} echo "3 - Value of var is $var" var="Prefix" echo ${var:+"This is default value"} echo "4 - Value of var is $var" echo ${var:?"Print this message"} echo "5 - Value of var is ${var}" 执行结果: $ ./test.sh Variable is not set 1 - Value of var is Variable is not set 2 - Value of var is Variable is not set 3 - Value of var is This is default value 4 - Value of var is Prefix Prefix 5 - Value of var is Prefix
#!/bin/sh a=101 b=225 #加 val=`expr $a + $b` echo "a + b : $val" #减 val=`expr $a - $b` echo "a - b : $val" #程 val=`expr $a \* $b` echo "a * b : $val" #除 val=`expr $b / $a` echo "b / a : $val" #求余 val=`expr $b % $a` echo "b % a : $val" if [ $a == $b ] then echo "a is equal to b" fi if [ $a != $b ] then echo "a is not equal to b" fi
$ ./test.sh a + b : 326 a - b : -124 a * b : 22725 b / a : 2 b % a : 23 a is not equal to b
#!/bin/sh a=10 b=20 #检测两个数是否相等,相等返回 true。 if [ $a -eq $b ] then echo "$a -eq $b : a is equal to b" else echo "$a -eq $b: a is not equal to b" fi #检测两个数是否相等,不相等返回 true。 if [ $a -ne $b ] then echo "$a -ne $b: a is not equal to b" else echo "$a -ne $b : a is equal to b" fi #检测左边的数是否大于右边的,如果是,则返回 true。 if [ $a -gt $b ] then echo "$a -gt $b: a is greater than b" else echo "$a -gt $b: a is not greater than b" fi #检测左边的数是否小于右边的,如果是,则返回 true。 if [ $a -lt $b ] then echo "$a -lt $b: a is less than b" else echo "$a -lt $b: a is not less than b" fi #检测左边的数是否大等于右边的,如果是,则返回 true。 if [ $a -ge $b ] then echo "$a -ge $b: a is greater or equal to b" else echo "$a -ge $b: a is not greater or equal to b" fi #检测左边的数是否小于等于右边的,如果是,则返回 true。 if [ $a -le $b ] then echo "$a -le $b: a is less or equal to b" else echo "$a -le $b: a is not less or equal to b" fi
Administrator@XY9A6SI917CWQBC MINGW32 /h $ ./test.sh 10 -eq 20: a is not equal to b 10 -ne 20: a is not equal to b 10 -gt 20: a is not greater than b 10 -lt 20: a is less than b 10 -ge 20: a is not greater or equal to b 10 -le 20: a is less or equal to b
#!/bin/sh a=10 b=20 #非运算,表达式为 true 则返回 false,否则返回 true if [ $a != $b ] then echo "$a != $b : a is not equal to b" else echo "$a != $b: a is equal to b" fi #与运算,两个表达式都为 true 才返回 true。 if [ $a -lt 100 -a $b -gt 15 ] then echo "$a -lt 100 -a $b -gt 15 : returns true" else echo "$a -lt 100 -a $b -gt 15 : returns false" fi #或运算,有一个表达式为 true 则返回 true。 if [ $a -lt 100 -o $b -gt 100 ] then echo "$a -lt 100 -o $b -gt 100 : returns true" else echo "$a -lt 100 -o $b -gt 100 : returns false" fi #或运算,有一个表达式为 true 则返回 true。 if [ $a -lt 5 -o $b -gt 100 ] then echo "$a -lt 100 -o $b -gt 100 : returns true" else echo "$a -lt 100 -o $b -gt 100 : returns false" fi
Administrator@XY9A6SI917CWQBC MINGW32 /h $ ./test.sh 10 != 20 : a is not equal to b 10 -lt 100 -a 20 -gt 15 : returns true 10 -lt 100 -o 20 -gt 100 : returns true 10 -lt 100 -o 20 -gt 100 : returns false
#!/bin/sh a="abc" b="efg" #= 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。 if [ $a = $b ] then echo "$a = $b : a is equal to b" else echo "$a = $b: a is not equal to b" fi #!= 检测两个字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。 if [ $a != $b ] then echo "$a != $b : a is not equal to b" else echo "$a != $b: a is equal to b" fi #-z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。 if [ -z $a ] then echo "-z $a : string length is zero" else echo "-z $a : string length is not zero" fi #-n 检测字符串长度是否为0,不为0返回 true。 [ -n $a ] 返回 true。 if [ -n $a ] then echo "-n $a : string length is not zero" else echo "-n $a : string length is zero" fi #str 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true。 if [ $a ] then echo "$a : string is not empty" else echo "$a : string is empty" fi
Administrator@XY9A6SI917CWQBC MINGW32 /h $ ./test.sh abc = efg: a is not equal to b abc != efg : a is not equal to b -z abc : string length is not zero -n abc : string length is not zero abc : string is not empty
文件测试运算符
代码示例: #!/bin/sh file="H:/test.sh" #-r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。 if [ -r $file ] then echo "File has read access" else echo "File does not have read access" fi #-w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。 if [ -w $file ] then echo "File has write permission" else echo "File does not have write permission" fi #-x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。 if [ -x $file ] then echo "File has execute permission" else echo "File does not have execute permission" fi #-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。 if [ -f $file ] then echo "File is an ordinary file" else echo "This is sepcial file" fi #-d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。 if [ -d $file ] then echo "File is a directory" else echo "This is not a directory" fi #-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。 if [ -s $file ] then echo "File size is zero" else echo "File size is not zero" fi #-e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。 if [ -e $file ] then echo "File exists" else echo "File does not exist" fi
结果:
Administrator@XY9A6SI917CWQBC MINGW32 /h $ ./test.sh File has read access File has write permission File has execute permission File is an ordinary file This is not a directory File size is zero File exists
str=‘this is a string‘
单引号字符串的限制:
your_name=‘qinjx‘ str="Hello, I know your are \"$your_name\"! \n"
双引号的优点:
your_name="qinjx" greeting="hello, "$your_name" !" greeting_1="hello, ${your_name} !" echo $greeting $greeting_1
$ ./test.sh hello, qinjx ! hello, qinjx !
获取字符串长度
string="abcd" echo ${#string} #输出 4
结果:
$ ./test.sh 4
string="alibaba is a great company" echo ${string:1:4} #输出liba
$ ./test.sh liba
string="alibaba is a great company" echo `expr index "$string" is`
$ ./test.sh 3
array_name=(value1 ... valuen)
array_name=(value0 value1 value2 value3)
array_name=(
value0
value1
value2
value3
)
array_name[0]=value0 array_name[1]=value1 array_name[2]=value2
${array_name[index]} valuen=${array_name[2]}
#!/bin/sh NAME[0]="张三" NAME[1]="李四" NAME[2]="王麻子" NAME[3]="赵六" NAME[4]="程起" echo "First Index: ${NAME[0]}" echo "Second Index: ${NAME[1]}"
$ ./test.sh First Index: 张三 Second Index: 李四
#!/bin/sh NAME[0]="张三" NAME[1]="李四" NAME[2]="王麻子" NAME[3]="赵六" NAME[4]="程起" echo "First Index: ${NAME[0]}" echo "Second Index: ${NAME[1]}" #使用@ 或 * 可以获取数组中的所有元素 echo "All : ${NAME[@]}" echo "All : ${NAME[*]}"
$ ./test.sh First Index: 张三 Second Index: 李四 All : 张三 李四 王麻子 赵六 程起 All : 张三 李四 王麻子 赵六 程起
# 取得数组元素的个数 length=${#array_name[@]} # 或者 length=${#array_name[*]} # 取得数组单个元素的长度 lengthn=${#array_name[n]}
例如:
#!/bin/sh NAME[0]="张三" NAME[1]="李四" NAME[2]="王麻子" NAME[3]="赵六" NAME[4]="程起" echo "First Index: ${NAME[0]}" echo "Second Index: ${NAME[1]}" #使用@ 或 * 可以获取数组中的所有元素 echo "All : ${NAME[@]}" echo "All : ${NAME[*]}" echo "${#NAME[@]}" echo "${#NAME[*]}" echo "${#NAME[n]}"
结果:
$ ./test.sh First Index: 张三 Second Index: 李四 All : 张三 李四 王麻子 赵六 程起 All : 张三 李四 王麻子 赵六 程起 5 5 2
echo arg
echo "\"It is a test\""
"It is a test"
name="OK" echo "$name It is a test"
OK It is a test
mouth=8 echo "${mouth}-1-2009"
结果将是:
8-1-2009
echo "OK!\n" echo "It is a test"
$ ./test.sh OK! It is a test
echo "OK!\c" echo "It is a test"
OK!It si a test
echo "It is a test" > myfile
$ ./test.sh
echo ‘$name\"‘
echo `date`
$ ./test.sh 2017年04月13日 14:00:47
if ... fi 语句; if ... else ... fi 语句; if ... elif ... else ... fi 语句。 if else语法: if [条件] then 条件为true执行语句 fi
#!/bin/sh a=88 b=105 if [ $a == $b ] then echo "a is equal to b" fi if [ $a != $b ] then echo "a is not equal to b" fi
$ ./test.sh a is not equal to b
if [ 条件 ] then 条件为true else 条件为false fi
demo:
#!/bin/sh a=88 b=105 if [ $a == $b ] then echo "a is equal to b" else echo "a is not equal to b" fi
结果:
$ ./test.sh a is not equal to b
if [ 条件 1 ] then 条件1成立 elif [ 条件 2 ] then 条件2成立 elif [ 条件 3 ] then 条件3成立 else 条件1,2,3都不成立的情况下执行 fi
#!/bin/sh a=88 b=105 if [ $a == $b ] then echo "a 等于 b" elif [ $a -gt $b ] then echo "a 大于 b" elif [ $a -lt $b ] then echo "a 小于 b" else echo "上面条件都不满足" fi
$ ./test.sh a 小于 b
还有两种方式:
#!/bin/sh if test $[3*3] -eq $[4+5]; then echo ‘The two numbers are equal!‘; fi;
结果:
$ ./test.sh The two numbers are equal!
#!/bin/sh num1=$[3*3] num2=$[4+5] if test $[num1] -eq $[num2] then echo ‘The two numbers are equal!‘ else echo ‘The two numbers are not equal!‘ fi
$ ./test.sh The two numbers are equal!
case 要判断的内容 in 值1) 要判断的内容等于值1 ;; 模式2) 要判断的内容等于值2 ;; *) 不等于值1,也不等于值2 ;; esac
demo:
#!/bin/bash echo "Input a number between 1 to 4" echo -e "Your number is:\c" read aNum case ${aNum} in 1) echo "You select 1" ;; 2) echo "You select 2" ;; 3) echo "You select 3" ;; 4) echo "You select 4" ;; *) echo "You do not select a number between 1 to 4" ;; esac
$ ./test.sh Input a number between 1 to 4 Your number is:2 You select 2
#!/bin/bash for loop in 1 2 3 4 5 do echo "The value is: $loop" done
$ ./test.sh The value is: 1 The value is: 2 The value is: 3 The value is: 4 The value is: 5
while 条件 do 满足条件,循环语句 done
#! /bin/bash declare -i COUNTER=0 while [ $COUNTER -lt 5 ] do COUNTER=`expr $COUNTER+1` echo $COUNTER done
COUNTER小于5不断循环直到大于等于5才退出
$ ./test.sh 1 2 3 4 5
until 条件为false do 条件为false执行循环语句,否则跳出循环 done
#!/bin/bash a=0 until [ ! $a -lt 10 ] do echo $a a=`expr $a + 1` done
$ ./test.sh 0 1 2 3 4 5 6 7 8 9
#!/bin/bash a=0 while [ $a -lt 1 ] do echo -n "Input a number between 1 to 5: " read aNum case $aNum in 1|2|3|4|5) echo "Your number is $aNum!" ;; *) echo "You do not select a number between 1 to 5, game is over!" break ;; esac done
$ ./test.sh Input a number between 1 to 5: 1 Your number is 1! Input a number between 1 to 5: 2 Your number is 2! Input a number between 1 to 5: 3 Your number is 3! Input a number between 1 to 5: 4 Your number is 4! Input a number between 1 to 5: 5 Your number is 5! Input a number between 1 to 5: 6 You do not select a number between 1 to 5, game is over!
demo2:
#!/bin/bash for var1 in 1 2 3 do for var2 in 0 5 do if [ $var1 -eq 2 -a $var2 -eq 0 ] then break 2 else echo "$var1 $var2" fi done
#!/bin/bash while : do echo -n "Input a number between 1 to 5: " read aNum case $aNum in 1|2|3|4|5) echo "Your number is $aNum!" ;; *) echo "You do not select a number between 1 to 5!" continue echo "Game is over!" ;; esac done
$ ./test.sh Input a number between 1 to 5: 1 Your number is 1! Input a number between 1 to 5: 2 Your number is 2! Input a number between 1 to 5: 3 Your number is 3! Input a number between 1 to 5: 4 Your number is 4! Input a number between 1 to 5: 5 Your number is 5! Input a number between 1 to 5: 6 You do not select a number between 1 to 5! Input a number between 1 to 5: 7 You do not select a number between 1 to 5! Input a number between 1 to 5:
#!/bin/bash # Define your function here Hello () { echo "Url is http://see.xidian.edu.cn/cpp/shell/" } # Invoke your function Hello
结果:
$ ./test.sh Url is http://see.xidian.edu.cn/cpp/shell/
有返回值的函数:
#!/bin/bash funWithReturn(){ echo "该方法获取输入内容总和" echo -n "值1:" read aNum echo -n "值2:" read anotherNum return $(($aNum+$anotherNum)) } funWithReturn # 调用并返回的值 ret=$? echo "两数之和是 $ret !"
$ ./test.sh 该方法获取输入内容?和 值 ?1?2?值 ?
函数嵌套:
#!/bin/bash # Calling one function from another number_one () { echo "Url_1 is http://c.biancheng.net/cpp/view/2491.html" number_two } number_two () { echo "Url_2 is http://192.168.30.7:8090/pages/viewpage.action?pageId=12387047" } number_one
$ ./test.sh Url_1 is http://c.biancheng.net/cpp/view/2491.html Url_2 is http://192.168.30.7:8090/pages/viewpage.action?pageId=12387047
#!/bin/bash . ./test2.sh echo $url
url="https://juejin.im/timeline/android"
执行:
$ ./test.sh https://juejin.im/timeline/android
$ file Usage: file [-bcEhikLlNnprsvzZ0] [--apple] [--extension] [--mime-encoding] [--mime-type] [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ... file -C [-m magicfiles] file [--help]
cat user
标签:direct 分割 extension while default 转义字符 shell命令 提示 qbc
原文地址:http://www.cnblogs.com/androidsuperman/p/6709712.html