工作中經(jīng)常需要用Shell寫一些腳本用于測試,但是每次寫shell枉侧,我都痛苦不已引瀑,因為shell的語法形式太雜亂,各種括號榨馁,各種$XX憨栽,搞得我每次寫完腳本,都得花大把時間解決語法格式錯誤翼虫。
為了解決這個問題屑柔,提高寫shell的效率,我把一些shell常用的demo蛙讥,總結(jié)出來锯蛀,方便我以及各位讀者查閱哈!
字符串連接
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:"
#起始下標:截取個數(shù)
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
}
執(zhí)行shell命令次慢,并將結(jié)果賦給變量
sh_set_val_according2exec_res() {
var=$(date)
echo $var
}
echo -e 開啟轉(zhuǎn)義字符
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" # 不再產(chǎn)生輸出
echo -e "OK \\\\\\\\b END" #執(zhí)行退格鍵
echo -e "OK \\\\\\\\v END \\\\\\\\v eee" #vertical tab
}
執(zhí)行命令旁涤,拋棄所有輸出
sh_exec_throw_output() {
ls >/dev/null 2>&1
}
$各種符號意義*
sh_dollar_means() {
echo "查看shell本身進程ID \\\\\\\\$\\\\\\\\$: $$"
echo "最后運行的后臺PID \\\\\\\\$!: $!"
echo "最后運行命令的結(jié)束碼 \\\\\\\\$?: $?"
echo "所有參數(shù)列表1 \\\\\\\\$*: $*"
echo "所有參數(shù)列表2 \\\\\\\\$@: $@"
echo "參數(shù)個數(shù) \\\\\\\\$\\\\\\\\#: $#"
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
#跟上一句的區(qū)別:直接修改原文件
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這兩條命令闻妓,請參考陳皓大神寫的相關文章~