1. 编写简单的hello world脚本,查看脚本内容,并运行
    使用bash命令运行shell脚本
[lwj@192 file01]$ touch test.sh  //创建新文件
[lwj@192 file01]$ vim test.sh  //编辑文件内容
[lwj@192 file01]$ cat test.sh  //查看文件内容
#!/bin/bash
echo "Hello World!"
[lwj@192 file01]$ bash test.sh  //运行shell脚本
Hello World!
  1. 使用declare进行运算,declare命令仅支持整数的数值运算,使用一段代码进行比较运算
[lwj@192 file01]$ declare -i var=10+10  //加法
[lwj@192 file01]$ echo $var
20
[lwj@192 file01]$ declare -i var=10*10  //乘法
[lwj@192 file01]$ echo $var
100
[lwj@192 file01]$ declare -i var=20-10  //减法
[lwj@192 file01]$ echo $var
10
[lwj@192 file01]$ declare -i var=10/3  //取整
[lwj@192 file01]$ echo $var
3
[lwj@192 file01]$ vim test.sh  //编辑文件
[lwj@192 file01]$ cat -n test.sh  //查看文件
     1	#!/bin/bash
     2	if [ $# -ne 2 ];then
     3	   echo "Usage: $0 number1 number2"
     4	   exit 1
     5	fi
     6	number1="$1"
     7	number2="$2"
     8	if (( number1>number2 ));then
     9	   echo "$number1 > $number2"
    10	elif (( number1 <  number2 ));then
    11	   echo "$number1 < $number2"
    12	elif (( number1 == number2 ));then
    13	   echo "$number1 == $number2"
    14	else
    15	   echo "should never be here."
    16	fi
[lwj@192 file01]$ bash test.sh 1 2  //1和2比较
1 < 2
[lwj@192 file01]$ bash test.sh 2 1  //2和1比较
2 > 1
[lwj@192 file01]$ bash test.sh 2 2  //2和2比较
2 == 2
  1. 使用test命令对两个数值比较(等于、小于、大于等于)
[lwj@192 file01]$ vim myscript.sh
[lwj@192 file01]$ cat myscript.sh
#!/bin/bash
a=11;b=16
test $a -eq $b && echo "$a = $b" || echo "$a != $b"  //等于
test $a -gt $b && echo "$a < $b" || echo "$a >= $b"  //小于
test $a -ge $b && echo "$a >= $b" || echo "$a <  $b"  //大于等于
[lwj@192 file01]$ sh myscript.sh
11 != 16
11 >= 16
11 <  16
  1. 使用test命令对两个字符串比较(等于、非空),test字符串运算
[lwj@192 file01]$ vim myscript.sh
[lwj@192 file01]$ cat myscript.sh
#!/bin/bash
a="centos"
test -z "$a" && echo "'$a' is null" || echo "'$a' is not null"  // 非空
test "$a" ==  "centos" && echo "'$a'='centos'" || echo "'$a'!='centos'"  //等于
[lwj@192 file01]$ sh myscript.sh
'centos' is not null // 非空
'centos'='centos'  //等于
  1. 使用test命令进行布尔运算,注释说明含义 -a:当表达式$a和$b同时为真时复合表达式返回真,否则为假
    -o:当表达式$a和$b任意一个表达式为真时复合表达式返回真,否则为假
[lwj@192 file01]$ vim myscript.sh
[lwj@192 file01]$ cat myscript.sh
#!/bin/bash
a=11;b=16
f1="myscript.sh"
f2="file2"
test  $b -o $a && echo "$a >= $b" || echo "$a < $b"
test -e "$f1" -a -r "$f1" && echo "$f1 is readable" || echo "$f1 is not exist or not readable"
[lwj@192 file01]$ sh myscript.sh
11 >= 16
myscript.sh is readable
  1. 使用[] 对文件类型、文件权限、文件比较等进行练习。 文件类型-f:当文件fname的文件类型为普通文件时返回真,否则为假
    文件权限-r:当文件fname存在具有可读权限时返回真,否则为假
    文件比较-nt:当文件fname1比文件fname2新时返回真,否则为假
[lwj@192 file01]$ vim myscript.sh
[lwj@192 file01]$ cat myscript.sh
#!/bin/bash
f1="myscript.sh"
f2="file2"
[ -f "$f1" ] && echo "$f1:ordinary file" || echo "$f1:not ordinary file"
[ -r "$f1" ] && echo "$f1:readable" || echo "$f1: not readable"
[ "$f1" -nt "$f2" ] && echo "$f1 is newer than $f2" || echo "$f2 is newer than $f1" 
[lwj@192 file01]$ sh myscript.sh
myscript.sh:ordinary file
myscript.sh:readable
myscript.sh is newer than file2
  1. 使用if-elif结构完成功能。猜数字游戏
[lwj@192 file01]$ vim myscript.sh
[lwj@192 file01]$ cat -n myscript.sh
     1	#!/bin/bash
     2	a=0
     3	b=99
     4	random_num=$(( $RANDOM*100/32767 ))
     5	read -p "请输入竞猜数字:" guess_num
     6	while [ $random_num -ne $guess_num ]
     7	do
     8	  if [ $random_num -gt $guess_num ]; then
     9	     echo "猜小了,范围变为 "$guess_num"-"$b",请输入竞猜数字:"
    10	         a=$guess_num
    11	         read guess_num
    12	  elif [ $random_num -lt $guess_num ]; then
    13	     echo "猜大了,范围变为 "$a"-"$guess_num",请输入竞猜数字:"
    14	         b=$guess_num
    15	         read guess_num
    16	     else
    17	         echo "请输入0-99的数字"
    18	  fi
    19	done
    20	echo "恭喜,竞猜正确"
[lwj@192 file01]$ sh myscript.sh
请输入竞猜数字:50
猜大了,范围变为 0-50,请输入竞猜数字:
25
猜大了,范围变为 0-25,请输入竞猜数字:
13
猜大了,范围变为 0-13,请输入竞猜数字:
7
猜大了,范围变为 0-7,请输入竞猜数字:
4
猜大了,范围变为 0-4,请输入竞猜数字:
2
猜大了,范围变为 0-2,请输入竞猜数字:
1
猜大了,范围变为 0-1,请输入竞猜数字:
0
恭喜,竞猜正确
  1. 结合case-in和函数,完成简易计算器。(选择运算符,再输入数)
[lwj@192 file01]$ vim mytest.sh
[lwj@192 file01]$ cat -n mytest.sh
     1	#!/bin/bash
     2	y=y
     3	while [ "$y" === "y" ] || [ "$y" == "Y" ]
     4	do
     5	  read -p "please input the first num:" num1
     6	  read -p "please input the operator:" a
     7	  read -p "please input the second num:" num2
     8	case "$a" in
     9	     +)
    10	       echo "运算结果为:$(($num1+$num2))"
    11	       ;;
    12	     -)echo "运算结果为:$(($num1-$num2))"
    13	       ;;
    14	     *)echo "运算结果为:$((%num1*$num2))"
    15	       ;;
    16	     /)echo "运算结果为:$(($num1/$num2))"
    17	       ;;
    18	esac
    19	read -p "是否还要继续运算(y/n):" y
    20	done
[lwj@192 file01]$ sh mytest.sh
please input the first num:9
please input the operator:-
please input the second num:2
运算结果为 :7
是否还要将继续运算(y/n):y
please input the first num:3
please input the operator:+
please input the second num:2
运算结果为 :5
是否还要将继续运算(y/n):y
please input the first num:3
please input the operator:*
please input the second num:3
运算结果为 :9
是否还要将继续运算(y/n):n
  1. 对函数传递参数大于、等于、小于所需参数的三种情形分别试验
[lwj@192 file01]$ vim myhs.sh
[lwj@192 file01]$ cat -n myhs.sh
     1	#!/bin/bash
     2	function sum()
     3	{
     4	 echo 'input paramenters are:$@ = "'$@'"'
     5	 if (test $# -lt 2);then
     6	 echo "传递的参数小于指定参数个数"
     7	 return 1
     8	 elif (test $# -gt 2);then
     9	 echo "传递的参数大于指定参数个数"
    10	 return 1
    11	 fi
    12	 echo "传递的参数等于指定参数个数"
    13	 echo "var_sum=$(($1+$2))"
    14	}
    15	sum 1
    16	sum 1 2
    17	sum 1 2 3
[lwj@192 file01]$ sh myhs.sh
input paramenters are:$@ = "1"
传递的参数小于指定参数个数
input paramenters are:$@ = "1 2"
传递的参数等于指定参数个数
var_sum=3
input paramenters are:$@ = "1 2 3"
传递的参数大于指定参数个数