随机数
在 6 ~ 9 范围内取随机数
$ echo `expr $[RANDOM%4] + 6`
在 shell 中生成 32 位随机密码
$ cat /dev/urandom | head -10 | sha512sum | head -c 32
用 /dev/urandom 的前 10 行内容做为随机的种子,计算其 sha 值,取结果的前 32 个字符。
调试
检查脚本是否能正常运行
本题的逻辑有些白痴,权当熟悉脚本用了。
如果可以正常运行,返回提示消息;如果运行错误,键入 V 或 v,会用 vim 自动打开脚本,键入 Q 或 q 或任意键可忽略并退出。
#!/bin/bash
if [ ${#1} == 0 ] ; then
read -p "please type in the script name : " file
else
file=$1
fi
# run the script if it's not empty
if [ -f $file ]; then
sh -n $file > /dev/null 2>&1
if [ $? -ne 0 ]; then
read -p "Syntax error detected. Press Q to exit. Press V to open it with vim" answer
case $answer in
v | V )
vim $file
;;
q | Q)
exit 0
;;
*)
exit 0
;;
esac
else
echo 'no error detected, congratulations!'
fi
else
echo "$file not exist"
exit 1
fi