#!/bin/bash #readname echo "your first name:" read firstname echo "your last name:" read lastname subname echo "your full name is ${firstname} ${lastname}!" echo "yout subname is ${subname}" #!/bin/bash #shell1.sh echo $"脚本的名字 $0" echo $"第一个参赛 $1" echo $"第二个参数 $2" #!/bin/bash #数组的演示 fruit[0]=apple fruit[1]=banana fruit[2]=peach fruit[3]="passion fruit" echo "单独显示:" echo $fruit[0] echo $fruit[1] echo $fruit[2] echo $fruit[3] echo "全部显示:" echo ${fruit[*]} echo ${fruit[@]} #!/bin/bash # if判断 if [ -d /usr/bin ]; then echo "/usr/bin 是个目录文件!" elif [ -h /usr/bin]; then echo "/usr/bin 是个符号链接文件" fi #case echo -n "please input [Yes, yes y, Y]:" read ANS case "$ANS" in [Yy]es) ANS="Y" echo "${ANS}" ;; [Yy]) ANS="N" echo "${ANS}" ;; esac #统计文件行数 echo -n "please input file name:" read filename if [ -f "$filename" ]; then n=0 while read line do n=`expr $n + 1` done < "$filename" echo $n fi #until用法 x=1 until [ $x -ge 10 ] do echo $x x=`expr $x + 1` done #for 循环处理文件 mkdir ${HOME}/public1 for file in $HOME/* do cp $file ${HOME}/public1 chmod a+r ${HOME}/public1/${file} done x=0 while [ $x -lt 10 ] do y=0 while [ $y -le $x ] do echo -n "$y " y=`expr $y + 1` done echo "" x=`expr $x + 1` done #无限循环 while : do list done #break 用来跳出循环 while : do echo -n "please input the command which you want to exec: " read CMD case $CMD in [qQ] | [qQ][Uu][iI][tT]) break ;; *) $CMD ;; esac done #break 命令也可以接受一一个整数作为参数,大于等于1的整数,标记着退出循环的层数,在嵌套循环里很 #有用 for i in 1 2 3 4 5 do mkdir -p /mnt/backup/docs/ch0${i} if [ $? -eq 0 ]; then for j in c h m pl sh do cp $HOME/docs/ch0${i}/*.${j} /mnt/backup/docs/ch0${i} if [ $? -ne 0 ]; then break 2; fi done else echo "Could not make backup directory." fi done #continue 命令 for FILE in $FILES do if [ ! -f "$FILE" ] ; then echo "Error: $FILE is not a file." continue fi done