我从其他人的Shell脚本中学到的

我是一个Shell脚本迷,也很喜欢从其他人的Shell脚本里学习一些有趣的东西。最近我偶然接触到用于方便ssh服务器双重认证的 authy-ssh 脚本。 浏览脚本后我学到了一些很酷的东西,在此也想分享给大家。

1. 让你的echo丰富多彩

很多时候,你会想让echo能以多种颜色区分不同输出。比如,绿色表示成功,红色告知失败,,黄色提示警告。

NORMAL=$(tput sgr0)GREEN=$(tput setaf 2; tput bold)YELLOW=$(tput setaf 3)RED=$(tput setaf 1)function red() {echo -e “$RED$*$NORMAL”}function green() {echo -e “$GREEN$*$NORMAL”}function yellow() {echo -e “$YELLOW$*$NORMAL”}# To print successgreen “Task has been completed”# To print errorred “The configuration file does not exist”# To print warningyellow “You have to use higher version.”

这里使用 tput 来配置输出颜色,输出文本,最后再恢复默认输出颜色。如果想对 tpu 了解更多,参看 prompt-color-using-tput 。

2. 输出debug信息

仅当设置DEBUG标志时才打印调试信息。

function debug() {if [[ $DEBUG ]]thenecho “>>> $*”fi}# For any debug messagedebug “Trying to find config file”

还有来自于一些很酷的Geeks的单行debug函数:

function debug() { ((DEBUG)) && echo “>>> $*”; }function debug() { [ “$DEBUG” ] && echo “>>> $*”; }3. 检查特定的可执行文件是否存在OK=0FAIL=1function require_curl() {which curl &>/dev/nullif [ $? -eq 0 ]thenreturn $OKfireturn $FAIL}

这里使用 which 命令来查找可执行文件 curl 的路径。如果成功找到,则可执行文件文件是存在的,否则就不存在。 &>/dev/null 将标准输出和标准错误重定向到 /dv/null (也就是不显示在终端上了)。

一些朋友建议可以直接使用 which 返回的状态码。

# From cool geeks at hacker newsfunction require_curl() { which “curl” &>/dev/null; }function require_curl() { which -s “curl”; }4. 显示脚本的使用说明

在我开始写Shell脚本的初期,常会使用 echo 命令显示脚本的使用说明。 但当说明的文字较多时,echo 语句就会变得一团糟。随后我发现,可以使用 cat命令来显示使用说明。

cat << EOFUsage: myscript

这里的 << 称为 here document,它可以将字符串放置在两个 EOF 之间。

5. 用户设置 vs. 默认配置

我们有时会希望在用户没有提供设置参数时能够使用默认值。

URL=${URL:-:8080}

这一语句检查环境变量 URL ,如果不存在,就将其设置为 localhost。

6. 检查字符串的长度if [ ${#authy_api_key} != 32 ]then red “you have entered a wrong API key” return $FAILfi

${#VARIABLE_NAME} 可以给出字符串的长度。

7. 为读取输入设置时限READ_TIMEOUT=60read -t “$READ_TIMEOUT” input# if you do not want quotes, then escape itinput=$(sed “s/[;\`\”\$\’ ]//g” <<< $input)# For reading number, then you can escape other charactersinput=$(sed ‘s/[^0-9]*//g’ <<< $input)8. 获取目录名和文件名# To find base directoryAPP_ROOT=`dirname “$0″`# To find the file namefilename=`basename “$filepath”`# To find the file name without extensionfilename=`basename “$filepath” .html`

完结。 祝各位编程开心,享受这美好的一天吧!

本文翻译自:

拥有一颗比九万五千公里还辽阔的心,

我从其他人的Shell脚本中学到的

相关文章:

你感兴趣的文章:

标签云: