shell 开发总结
1、if …else…语法结构
ret=`ls *`
if [[ $ret =~ "hello"]];then
echo "hell world"
return
else
echo "are you ok?"
return
fi
2、函数书写格式
function test(){
cd /opt
str=$"\n"
nohup ./xxx.sh start &
sstr=$(echo -e $str)
echo $sstr
sleep 15
return
}
test
3、自动化键盘输入expect
yum install expect -y
/usr.bin/expect <<EOF
span yum install npm
expect {*Is this ok [y/d/N]:*}
send "y\r"
sleep 20
exit
EOF
4、获取服务器IP
ip a show|grep -w inet |grep -w 180*|awk '{print $2}'|awk -F '/' '{print $1}'
ip a show|grep -w inet |grep -w 180*|awk '{print $2}'
5、sed替换
sed -i "s#user=bob#user=judy#g" a.txt
6、 2>&1
nohup ./xxx.sh start 2>&1 >log &
nohup ./xxx.sh start &>log &
7、curl访问网站
curl -l http://127.0.0.1:8000