1、多线程执行

  1. while read line
  2. do
  3. echo $user
  4. done < user.txt

2、while循环从文件中读取内容:适合处理文件

  1. while read ip
  2. do
  3. {
  4. ping -c1 -W1 $ip &>/dev/null
  5. }&
  6. done < ip.txt
  7. thread=5 #进程个数
  8. tmp_fifofile=/tmp/$$.fifo #创建管道
  9. mkfifo $tmp_fifofile
  10. exec 8<> $tmp_fifofile #打开文件描述符
  11. rm $tmp_fifofile
  12. for i in `seq $thread`
  13. do
  14. echo >&8 #往管道中加入5个空行,加入其他的内容也可以
  15. done
  16. while read ip
  17. do
  18. read -u 8 #每次读一行
  19. {
  20. ping -c1 -W1 $ip &>/dev/null
  21. echo >&8 #处理完后加入一行,使管道中一直有五行
  22. }&
  23. done < ip.txt
  24. wait
  25. exec 8<&- #关闭文件描述符

3、无限循环,:命令执行永久为true

  1. while :
  2. do
  3. #statements
  4. done
  5. while true
  6. do
  7. #statements
  8. done

4、判断:再次确认(输入y/n)

  1. read -p "Are you sure? [y/n]" action
  2. if [[ "$action" = "y" ]]; then
  3. #statements
  4. fi
  5. read -p "Are you sure? [y/n]" action
  6. case $action in
  7. y|Y|YES|yes )
  8. ;;
  9. esac

5、判断参数个数

  1. if [[ $# -eq 0 ]]; then
  2. echo "参数个数为0"
  3. exit 1
  4. fi
  1. 默认取所有参数$*
  2. for i
  3. do
  4. #statements
  5. done

6、提示用法

  1. usage(){
  2. echo "Usage: `basename $0` (start|stop|restart|safeStart)"
  3. exit -1
  4. }

7、$? : 判断上一个命令执行是否成功

  1. mkdir /usr/local/redis
  2. if [[ $? -eq 0 ]]; then
  3. echo "ok"
  4. fi

7、关联数组

  1. declare -A hosts
  2. while read line
  3. do
  4. type=`echo $line |awk '{print $2}'`
  5. let hosts[$type]++
  6. done </etc/hosts
  7. for i in ${!hosts[@]} #数组索引
  8. do
  9. echo "$i: ${hosts[$i]}"
  10. done

8、select选项

  1. #!/usr/bin/bash
  2. #$PS3赋值来修改提示符
  3. PS3="command(5 for quit):"
  4. select command in disk_partion fs cpu_load mem_util quit
  5. do
  6. case $command in
  7. disk_partion )
  8. fdisk -l
  9. ;;
  10. fs )
  11. df -h
  12. ;;
  13. cpu_load )
  14. uptime
  15. ;;
  16. mem_util )
  17. free -m
  18. ;;
  19. quit)
  20. break
  21. ;;
  22. *)
  23. echo "erro"
  24. exit -1
  25. esac
  26. done

可以使用循环自定义:

  1. #!/bin/bash
  2. help(){
  3. echo "Command action"
  4. echo " a toggle a bootable flag"
  5. echo " b edit bsd disklabel"
  6. echo " c toggle the dos compatibility flag"
  7. echo " d delete a partition"
  8. echo " g create a new empty GPT partition table"
  9. echo " G create an IRIX (SGI) partition table"
  10. echo " l list known partition types"
  11. echo " m print this menu"
  12. echo " n add a new partition"
  13. echo " o create a new empty DOS partition table"
  14. echo " p print the partition table"
  15. echo " q quit without saving changes"
  16. echo " s create a new empty Sun disklabel"
  17. echo " t change a partition's system id"
  18. echo " u change display/entry units"
  19. echo " v verify the partition table"
  20. echo " w write table to disk and exit"
  21. echo " x extra functionality (experts only)"
  22. }
  23. while [[ true ]]; do
  24. read -p "command(m for help):" command
  25. case $command in
  26. m )
  27. help
  28. ;;
  29. q )
  30. exit
  31. ;;
  32. * )
  33. help
  34. ;;
  35. esac
  36. done