我从其他Shell脚本中学到了什么?
来源:互联网 发布:浙江网络预约挂号平台 编辑:程序博客网 时间:2024/04/28 17:19
我从其他Shell脚本中学到了什么?
脚本shell调试经验分享摘要:作者Fizer Khan是一位Shell脚本迷,他对有关Shell脚本新奇有趣的东西是如此的痴迷。本文他分享了八大Shell脚本技巧,希望你会喜欢,这些脚本可以直接拿来使用!作者Fizer Khan是一位Shell脚本迷,他对有关Shell脚本新奇有趣的东西是如此的痴迷。最近他遇到了authy-ssh脚本,为了缓解ssh服务器双重认证问题,他学到了许多有用且很酷的东西。对此,他想分享给大家。
一、Colors your echo
大多数情况下,你希望输出echo Color,比如绿色代表成功,红色代表失败,黄色代表警告。
12345678910111213141516171819202122232425NORMAL=$(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 success
green
"Task has been completed"
# To print error
red
"The configuration file does not exist"
# To print warning
yellow
"You have to use higher version."
这里使用tput来设置颜色、文本设置并重置到正常颜色。想更多了解tput,请参阅prompt-color-using-tput。
二、To print debug information (打印调试信息)
打印调试信息只需调试设置flag。
123456789function
debug() {
if
[[ $DEBUG ]]
then
echo
">>> $*"
fi
}
# For any debug message
debug
"Trying to find config file"
某些极客还会提供在线调试功能:
123# From cool geeks at hacker news
function
debug() { ((DEBUG)) && echo
">>> $*"
; }
function
debug() { [
"$DEBUG"
] && echo
">>> $*"
; }
三、To check specific executable exists or not (检查特定可执行的文件是否存在)123456789101112OK=0
FAIL=1
function
require_curl() {
which curl &>/dev/
null
if
[ $? -eq 0 ]
then
return
$OK
fi
return
$FAIL
}
这里使用which来命令查找可执行的curl 路径。如果成功,那么可执行的文件存在,反之则不存在。将&>/dev/null设置在输出流中,错误流会显示to /dev/null (这就意味着在控制板上没有任何东西可打印)。
有些极客会建议直接通过返回which来返回代码。
123# From cool geeks at hacker news
function
require_curl() { which
"curl"
&>/dev/
null
; }
function
require_curl() { which -s
"curl"
; }
四、To print usage of scripts (打印使用的脚本)
当我开始编写shell 脚本,我会用echo来命令打印已使用的脚本。当有大量的文本在使用时, echo命令会变得凌乱,那么可以利用cat来设置命令。
1234567891011121314151617cat << EOF
Usage: myscript <command> <arguments>
VERSION: 1.0
Available Commands
install - Install package
uninstall - Uninstall package
update - Update package
list - List packages
EOF
这里的<<被称为<<here document,字符串在两个EOF中。五、User configured value vs Default value (用户配置值VS 默认值)
有时,如果用户没有设置值,那么会使用默认值。
1URL=${URL:-http:
//localhost:8080}
检查URL环境变量。如果不存在,可指定为localhost。
六、To check the length of the string 检查字符串长度
12345if
[ ${
#authy_api_key} != 32 ]
then
red
"you have entered a wrong API key"
return
$FAIL
fi
利用 ${#VARIABLE_NAME} 定义变量值的长度。
七、To read inputs with timeout (读取输入超时)
12345678READ_TIMEOUT=60
read -t
"$READ_TIMEOUT"
input
# if you do not want quotes, then escape it
input=$(sed
"s/[;\`\"\$\' ]//g"
<<< $input)
# For reading number, then you can escape other characters
input=$(sed 's/[^0-9]*
//g' <<< $input)
八、To get directory name and file name (获取目录名和文件名)12345678# To find base directory
APP_ROOT=`dirname
"$0"
`
# To find the file name
filename=`basename
"$filepath"
`
# To find the file name without extension
filename=`basename
"$filepath"
.html`
作者Fizer Khan是一位Shell脚本迷,他对有关Shell脚本新奇有趣的东西是如此的痴迷。最近他遇到了authy-ssh脚本,为了缓解ssh服务器双重认证问题,他学到了许多有用且很酷的东西。对此,他想分享给大家。
一、Colors your echo
大多数情况下,你希望输出echo Color,比如绿色代表成功,红色代表失败,黄色代表警告。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 success
green
"Task has been completed"
# To print error
red
"The configuration file does not exist"
# To print warning
yellow
"You have to use higher version."
二、To print debug information (打印调试信息)
打印调试信息只需调试设置flag。
1
2
3
4
5
6
7
8
9
function
debug() {
if
[[ $DEBUG ]]
then
echo
">>> $*"
fi
}
# For any debug message
debug
"Trying to find config file"
1
2
3
# From cool geeks at hacker news
function
debug() { ((DEBUG)) && echo
">>> $*"
; }
function
debug() { [
"$DEBUG"
] && echo
">>> $*"
; }
1
2
3
4
5
6
7
8
9
10
11
12
OK=0
FAIL=1
function
require_curl() {
which curl &>/dev/
null
if
[ $? -eq 0 ]
then
return
$OK
fi
return
$FAIL
}
有些极客会建议直接通过返回which来返回代码。
1
2
3
# From cool geeks at hacker news
function
require_curl() { which
"curl"
&>/dev/
null
; }
function
require_curl() { which -s
"curl"
; }
当我开始编写shell 脚本,我会用echo来命令打印已使用的脚本。当有大量的文本在使用时, echo命令会变得凌乱,那么可以利用cat来设置命令。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cat << EOF
Usage: myscript <command> <arguments>
VERSION: 1.0
Available Commands
install - Install package
uninstall - Uninstall package
update - Update package
list - List packages
EOF
五、User configured value vs Default value (用户配置值VS 默认值)
有时,如果用户没有设置值,那么会使用默认值。
1
URL=${URL:-http:
//localhost:8080}
六、To check the length of the string 检查字符串长度
1
2
3
4
5
if
[ ${
#authy_api_key} != 32 ]
then
red
"you have entered a wrong API key"
return
$FAIL
fi
利用 ${#VARIABLE_NAME} 定义变量值的长度。
七、To read inputs with timeout (读取输入超时)1
2
3
4
5
6
7
8
READ_TIMEOUT=60
read -t
"$READ_TIMEOUT"
input
# if you do not want quotes, then escape it
input=$(sed
"s/[;\`\"\$\' ]//g"
<<< $input)
# For reading number, then you can escape other characters
input=$(sed 's/[^0-9]*
//g' <<< $input)
1
2
3
4
5
6
7
8
# To find base directory
APP_ROOT=`dirname
"$0"
`
# To find the file name
filename=`basename
"$filepath"
`
# To find the file name without extension
filename=`basename
"$filepath"
.html`
- 我从其他Shell脚本中学到了什么?
- 我从其他Shell脚本中学到了什么?
- 我从其他Shell脚本中学到了什么?
- 我从1万小时编程中学到了什么
- 我从1万小时编程中学到了什么
- 我从1万小时编程中学到了什么
- 我从其他人的Shell脚本中学到的
- 我从其他人的Shell脚本中学到的
- 我从其他人的Shell脚本中学到的
- 我从创立3家科技公司的经历中学到了什么
- 我从创立3家科技公司的经历中学到了什么
- 商界传奇Bill Gross:我从创办的100多家公司中学到了什么
- 我从创立3家科技公司的经历中学到了什么
- 我从55个Swift标准库协议中学到了什么?
- 我从55个Swift标准库协议中学到了什么?
- 我们从最近的黑客攻击中学到了什么?
- 我们从产品团队扩大中学到了什么
- 我们从产品团队扩大中学到了什么
- Tomcat:IOException while loading persisted sessions: java.io.EOFException解决手记
- 讲故事励志
- Unity3d WWW Get方式特殊字符的处理
- error: unrecognized command line option "-fconserve-stack"解决办法
- HMM模型/最大熵模型/CRF模型比较
- 我从其他Shell脚本中学到了什么?
- debian启动vncserver连接无法看到界面和启动ssh服务
- 没有装摄像头down软件 ICON不出来
- VC++中MCI播放音频文件
- linux 下DSP音频处理
- hibernate一级缓存和二级缓存的区别
- 浅谈Android中onInterceptTouchEvent与onTouchEvent(图文)
- 如何下载videolectures的视频
- Static ,const,Continue 和Break,位的操作的作用总结