脚本范例
输出消息
在 echo
命令使用 -n
参数可以让输出不换行:
#!/bin/bash fruite=orange count=110 echo -n "We have ${count} ${fruite}s" echo "??"
交互式脚本
使用 read
来读取用户输入保存到变量中:
#!/bin/bash read -r -p "Set Username: " newuser read -r -p "Set Password: " newpass echo -e "\n Your user:pass is: $newuser:$newpass"
要想隐藏用户输入使用 -s
选项。如果 read
命令不指定变量,read
会将收到的数据放进特殊的环境变量 REPLY
中。
指定输入超时时间用 -t
参数,计时器过期后,read
命令会返回一个非零退出状态码:
read -t 5 -p "timeout 5s:" myname
也可以指定 read
命令统计输入的字符数,并和 case
配合检测用户输入的字符,脚本在用户输入后立即执行:
#!/bin/bash read -n1 -p "Continue [Y/N]?" answer case $answer in Y|y) echo;echo "OK, Le't continue";; N|n) echo;echo "Bye.";; *) echo;echo "Type Wrong!";; esac
read
也可以用来读取 .csv
文件(逗号分隔值)内容:
#!/bin/bash input=user.csv while IFS=',' read -r username password; do echo "adding user $username" useradd $username -p $password done<$input
或者使用 cat
显示文件内容后再用管道传给 read
:
#!/bin/bash cat output.txt | while read da_line;do echo "$da_line" done
命令替换
备份时将备份时间自动写入到文件名中:
#!/bin/bash # 利用date获取时间,格式为190322 data1=$(date +%Y%m%d) filename1='sqlbackup' # 使用变量命名文件名 touch "$filename1-$data1"
文件检测
例如根据用户输入检测文件是否存在,不存在返回 ‘not exist’,存在返回文件类型:
#!/bin/bash read -rp "Type filename:" file1 test -z $file1 && echo "Type error" && exit 0 test -e $file1 || echo "not exist" && exit 0 test -f $file1 && echo "$file1 is file" test -d $file1 && echo "$file1 is dir"
使用中括号判断
使用 [ ]
判断用户输入是 “yes” 还是 “no”:
#!/bin/bash read -rp "Type yes or no: " utype [ "$utype" == "yes" ] && echo 'OK' && exit 0 [ "$utype" == "no" ] && echo 'NO' && exit 0 echo 'ERROR' && exit 0
判断用户是否为 root:
#!/bin/bash if [ $UID -ne 0 ]; then echo Non root user. else echo root user. fi
参数变量调用
写一段脚本将特殊变量输出:
#!/bin/bash echo "Script name is: $0" echo "Parameter number is: $#" echo "Whole parameter: $@" echo "Frist parameter: $1"
运行结果:
[root@101c7 bin]$ bash sh06.sh op1 ca2 Script name is: sh06.sh Parameter number is: 2 Whole parameter: op1 ca2 Frist parameter: op1
If 条件判断
使用 if
判断对输入参数进行检测:
#!/bin/bash if [ "$1" == "Hello" ] || [ "$1" == "hello" ]; then echo "hello" elif [ "$1" == "" ]; then echo "no parameters" else echo "only hello allowed" fi
Case 条件判断
使用 case
判断脚本参数:
#!/bin/bash case $1 in "hello") echo "hello" ;; "") echo "no parameters" ;; *) echo "only hello allowed" exit 1 ;; esac
函数调用
简单的函数内参数调用:
#!/bin/bash function printype(){ echo "$1 is best" } if [ -n "$2" ]; then printype $2 elif [ -n "$1" ]; then printype $1 else echo "no parameters" fi
不定循环
使用 while
结构,当输入 y
或 Y
时才停止脚本:
#!/bin/bash while [ "$yn" != "y" ] && [ "$yn" != "Y" ] do read -p "Type y or Y to stop:" yn done echo "you stop it!"
使用 until
结构的循环写法:
#!/bin/bash until [ "$yn" == "y" ] || [ "$yn" == "Y" ] do read -p "Type y or Y to stop:" yn done echo "you stop it!"
固定循环
使用 for
来遍历 passwd
中的用户名,并执行 id
命令:
#!/bin/bash users=$(cut -d ':' -f1 /etc/passwd) for username in $users do id $username done
使用数字定义循环次数,例如计算 101 到 200 之间奇数乘积:
#!/bin/bash s=1 for ((i=101;i<=200;i=i+2)); do s=$(($s*$i)) done echo "$s"
批量建立用户 user100
到 user110
:
#!/bin/bash for ((a = 100; a < 110; a++)); do userpass='user'$a /usr/sbin/useradd -p $userpass $userpass done /usr/bin/tail -10 /etc/passwd