for skill in Ada Coffe Action Java; do echo "I am good at ${skill}Script" done # There must be no spaces on both side of the equal sign, otherwise an error will be reported your_name="tom" echo $your_name your_name="alibaba" echo $your_name # Read-only variable myUrl="https://www.google.com" readonly myUrl # Error! variable.sh: line 13: myUrl: readonly variable myUrl="https://www.runoob.com" # unset url unset_variable="https://www.runoob.com" unset myUrl # nothing output echo $myUrl
# Create a simple array my_array=(A B "C" D) # We can aslo use index to define array array_name[0]=value0 array_name[1]=value1 array_name[2]=value2 # Read value from array echo "The fisrt element is: ${my_array[0]}" echo "The second element is: ${my_array[1]}" echo "The third element is: ${my_array[2]}" echo "The forth element is: ${my_array[3]}" # Associative array(which is like dictionary in python) site["google"]="www.google.com" site["runoob"]="www.runoob.com" site["taobao"]="www.taobao.com" echo ${site["runoob"]} # we can get all elements in array using @ or * my_array[0]=A my_array[1]=B my_array[2]=C my_array[3]=D echo "All elements in array are: ${my_array[*]}" echo "All elements in array are: ${my_array[@]}" echo "All elements in site: ${site[*]}" echo "All elements in site: ${site[@]}" echo "All keys are: ${!site[*]}" # Get The element numbers of array echo "The number of array elements: ${#my_array[*]}" echo "The number of array elements: ${#my_array[@]}"
# There must be spaces on both side of '+' # Arithmetic operator val=`expr 2 + 2` echo "The sum of two numers: $val"
a=10 b=20
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 "a / b: $val"
val=`expr $b % $a` echo "a % b: $val"
if [ $a == $b ] then echo "a is equal b" fi
if [ $a != $b ] then echo "a is not equal b" fi # Relational operator a=10 b=20 if [ $a -eq $b ] then echo "$a -eq $b : a is equal b" else echo "$a -eq $b : a is not equal b" fi
if [ $a -ne $b ] then echo "$a -ne $b : a is not equal b" else echo "$a -ne $b : a is equal b" fi
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
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
if [ $a -ge $b ] then echo "$a -ge $b : a is greater than or equal b" else echo "$a -ge $b : a is not greater than or euqal b" fi
if [ $a -le $b ] then echo "$a -le $b : a is less than or euqal b" else echo "$a -le $b : a is not less than or euqal b" fi # Bool operator a=10 b=20 if [ $a != $b ] then echo "$a != $b : a is not equal b" else echo "$a == $b : a is euqal b" fi # -a: and if [ $a -lt 100 -a $b -gt 15] then echo "$a is less than 100 and $b is greater than 15 is valid" else echo "$a is less than 100 and $b is greater than 15 is not valid" fi # -o: or if [ $a -lt 100 -o $b -gt 100 ] then echo "$a is less than 100 or $b is greater than 100 is valid" else echo "$a is less than 100 or $b is greater than 100 is not valid" fi # Logic operator a=10 b=20
if [[ $a -lt 100 && $b -gt 100 ]] then echo "return true" else echo "return false" fi
if [[ $a -lt 100 || $b -gt 100 ]] then echo "return true" else echo "return false" fi # Strign operator a="abc" b="efg" if [ $a = $b ] then echo "$a = $b : a is equal b" else echo "$a = $b : a is not equal b" fi
if [ $a != $b ] then echo "$a != $b : a is not equal b" else echo "$a != $b : a is equal b" fi
if [ -z $a ] then echo "-z $a : the length of string is 0" else echo "-z $a : the length of string is not 0" fi
if [ -n $a ] then echo "-z $a : the length fo string is not 0" else echo "-z $a : the length fo string is 0" fi
if [ $a ] then echo "$a : the string is not null" else echo "$a : the strign is null" fi # file test operator file="/Volumes/HDD/Users/singheart/Project/shell/learn/operator.sh" if [ -r $file ] then echo "file is readable" else echo "file is not readable" fi
if [ -w $file ] then echo "file is writable" else echo "file is not writable" fi
if [ -x $file ] then echo "file is executable" else echo "file is not executable" fi
if [ -d $file ] then echo "file is directory" else echo "file is not directory" fi
if [ -s $file ] then echo "file is not empty" else echo "file is empty" fi
if [ -e $file ] then echo "file exists" else echo "file does not exist" fi
# output normal string echo "It is a test" # The double quotation marks can be omitted echo It is a test # Show escape characters echo "\"It is a test\"" # Show varaible echo "Please input your name:" # read will read one line from standard input" read name echo "your name is $name" # New line echo "OK! \n" echo "It is a test" # Don't output new line echo "OK! \c" echo "It is a test" # Redirect output to a file echo "It is a test" > myfile # single quotation echo '$name\"' # Show the result of command echo `date`
shell printf命令
1 2 3 4
printf "%-10s %-8s %-4s\n" name sex weight\(KG\) printf "%-10s %-8s %-4.2f\n" xoxi man 66.1234 printf "%-10s %-8s %-4.2f\n" singheart man 66.1234 printf "%-10s %-8s %-4.2f\n" jiong woman 66.1234
num1=100 num2=100 if test $[num1] -eq $[num2] then echo "Two numbers are equal" else echo "Two numbers are not equal" fi # []执行基本的算数运算 a=5 b=6 result=$[a+b] echo "result is : $result" # string test num1="ru1noob" num2="runoob" if test $num1 = $num2 then echo "Two strings are equal" else echo "Two strings are not equal" fi # file test cd /bin if test -e ./bash then echo "file exists" else echo "file does not exist" fi
a=10 b=20 # ifelse的[...]判断语句中大于使用-gt,小于使用-lt # 如果使用((...))作为判断语句,大于和小于可以直接使用>和< a=10 b=20 if [ $a == $b ] then echo "a is equal b" elif (( $a > $b )) then echo "a is greater than b" elif [ $a -lt $b ] then echo "a is less than b" else echo "no valid condition" fi # if_else is usually used with test num1=$[2*3] num2=$[1+5] if test $[num1] -eq $[num2] then echo "Two nums are equal" else echo "Two nums are not equal" fi # for loop for loop in 1 2 3 4 5 do echo "The value is : $loop" done
for str in This is a string do echo "value is $str" done # while loop int=1 while (( $int <= 5 )) do echo $int # use bash let command let "int++" done
echo "Press <CTRL-D> exit" echo "input your favorite site:\c" while read FILM do echo "Yes! $FILM is a greate site" echo "input your favorite site:\c" done # condition loop a=0 until [ ! $a -lt 10 ] do echo $a a=`expr $a + 1` done # case...esac echo "input digit between 1 and 4:\c" read aNum case $aNum in 1) echo "you choose 1" ;; 2) echo "you choose 2" ;; 3) echo "you choose 3" ;; 4) echo "you choose 4" ;; *) echo "choosed number are not between 1 and 4" ;; esac
site="runoob" case "$site" in "runoob") echo "菜鸟教程" ;; "google") echo "google search" ;; "taoabo") echo "taobao" ;; esac # break while : do echo "input numer between 1 and 5:\c" read aNum case $aNum in 1|2|3|4|5) echo "input number is $aNum!" ;; *) echo "input number is not between 1 and 5. Gameover!" break; ;; esac done # continue while : do echo "input number between 1 and 5:\c" read aNum case $aNum in 1|2|3|4|5) echo "your input is $aNum!" ;; *) echo "your input is not between 1 and 5" continue echo "game over" ;; esac done
funWithReturn(){ echo "Please input the first number:\c" read aNum echo "Please input the second number:\c" read anotherNum echo "you input $aNum and $anotherNum" return $(($aNum+$anotherNum)) } funWithReturn # use $? to get return value from function echo "The sum is $? !"
fun3(){ echo "the first parameter is $1" echo "the second parameter is $2" echo "the third parameter is $3" echo "the tenth parameter is $10" echo "the tenth parameter is ${10}" echo "total parameter number is $#" echo "作为一个字符串输出所有参数 $* " } fun3 1 2 3 4 5 6 7 8 9 34 73
inline_out_file="inline_out.app" outline_out_file="outline_out.app" # judge whether source file exists if [ ! -e $inline_src_file ] then echo "$inline_src_file does not exist!" fi
if [ ! -e $outline_src_file ] then echo "$outline_src_file does not exists!" fi # judge whether the binary executable file exists. # If it exists, delete them. if [ -e $inline_out_file ] then echo "file $inline_out_file already exists, I will delete it!" rm -rf $inline_out_file fi
if [ -e $outline_out_file ] then echo "file $outline_out_file already exists, I will delete it!" rm -rf $outlie_out_file fi # compile with g++ g++ $inline_src_file -o $inline_out_file g++ $outline_src_file -o $outline_out_file if [ -e $inline_out_file -a -e $outline_out_file ] then echo "$inline_out_file and $outline_out_file have generated" else echo "Fail to generate $inline_out_file and $outline_out_file!" fi
inline_app_size=$(ls -l $inline_out_file | awk '{print $5}') outline_app_size=$(ls -l $outline_out_file | awk {'print $5}') echo $inline_app_size echo $outline_app_size echo "The file size of $inline_out_file generated by $inline_src_file is $inline_app_size bytes." echo "The file size of $outline_out_file generated by $outline_src_file is $outline_app_size bytes." if (( $inline_app_size > $outline_app_size)) then echo "The executable file size generated with inline_function is bigger!" fi