工作中经常需要用Shell写一些脚本用于测试,但是每次写shell,我都痛苦不已,因为shell的语法形式太杂乱,各种括号,各种$XX,搞得我每次写完脚本,都得花大把时间解决语法格式错误。
为了解决这个问题,提高写shell的效率,我把一些shell常用的demo,总结出来,方便我以及各位读者查阅哈!
字符串连接
sh_strcat() {
#method 1
local str="strcat"
local str2="helo"
echo "strcat method1: ${str} ${str2}"
#method 2
echo "strcat method2:" ${str} ${str2}
}
获取字符串长度
sh_strlen() {
local str="strlen"
echo "strlen: ${#str}"
}
截取字符串
sh_substr() {
local substr="hello Meizu!"
echo -n "substr:"
#起始下标:截取个数
echo ${substr:0:5}
echo ${substr:6:5}
}
if-else语句格式
sh_if_else() {
local min=10
local max=10
# -gt -lt -le -ge
#if [ ${min} -lt ${max} ]; then
if ((${min} < ${max})); then
echo "min < max"
#elif [ ${min} -gt ${max} ]; then
elif ((${min} > ${max})); then
echo "min > max"
else
echo "min = max"
fi
}
for语句格式
sh_for_ctrl_fmt() {
echo "sh_for_ctrl_format:"
#for((i=0; i<10; i++)); do
for i in `seq 10`; do
echo $i
done
}
while语句格式
sh_while_fmt() {
local var=10
while ((${var} > 0)); do
var=$((var-1))
echo $var
done
}
执行shell命令,并将结果赋给变量
sh_set_val_according2exec_res() {
var=$(date)
echo $var
}
echo -e 开启转义字符
sh_echo_e() {
echo "echo -e:"
echo -e "OK \\\\\\\\t END" #horizonal tab
echo -e "OK \\\\\\\\n END" # 换行
echo -e "OK ccc \\\\\\\\c ccc END" # 不再产生输出
echo -e "OK \\\\\\\\b END" #执行退格键
echo -e "OK \\\\\\\\v END \\\\\\\\v eee" #vertical tab
}
执行命令,抛弃所有输出
sh_exec_throw_output() {
ls >/dev/null 2>&1
}
$各种符号意义*
sh_dollar_means() {
echo "查看shell本身进程ID \\\\\\\\$\\\\\\\\$: $$"
echo "最后运行的后台PID \\\\\\\\$!: $!"
echo "最后运行命令的结束码 \\\\\\\\$?: $?"
echo "所有参数列表1 \\\\\\\\$*: $*"
echo "所有参数列表2 \\\\\\\\$@: $@"
echo "参数个数 \\\\\\\\$\\\\\\\\#: $#"
echo "当前脚本文件名 \\\\\\\\$0: $0"
}
四则运算
sh_operator() {
echo "sh_operator:"
local num1=10
local num2=10
#res=`expr 2 \\\\\\\\* ${num1}`
#echo ${res}
local result_add=$((${num1} + ${num2}))
echo "add: "$result_add
local result_minus=$((${num1} - ${num2}))
echo "minus: "$result_minus
local result_multi=$((${num1} * ${num2}))
echo "multi: "$result_multi
local result_divide=$((${num1} / ${num2}))
echo "divide: "$result_divide
local result_mod=$((${num1} % ${num2}))
echo "mod: "$result_mod
#######################################
let result_add++
echo "result_add++: "${result_add}
let ++result_add
echo "++result_add: "${result_add}
let result_add+=5
echo "result_add+=5: "${result_add}
let result_add--
echo "result_add--: "${result_add}
let --result_add
echo "--result_add: "${result_add}
let result_add-=5
echo "result_add-=5: "${result_add}
}
sh_random
sh_random() {
local random_num=`awk 'BEGIN{srand();print int(rand()*1000)}'`
echo "random num: ${random_num}"
}
EOF INPUT
sh_EOF_INPUT() {
ncat $ip 6600 << EOF
seekid $position $random_num
EOF
}
strcmp && ||
# -a ==> 逻辑与
# -o ==> 逻辑或
# ! ==> 逻辑非
sh_strcmp() {
echo "sh_strcmp:"
local str1='y'
local str2='z'
#if (( "x${str1}" == "xy" && "x${str2}" == "xz" ));then
#if [ "x${str1}" == "xy" -a "x${str2}" == "xz" ];then
if [[ "x${str1}" == "xy" && "x${str2}" == "xz" ]];then
echo "sh_strcmp: && success"
else
echo "sh_strcmp: && failed"
fi
local flag=0
#if [ ! ${flag} == 0 ]; then
if (( ! ${flag} == 0 )); then
echo "! success"
else
echo "! failed"
fi
#if (( -d "/etc/zsh" && -e /etc/zsh/newuser.zshrc.recommended )); then
if [ -d "/etc/zsh" -a -e /etc/zsh/newuser.zshrc.recommended ]; then
echo "exist"
else
echo "not exist"
fi
}
sh_switch_case
sh_switch_case() {
echo "sh_switch_case:"
case "$1" in
start) echo "case $1";;
stop) echo "case $1";;
install) echo "case $1";;
remove) echo "case $1";;
upgrade) echo "case $1";;
esac
}
输出pwd
sh_print_pwd() {
echo "sh_print_pwd:"
dirname=$(cd "$(dirname "$0")";pwd)
echo "dirname: "${dirname}
}
awk抓取html
sh_awk_spider_html() {
echo "sh_awk_spider_html:"
awk -F'<div class="content"|/ul>' '{for(i=1;i<=NF;i++){if($i~/class="play-img"/){print $i}}}' \\\\\\\\
${bt_spider_web_path}plug_xunbo.html|grep -a "h2"|grep -a "${bt_detail_url}" |tail -1 > ${bt_spider_log}xunbo_${kind}_classify.log
awk -F'<a|/>' '{for(i=1;i<=NF;i++){if($i~/alt/){print $i}}}' ${bt_spider_log}xunbo_${kind}_classify.log > ${bt_spider_log}classify_${kind}_info.log
awk -F'[""]' '{print $4}' ${bt_spider_log}classify_${kind}_info.log > ${bt_spider_log}bt_${kind}_detail_link.log
awk -F'[""]' '{print $6}' ${bt_spider_log}classify_${kind}_info.log > ${bt_spider_log}bt_${kind}_name.log
awk -F'[""]' '{print $10}' ${bt_spider_log}classify_${kind}_info.log > ${bt_spider_log}bt_${kind}_pic.log
}
sed基本使用
#查询hello关键字,打印相应行
sed -n '/hello/p' cron.txt
#删除含hello的行,输出到屏幕(原文件没删除)
sed '/hello/d' cron.txt
#跟上一句的区别:直接修改原文件
sed -i '/hello/d' cron.txt
#移除空白行:
sed -i '/^$/d' file
#在包含hello关键字的指定行前加#号,注释该行:
sed -i '/hello/s/^/#/g' /etc/crontabs/root
#取消#号,取消注释:
sed -i '/hello/s/^#//' /etc/crontabs/root
如果想了解更多awk和sed这两条命令,请参考陈皓大神写的相关文章~
网友评论