read命令: Read a line from the standard input and split it into fields. read - read [-a array] [-p prompt] [-t timeout] [name ...] -a array:定义索引数组; -p prompt:给用户输出提示信息; -t timeout:用户输入的超时时间; name:变量或数组的名称;如果省略此内容,bash会将read读到的信息直接保存到内置的名为REPLY变量中;
#!/bin/bash # if [ $# -ne 2 ] ; then echo "Make sure provide TWO arguments." exit 5 fi
if [ $1 == '-a' ] ; then if ! id $2 &> /dev/null ; then useradd $2 &> /dev/null echo $2 | passwd --stdin $2 &> /dev/null echo "User $2 created succesfully and password changed to it's username." else echo "$2 exists already." fi elif [ $1 == '-d' ] ; then if id $2 &> /dev/null ; then userdel -r $2 &> /dev/null echo "User $2 delete finished." else echo "$2 does not exist yet." fi else echo "Usage: $(basename $0) -a USERNAME | -d USERNAME" exit 6 fi
改进版:使用read命令; #!/bin/bash # if [ $# -ne 1 ] ; then echo "Make sure provide ONE argument." exit 5 fi
if [ $1 == '-a' ] ; then read -p "Please input a username for creating: " USERNAME if ! id $USERNAME &> /dev/null ; then useradd $USERNAME &> /dev/null echo $USERNAME | passwd --stdin $USERNAME &> /dev/null echo "User $USERNAME created succesfully and password changed to it's username." else echo "$USERNAME exists already." fi elif [ $1 == '-d' ] ; then read -p "Please input a username for deleting: " USERNAME read -t 5 -p "confirm? Input 'yes' to continue: " CHOICE [ -z $CHOICE ] && CHOICE='no'
if [ $CHOICE == 'yes' ] ; then if id $USERNAME &> /dev/null ; then userdel -r $USERNAME &> /dev/null echo "User $USERNAME delete finished." else echo "$USERNAME does not exist yet." fi else echo echo "$USERNAME is not deleted." fi else echo "Usage: $(basename $0) { -a | -d }" exit 6 fi
写脚本解决问题: 1.判断用户通过命令行给的一个参数是否为整数。
#!/bin/bash # if [ $# -ne 1 ] ; then echo "Make sure provide ONE digit." exit 5 fi
# if ! [[ $1 =~ [^[:digit:]] ]] ; then if [[ $1 =~ ^[[:digit:]]+$ ]] ; then echo "$1 is a pure digit." else echo "$1 is not a digit." fi
for循环: 1.遍历列表的循环: Execute commands for each member in a list. for - for NAME [in WORDS ... ] ; do COMMANDS; done
建议在脚本中的书写格式: for VAR_NAME in LIST ; do 循环体 done 或 for VAR_NAME in LIST do 循环体 done
注意: VAR_NAME:任意指定的变量名称,变量的值是从LIST中遍历获取的各个元素; LIST:for循环需要遍历的列表;可以通过以下方式生成列表: 1.直接给出列表; 2.纯整数列表: 1) 花括号展开: {FIRSTNUMM..LASTNUM} {FIRST,SECOND,THIRD,....,LAST} 2) seq命令 seq [OPTION]... LAST seq [OPTION]... FIRST LAST seq [OPTION]... FIRST INCREMENT LAST 3.花括号展开: {FIRST..LAST} 4.命令的执行结果: ls /etc grep /PATH/TO/SOMEFILE 5.GLOBBING 6.某些特殊变量的值: $*, $@ 循环体: 一般来说,循环体中应该包括能够用到VAR_NAME变量的值的命令或命令的组合;如果循环体中的命令并没有用到VAR_NAME变量的值的话,列表的元素个数就是此次for循环的次数;
写一个脚本,计算1到100的数字之和; #!/bin/bash # declare -i SUM=0 for I in {1..100} ; do SUM=$[SUM+I] done echo "The summary is: $SUM"
写一个脚本,能够通过-a或-d选项添加或删除一个或多个用户账户; #!/bin/bash # if [ $# -lt 2 ] ; then echo "Make sure provide more than TWO arguments." exit 5 fi
if [ $1 == '-a' ] ; then shift for I in "$@" ; do if ! id $I &> /dev/null ; then useradd $I &> /dev/null echo $I | passwd --stdin $I &> /dev/null echo "User $I created succesfully and password changed to it's username." else echo "$I exists already." fi done elif [ $1 == '-d' ] ; then shift for J in "$@" ; do if id $J &> /dev/null ; then userdel -r $J &> /dev/null echo "User $J delete finished." else echo "$J does not exist yet." fi done else echo "Usage: $(basename $0) -a UNAME1 [UNAME2 ...] | -d UNAME1 [UNAME2 ...]" exit 6 fi
计算指定数字范围内自然数的和: #!/bin/bash # declare -i SUM=0 read -p "Please input TWO integer: " INT1 INT2 if [[ $INT1 =~ [^[:digit:]] ]] ; then echo "$INT1 must be an integer." exit 5 fi
if [[ $INT2 =~ [^[:digit:]] ]] ; then echo "$INT2 must be an integer." exit 5 fi
if [ $INT1 -gt $INT2 ] ; then for I in $(seq $INT2 $INT1) ; do # SUM=$[SUM+I] let SUM+=$I done echo "The summary is: $SUM" else for I in $(seq $INT1 $INT2) ; do # SUM=$[SUM+I] let SUM+=$I done echo "The summary is: $SUM" fi
N行,N-1个空白字符,2*(总行数-当前行号)+1 个* #!/bin/bash # if [ $# -ne 1 ] ; then echo "Usage: $(basename $0) INTEGER" exit 5 fi
if [[ $1 =~ [^[:digit:]] ]] ; then echo "Usage: $(basename $0) INTEGER" exit 6 fi
LINENUM=$1 for I in $(seq $LINENUM) ; do for J in $(seq $[I-1]) ; do echo -n " " done for K in $(seq $[2*(LINENUM-I)+1]) ; do echo -n "*" done echo done
打印九九乘法表 第一行:1个 第二行:2个 ... 第九行:9个
#!/bin/bash # for I in {1..9} ; do for J in $(seq $I) ; do echo -ne "$I×$J=$[I*J]\t" done echo done
#!/bin/bash # for (( I=1 ; I<=9 ; I++ )) ; do for (( J=1 ; J<=I ; J++ )) ; do echo -ne "$J×$I=$[I*J]\t" done echo done