commit 847f5e7495fafa68ccff2d010350e2b3f0f52969 Author: unknown Date: Fri May 13 21:42:54 2016 +0800 2016-05-13 diff --git a/基本脚本/exit命令.sh b/基本脚本/exit命令.sh new file mode 100644 index 0000000..2eafe82 --- /dev/null +++ b/基本脚本/exit命令.sh @@ -0,0 +1,9 @@ +#!/bin/bash +#退出状态码,最大为255,超过则进行模运算 +#testing the exit status +var1=10 +var2=20 +var3=$[ $var1 + $var2] +echo The answer is $var3 +exit 5 + diff --git a/基本脚本/使用expr执行数学运算.sh b/基本脚本/使用expr执行数学运算.sh new file mode 100644 index 0000000..3d6c8f2 --- /dev/null +++ b/基本脚本/使用expr执行数学运算.sh @@ -0,0 +1,8 @@ +#!/bin/bash +#An example of using the expr command + +var1=10 +var2=20 +var3=`expr $var2 / $var1` +echo "The result is $var3" + diff --git a/基本脚本/使用内联重定向计算表达式.sh b/基本脚本/使用内联重定向计算表达式.sh new file mode 100644 index 0000000..cd5b01c --- /dev/null +++ b/基本脚本/使用内联重定向计算表达式.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +var1=10.45 +var2=43.67 +var3=33.2 +var4=71 + +var5=`bc < log.$today diff --git a/处理用户输入/从文件中读取数据.sh b/处理用户输入/从文件中读取数据.sh new file mode 100644 index 0000000..015c852 --- /dev/null +++ b/处理用户输入/从文件中读取数据.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# reading data from a file + +count=1 +cat test | while read line +do + echo "Line $count: $line" + count=$[ $count + 1 ] +done +echo "Finished processing the file" + diff --git a/处理用户输入/使用getopts.sh b/处理用户输入/使用getopts.sh new file mode 100644 index 0000000..6338939 --- /dev/null +++ b/处理用户输入/使用getopts.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# simple demonstration of the getopts command + +while getopts :ab:c opt +do + case "$opt" in + a) echo "Found the -a option";; + b) echo "Found the -b option, with value $OPTARG";; + c) echo "Found the -c option";; + *) echo "Unknown option:$opt";; + esac +done diff --git a/处理用户输入/使用getopts处理选项和参数.sh b/处理用户输入/使用getopts处理选项和参数.sh new file mode 100644 index 0000000..732ac69 --- /dev/null +++ b/处理用户输入/使用getopts处理选项和参数.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# processing options and parameters with getopts + +while getopts :ab:cd opt +do + case "$opt" in + a) echo "Found the -a option";; + b) echo "Found the -b option,with value $OPTARG";; + c) echo "Found the -c option";; + d) echo "Found the -d option";; + *) echo "Unknown option: $opt";; + esac +done +shift $[ $OPTIND - 1 ] +count=1 +for param in "$@" +do + echo "Parameter $count: $param" + count=$[ $count + 1 ] +done diff --git a/处理用户输入/使用getopt命令.sh b/处理用户输入/使用getopt命令.sh new file mode 100644 index 0000000..55c9867 --- /dev/null +++ b/处理用户输入/使用getopt命令.sh @@ -0,0 +1,25 @@ +#!/bin/bash +#extracting command line options and values with getopt +# getopt command is not goot at dealing with space,we can use getopts +set -- `getopt -q ab:c "$@"` +while [ -n "$1" ] +do + case "$1" in + -a) echo "Found the -a option";; + -b) param="$2" + echo "Found the -b option,with parameter value $param" + shift;; + -c) echo "Found the -c option";; + --) shift + break;; + *) echo "$1 is not an option";; + esac + shift +done + +count=1 +for param in "$@" +do + echo "Parameter #$count: $param" + count=$[ $count+1 ] +done diff --git a/处理用户输入/使用shift命令.sh b/处理用户输入/使用shift命令.sh new file mode 100644 index 0000000..11877cf --- /dev/null +++ b/处理用户输入/使用shift命令.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# shift n 移动变量 + +count=1 +while [ -n "$1" ] +do + echo "Parameter #$count = $1" + count=$[ $count+1 ] + shift +done + +echo -e "\n" + +# demonstrating a multi-position shift +echo "The original parameters : $*" +shift 2 +echo "Here's the new first parameter: $1" diff --git a/处理用户输入/分离参数和选项.sh b/处理用户输入/分离参数和选项.sh new file mode 100644 index 0000000..ee3da94 --- /dev/null +++ b/处理用户输入/分离参数和选项.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +#extracting options and parameters + +while [ -n "$1" ] +do + case "$1" in + -a) echo "Found the -a option";; + -b) echo "Found the -b option";; + -c) echo "Found the -c option";; + --) shift + break;; + *) echo "$1 is not an option";; + esac + shift +done + +count=1 +for param in $@ +do + echo "Parameter #$count: $param" + count=$[ $count + 1 ] +done diff --git a/处理用户输入/参数计数.sh b/处理用户输入/参数计数.sh new file mode 100644 index 0000000..65673e8 --- /dev/null +++ b/处理用户输入/参数计数.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# getting the number of parameters + +echo There were $# parameters supplied + +#花括号里不能使用美元符号 +params=$# + +echo The last parameter is $params +echo The last parameter is ${!#} diff --git a/处理用户输入/处理带值的选项.sh b/处理用户输入/处理带值的选项.sh new file mode 100644 index 0000000..b8cc631 --- /dev/null +++ b/处理用户输入/处理带值的选项.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# extracting command line options and values + +while [ -n "$1" ] +do + case "$1" in + -a) echo "Found the -a option";; + -b) param="$2" + echo "Found the -b option, with parameter value $param" + shift;; + -c) echo "Found the -c option";; + --) shift + break;; + *) echo "$1 is not an option";; + esac + shift +done + +count=1 +for param in "$@" +do + echo "Parameter #$count : $param" + count=$[ $count + 1 ] +done diff --git a/处理用户输入/处理简单选项.sh b/处理用户输入/处理简单选项.sh new file mode 100644 index 0000000..60dd404 --- /dev/null +++ b/处理用户输入/处理简单选项.sh @@ -0,0 +1,13 @@ +#!/bin/bash +# extracting command line options as parameters + +while [ -n "$1" ] +do + case "$1" in + -a) echo "Found the -a option";; + -b) echo "Found the -b optins";; + -c) echo "Found the -c optins";; + *) echo "$1 is not a valid options";; + esac + shift +done diff --git a/处理用户输入/抓取所有数据.sh b/处理用户输入/抓取所有数据.sh new file mode 100644 index 0000000..a93ccf0 --- /dev/null +++ b/处理用户输入/抓取所有数据.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# testing $* and $@ + +count=1 +for param in "$*" +do + echo "\$* Parameter #$count = $param" + count=$[ $count+1 ] +done + +count=1 +for param in "$@" +do + echo "\$@ Paramenter #$count = $param" + count=$[ $count+1 ] +done diff --git a/处理用户输入/获取用户输入.sh b/处理用户输入/获取用户输入.sh new file mode 100644 index 0000000..4e36186 --- /dev/null +++ b/处理用户输入/获取用户输入.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# testing the reading command + +echo -n "Enter your name:" +read name +echo "Hello $name, welcome to my program" + +read -p "Please enter your age: " age +days=$[ $age * 365 ] +echo "That makes you over $days days old" + +#指定多个变量,输入的每个数据值都会分配给表中的下一个变量,如果用完了,就全分配各最后一个变量 +read -p "Please enter name:" first last +echo "Checking data for $last. $first..." + +#如果不指定变量,read命令就会把它收到的任何数据都放到特殊环境变量REPLY中 +read -p "Enter a number:" +factorial=1 +for (( count=1; count<=$REPLY; count++)) +do + factorial=$[ $factorial * $count ] +done +echo "The factorial of $REPLY is $factorial" + + + diff --git a/处理用户输入/读取参数.sh b/处理用户输入/读取参数.sh new file mode 100644 index 0000000..1a37a7c --- /dev/null +++ b/处理用户输入/读取参数.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# using one command line parameter + +factorial=1 +for (( number = 1; number <= $1; number++)) +do + factorial=$[ $factorial * $number ] +done +echo The factor of $1 is $factorial diff --git a/处理用户输入/读取多个命令行参数.sh b/处理用户输入/读取多个命令行参数.sh new file mode 100644 index 0000000..6d2efcf --- /dev/null +++ b/处理用户输入/读取多个命令行参数.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# handing lots of parameters + +total=$[ ${10} * ${11} ] +echo The tenth parameter is ${10} +echo The eleventh parameter is ${11} +echo The total is $total diff --git a/处理用户输入/读取程序名.sh b/处理用户输入/读取程序名.sh new file mode 100644 index 0000000..f148da0 --- /dev/null +++ b/处理用户输入/读取程序名.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# testing the $0 parameter + +echo The command entered is $0 + +#当传给$0变量的真实字符串是整个脚本的路径是,程序中就会使用整个路径,而不仅仅是程序名 + +name=`basename $0` +echo The command entered is $name diff --git a/处理用户输入/超时和输入计数.sh b/处理用户输入/超时和输入计数.sh new file mode 100644 index 0000000..7dc8387 --- /dev/null +++ b/处理用户输入/超时和输入计数.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# timing the data entry + +if read -t 5 -p "Please enter your name:" name +then + echo "Hello, $name, welcome to my script" +else + #起到换行的作用 + echo + #输入计数 -n1 + read -n1 -p "Do you want to continue [Y/N]?" answer + case $answer in + Y | y) echo + echo "Fine, continue on...";; + N | n) echo + echo "OK,goodbye";; + *) echo + echo "OK, wrong, goodbye" + esac + echo "Sorry, this is the end of the script" +fi + diff --git a/处理用户输入/隐藏方式读取数据.sh b/处理用户输入/隐藏方式读取数据.sh new file mode 100644 index 0000000..1299be2 --- /dev/null +++ b/处理用户输入/隐藏方式读取数据.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# hiding input data from monitor + +read -s -p "Please enter your password: " pass + +#添加了-s选项之后,不会自动换行,不添加-s 会自动换行 +echo +echo "Is your password really $pass?" diff --git a/理解输入和输出/2.sh b/理解输入和输出/2.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/理解输入和输出/2.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/理解输入和输出/3.sh b/理解输入和输出/3.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/理解输入和输出/3.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/理解输入和输出/4.sh b/理解输入和输出/4.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/理解输入和输出/4.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/理解输入和输出/newsh b/理解输入和输出/newsh new file mode 100644 index 0000000..e71a171 --- /dev/null +++ b/理解输入和输出/newsh @@ -0,0 +1,10 @@ +#!/bin/bash + +#创建10个sh + +for (( i=1; i<=4; i++)) +do + touch $i.sh + echo '#!/bin/bash' > $i.sh + chmod 764 $i.sh +done diff --git a/理解输入和输出/test b/理解输入和输出/test new file mode 100644 index 0000000..6947bc4 --- /dev/null +++ b/理解输入和输出/test @@ -0,0 +1,2 @@ +ls: cannot access badtest: No such file or directory +but this should go to the testerror file diff --git a/理解输入和输出/test1 b/理解输入和输出/test1 new file mode 100644 index 0000000..fffebad --- /dev/null +++ b/理解输入和输出/test1 @@ -0,0 +1 @@ +This is the end of the script diff --git a/理解输入和输出/临时重定向.sh b/理解输入和输出/临时重定向.sh new file mode 100644 index 0000000..4681abe --- /dev/null +++ b/理解输入和输出/临时重定向.sh @@ -0,0 +1,6 @@ +#!/bin/bash +# testing STDERR messages + +echo "This is an error " >&2 +echo "This is another error" +echo "This is also an error" >&2 diff --git a/理解输入和输出/在脚本中使用重定向输入.sh b/理解输入和输出/在脚本中使用重定向输入.sh new file mode 100644 index 0000000..c3105fe --- /dev/null +++ b/理解输入和输出/在脚本中使用重定向输入.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# redirecting the inpiut + +# 从test中读取数据,而不是从STDIN中读取数据 +exec 0< test +count=1 +while read line +do + echo "Line #$count : $line " + count=$[ $count +1 ] +done + diff --git a/理解输入和输出/永久重定向.sh b/理解输入和输出/永久重定向.sh new file mode 100644 index 0000000..d03a3bd --- /dev/null +++ b/理解输入和输出/永久重定向.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# testing STDERR messages +# redirecting all to a file + +# 脚本执行期间,用exec命令告诉shell重定向某个特定文件描述符 +exec 2>test + +ls badtest +echo "This is test of redirecting all output" +echo "from a script to another file" + +exec 1>test1 +echo "This is the end of the script" +echo "but this should go to the testerror file" >&2 diff --git a/结构化命令/bash-shell无法处理浮点数.sh b/结构化命令/bash-shell无法处理浮点数.sh new file mode 100644 index 0000000..8aaa353 --- /dev/null +++ b/结构化命令/bash-shell无法处理浮点数.sh @@ -0,0 +1,11 @@ +#!/bin/bash +#bash shell 仅能处理浮点数值,test命令无法处理val1变量中存储的浮点值 + +#testing floating point numbers + +val1=`echo "scale=4; 10 / 3" | bc` +echo "The test value is $val1" +if [ $val1 -gt 3 ] +then + echo "The result is larger than 3" +fi diff --git a/结构化命令/continue退出多层循环.sh b/结构化命令/continue退出多层循环.sh new file mode 100644 index 0000000..96ae556 --- /dev/null +++ b/结构化命令/continue退出多层循环.sh @@ -0,0 +1,16 @@ +#!/bin/bash +#continuing an outer loop + +for (( a=1; a<=5; a++)) +do + echo "Iteration $a:" + for (( b=1; b<3; b++ )) + do + if [ $a -gt 2 ] && [ $a -lt 4 ] + then + continue 2 + fi + var3=$[ $a * $b ] + echo " The result of $a * $b is $var3" + done +done diff --git a/结构化命令/if-then-else语句.sh b/结构化命令/if-then-else语句.sh new file mode 100644 index 0000000..41aae93 --- /dev/null +++ b/结构化命令/if-then-else语句.sh @@ -0,0 +1,10 @@ +#!/bin/bash +#testing the else section +testuser=badtest +if grep $testuser /etc/passwd +then + echo The files for user $testuser are: + ls -a /home/.b* +else + echo "The user name $testuser does not exist on this system" +fi diff --git a/结构化命令/if-then语句.sh b/结构化命令/if-then语句.sh new file mode 100644 index 0000000..21b53a6 --- /dev/null +++ b/结构化命令/if-then语句.sh @@ -0,0 +1,12 @@ +#!/bin/bash +#testing the if statement +if date +then + echo "it worked" +fi +echo -e '\n' +if asd +then + echo "it not worked" +fi +echo 'We are outside the if statement' diff --git a/结构化命令/output.txt b/结构化命令/output.txt new file mode 100644 index 0000000..9609b05 --- /dev/null +++ b/结构化命令/output.txt @@ -0,0 +1,12 @@ +/home/tiandi/Desktop is a directory +/home/tiandi/Documents is a directory +/home/tiandi/Downloads is a directory +/home/tiandi/examples.desktop is a file +/home/tiandi/lamp is a directory +/home/tiandi/Music is a directory +/home/tiandi/Pictures is a directory +/home/tiandi/Public is a directory +/home/tiandi/sh is a directory +/home/tiandi/Templates is a directory +/home/tiandi/test is a directory +/home/tiandi/Videos is a directory diff --git a/结构化命令/while里面嵌套for循环.sh b/结构化命令/while里面嵌套for循环.sh new file mode 100644 index 0000000..ab53b05 --- /dev/null +++ b/结构化命令/while里面嵌套for循环.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# placing a for loop inside a while loop + +var1=5 + +while [ $var1 -ge 0 ] +do + echo "Outer loop: $var1" + for (( var2=1; $var2 < 3; var2++)) + do + var3=$[ $var1*$var2] + echo "Inner loop: $var1 * $var2 = $var3" + done + var1=$[ $var1 - 1 ] +done diff --git a/结构化命令/两个for嵌套循环.sh b/结构化命令/两个for嵌套循环.sh new file mode 100644 index 0000000..7bba122 --- /dev/null +++ b/结构化命令/两个for嵌套循环.sh @@ -0,0 +1,11 @@ +#!/bin/bash +#nesting for loops + +for (( a=1; a<=3; a++ )) +do + echo "Starting loop $a:" + for (( b=1; b<=3; b++)) + do + echo "Inside loog: $b:" + done +done diff --git a/结构化命令/从变量读取列表.sh b/结构化命令/从变量读取列表.sh new file mode 100644 index 0000000..43aad21 --- /dev/null +++ b/结构化命令/从变量读取列表.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# using a variable to hold the list + +list="Alabama Alaska Arizona" +list=$list" Connecticut" + +for state in $list +do + echo "Have you ever visited $state" +done + diff --git a/结构化命令/从命令读取值.sh b/结构化命令/从命令读取值.sh new file mode 100644 index 0000000..a5e52c4 --- /dev/null +++ b/结构化命令/从命令读取值.sh @@ -0,0 +1,18 @@ +#!/bin/bash +#reading values from a file + +file="state" + +#更改字段分隔符,使其只能识别换行符 +IFS=$'\n' + +#处理长脚本是,在一个地方修改了该值,然后可能忘了修改过该值 +#IFS.OLD=$IFS +#IFS=$'\n' +#具体代码 +#IFS=$IFS.OLD + +for state in `cat $file` +do + echo "Visit beautiful $state" +done diff --git a/结构化命令/使用C语言风格的for命令.sh b/结构化命令/使用C语言风格的for命令.sh new file mode 100644 index 0000000..8c86994 --- /dev/null +++ b/结构化命令/使用C语言风格的for命令.sh @@ -0,0 +1,7 @@ +#!/bin/bash +#testing the C-style for loop + +for (( i=1; i<=10; i++ )) +do + echo "The next number is $i" +done diff --git a/结构化命令/使用break跳出外部循环.sh b/结构化命令/使用break跳出外部循环.sh new file mode 100644 index 0000000..f6a8ea9 --- /dev/null +++ b/结构化命令/使用break跳出外部循环.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# break n,默认为1 + +for (( a=1; a<=3; a++ )) +do + echo "Outer loop : $a" + for (( b=1; b < 100; b++ )) + do + if [ $b -gt 4 ] + then + break 2 + fi + echo " Inner loop:$b" + done +done + diff --git a/结构化命令/使用case语句.sh b/结构化命令/使用case语句.sh new file mode 100644 index 0000000..8a742d8 --- /dev/null +++ b/结构化命令/使用case语句.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +#using the case command + +case $USER in +tiandi | barbar) + echo "Welcome, $USER" + echo "Pleas enjoy your visit";; +testing) + echo "Special testing account";; +jessica) + echo "Do not forget to logout when you are out";; +*) + echo "Sorry, you are not allowed here";; +esac diff --git a/结构化命令/使用continue命令.sh b/结构化命令/使用continue命令.sh new file mode 100644 index 0000000..9d9e3a1 --- /dev/null +++ b/结构化命令/使用continue命令.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +#using the continue command + +for (( var1 = 1; var1 < 15; var1++ )) +do + if [ $var1 -gt 5 ] && [ $var1 -lt 10 ] + then + continue + fi + echo "Iteration number:$var1" +done diff --git a/结构化命令/使用elif.sh b/结构化命令/使用elif.sh new file mode 100644 index 0000000..ba231a0 --- /dev/null +++ b/结构化命令/使用elif.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# looking for a possible value + +if [ $USER = "tiandi" ] +then + echo "Welcome $USER" + echo "Please enjoy your visit" +elif [ $USER = testing ] +then + echo "Welcome $USER" + echo "Please enjoy your visit" +elif [ $USER = barbar ] +then + echo "Do not forget to logout when you're done" +else + echo "Sorry, you are not allowed here" +fi + diff --git a/结构化命令/使用test进行数值比较.sh b/结构化命令/使用test进行数值比较.sh new file mode 100644 index 0000000..1220b20 --- /dev/null +++ b/结构化命令/使用test进行数值比较.sh @@ -0,0 +1,15 @@ +#!/bin/bash +var1=10 +var2=5 +if [ $var1 -gt 5 ] +then + echo "The test value $var1 is greater than 5" +fi +if [ $var1 -eq $var2 ] +then + echo "The values is equal" +else + echo "The values are different" +fi + + diff --git a/结构化命令/使用until命令.sh b/结构化命令/使用until命令.sh new file mode 100644 index 0000000..33732d4 --- /dev/null +++ b/结构化命令/使用until命令.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +#using the until command + +var1=100 +until [ $var1 -eq 0 ] +do + echo $var1 + var1=$[ $var1-25 ] +done + +var1=100 +until echo $var1 + [ $var1 -eq 0 ] +do + echo Inside the loop: $var1 + var1=$[ $var1 - 25 ] +done + + + diff --git a/结构化命令/使用while.sh b/结构化命令/使用while.sh new file mode 100644 index 0000000..e49d5bb --- /dev/null +++ b/结构化命令/使用while.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# while command test + var1=10 + while [ $var1 -gt 0 ] + do + echo $var1 + var1=$[ $var1 - 1 ] +done + diff --git a/结构化命令/使用while多个测试命令.sh b/结构化命令/使用while多个测试命令.sh new file mode 100644 index 0000000..c673e85 --- /dev/null +++ b/结构化命令/使用while多个测试命令.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +#testing a multicommand while loop + +var1=10 +while echo $var1 + [ $var1 -ge 0 ] +do + echo 'This is inside the loop' + var1=$[ $var1 - 1 ] +done diff --git a/结构化命令/使用双圆括号.sh b/结构化命令/使用双圆括号.sh new file mode 100644 index 0000000..984545e --- /dev/null +++ b/结构化命令/使用双圆括号.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# using double parenthesis + +var1=10 + +if (( $var1 ** 2 > 90)) +then + (( var2 = $var1 ** 2)) + echo "The square of $var1 if $var2" +fi + diff --git a/结构化命令/使用双引号圈起用空格分隔的字符串.sh b/结构化命令/使用双引号圈起用空格分隔的字符串.sh new file mode 100644 index 0000000..a965817 --- /dev/null +++ b/结构化命令/使用双引号圈起用空格分隔的字符串.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# another example of how not to use the for command + +for test in Newada "New Hampshire" +do + echo "Now going to $test" +done diff --git a/结构化命令/使用双方括号.sh b/结构化命令/使用双方括号.sh new file mode 100644 index 0000000..4d76eca --- /dev/null +++ b/结构化命令/使用双方括号.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# using pattern matching + +if [[ $USER == r* ]] +then + echo "Hello $USER" +else + echo "Sorry, I do not know you" +fi diff --git a/结构化命令/使用嵌套循环并修改IFS.sh b/结构化命令/使用嵌套循环并修改IFS.sh new file mode 100644 index 0000000..5887c99 --- /dev/null +++ b/结构化命令/使用嵌套循环并修改IFS.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +#changing the IFS value + +IFS.OLD=$IFS +IFS=$'\n' +for entry in `cat /etc/passwd` +do + echo "Values in $entry -" + IFS=: + for value in $entry + do + echo " $value" + done +done diff --git a/结构化命令/使用符合条件测试.sh b/结构化命令/使用符合条件测试.sh new file mode 100644 index 0000000..89f7a8f --- /dev/null +++ b/结构化命令/使用符合条件测试.sh @@ -0,0 +1,9 @@ +#!/bin/bash +#testing compound comparisons + +if [ -d $HOME ] && [ -w $HOME/testing ] +then + echo "The file exists and you can write to it" +else + echo "I cannot write to it" +fi diff --git a/结构化命令/使用管道或重定向.sh b/结构化命令/使用管道或重定向.sh new file mode 100644 index 0000000..7ec74a4 --- /dev/null +++ b/结构化命令/使用管道或重定向.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# redirecting the for output to a file +for file in /home/tiandi/* +do + if [ -d "$file" ] + then + echo "$file is a directory" + else + echo "$file is a file" + fi +done > output.txt + +# piping a loop to another command + +for state in "North Dakota" Connecticut +do + echo "$state is next place to go" +done | sort +echo "This completes our travels" diff --git a/结构化命令/使用通配符处理目录.sh b/结构化命令/使用通配符处理目录.sh new file mode 100644 index 0000000..cc45dad --- /dev/null +++ b/结构化命令/使用通配符处理目录.sh @@ -0,0 +1,12 @@ +#!/bin/bash +#iterate through all the files in a directory +for file in /home/tiandi/test/* +do + if [ -d "$file" ] + then + echo "$file is a directory" + elif [ -f "$file" ] + then + echo "$file is a file" + fi +done diff --git a/结构化命令/判断字符串长度是否为零.sh b/结构化命令/判断字符串长度是否为零.sh new file mode 100644 index 0000000..13114cc --- /dev/null +++ b/结构化命令/判断字符串长度是否为零.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# testing string length + +#-n 判断长度是否非零 +#-z 判断长度是否为零 + +val1=testing +val2='' + +if [ -n "$val1" ] +then + echo "The string $val1 is not empty" +else + echo "The string $val1 is empty" +fi + +if [ -z "$val2" ] +then + echo "The string $val2 is empty" +else + echo "The string $val2 is not empty" +fi +if [ -z "$val3" ] +then + echo "The string $val3 is empty" +else + echo "The string $val3 is not empty" +fi + diff --git a/结构化命令/在for语句中使用多个变量.sh b/结构化命令/在for语句中使用多个变量.sh new file mode 100644 index 0000000..48d4118 --- /dev/null +++ b/结构化命令/在for语句中使用多个变量.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# multiple variables + +for (( a=1, b=10; a<=10; a++,b-- )) +do + echo "$a - $b" +done diff --git a/结构化命令/在then块中使用多条命令.sh b/结构化命令/在then块中使用多条命令.sh new file mode 100644 index 0000000..a4d439c --- /dev/null +++ b/结构化命令/在then块中使用多条命令.sh @@ -0,0 +1,8 @@ +#!/bin/bash +#testing multiple commands in the then section +testuser=tiandi +if grep $testuser /etc/passwd +then + echo The bash files from user $testuser are: + ls -a /home/$testuser/.b* +fi diff --git a/结构化命令/字符串比较.sh b/结构化命令/字符串比较.sh new file mode 100644 index 0000000..0382761 --- /dev/null +++ b/结构化命令/字符串比较.sh @@ -0,0 +1,9 @@ +#!/bin/bash +#testing string equality + +testuser=tiandi + +if [ $USER = $testuser ] +then + echo "Welcome $testuser" +fi diff --git a/结构化命令/嵌套if.sh b/结构化命令/嵌套if.sh new file mode 100644 index 0000000..05a7907 --- /dev/null +++ b/结构化命令/嵌套if.sh @@ -0,0 +1,2 @@ +#!/bin/bash + diff --git a/结构化命令/检查文件或目录是否存在.sh b/结构化命令/检查文件或目录是否存在.sh new file mode 100644 index 0000000..f0380e8 --- /dev/null +++ b/结构化命令/检查文件或目录是否存在.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# checking if a directory or a file exists + +if [ -e $HOME ] +then + echo "OK on the directory.now to check the file" + #checking if a file exists + if [ -e $HOME/testing ] + then + #the file exists,append data to it + echo "Appending date to existing file" + date >> $HOME/testing + else + #the file is not exists,create a new file + echo "Creating a new file" + date > $HOME/testing + fi +else + echo 'Sorry. you do not have a $HOME directory' +fi diff --git a/结构化命令/检查目录.sh b/结构化命令/检查目录.sh new file mode 100644 index 0000000..7a1fe79 --- /dev/null +++ b/结构化命令/检查目录.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# look before you leap + +if [ -d $HOME ] +then + echo "Your home directory exists" + cd $HOME + ls -a +else + echo "There is a problem with your HOME direcotry" +fi diff --git a/结构化命令/正确使用大于小于号.sh b/结构化命令/正确使用大于小于号.sh new file mode 100644 index 0000000..3cc6876 --- /dev/null +++ b/结构化命令/正确使用大于小于号.sh @@ -0,0 +1,12 @@ +#!/bin/bash +#正确使用大于小于号 + +val1=baseball +val2=hocky + +if [ $val1 \> $val2 ] +then + echo "$val1 is greater than $val2" +else + echo "$val1 is less than $val2" +fi diff --git a/结构化命令/注意test大小写顺序和sort不同.sh b/结构化命令/注意test大小写顺序和sort不同.sh new file mode 100644 index 0000000..96123e4 --- /dev/null +++ b/结构化命令/注意test大小写顺序和sort不同.sh @@ -0,0 +1,12 @@ +#!/bin/bash +#test命令中,大小字母会被当成小于小写字符,而在sort中,小写字母会先出现,test使用标准的ASCII排序,sort使用本地化语言设置进行排序,对于英语,本地化设置制定了排序顺序中小写字母出现在大写字母之前 + +var1=Testing +var2=testing + +if [ $val1 \> $val2 ] +then + echo '$val1 is greater than $val2' +else + echo '$val1 is less than $val2' +fi diff --git a/结构化命令/解决读取列表中的复杂值.sh b/结构化命令/解决读取列表中的复杂值.sh new file mode 100644 index 0000000..e5e442d --- /dev/null +++ b/结构化命令/解决读取列表中的复杂值.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +for test in I don\'t know if "this'll" work +do + echo "word:$test" +done diff --git a/结构化命令/读取列表中的值.sh b/结构化命令/读取列表中的值.sh new file mode 100644 index 0000000..7063175 --- /dev/null +++ b/结构化命令/读取列表中的值.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# basic for command +for test in Alabama Alaska Arizona +do + echo The next state is $test +done diff --git a/结构化命令/读取里表中复杂的值.sh b/结构化命令/读取里表中复杂的值.sh new file mode 100644 index 0000000..52034e7 --- /dev/null +++ b/结构化命令/读取里表中复杂的值.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# another example of how not to use the for command + +for test in I don't know if this'll work +do + echo "word:$test" +done diff --git a/结构化命令/错误的使用大于小于号.sh b/结构化命令/错误的使用大于小于号.sh new file mode 100644 index 0000000..7bc876b --- /dev/null +++ b/结构化命令/错误的使用大于小于号.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# 大于小于号必须转义,否则shell会将它们当做重定向符号而把字符串值当做文件名处理 +# 大于小于号顺序和sort命令所采用的有所不同 +# mis-using string comparisons + +val1=baseball +val2=hockey + +if [ $val1 > $val2 ] +then + echo "$val1 is greater than $val2" +else + echo "$val1 is less than $val2" +fi