shell编写笔记

1.写入、替换配置文件shell

# 替换
node114:~/test # sed -i "/redis.addr/s/.*/redis.addr=127.0.0.1/" ./sed-test
# 插入最后一行 node114:
~/test # echo "redis.target=66" >> sed-test
# 替换指定字符串开头的行 避免替换错误
sed -ri "/^port=/s/.*/port=3306/g" /etc/my.cnf

效果:

node114:~/test # tail -f sed-test 
redis.addr=192.168.51.117
redis.port=6379
redis.auth=Xunmeizongmu_2019
redis.target=55
^C
node114:~/test # tail -f sed-test 
redis.addr=127.0.0.1
redis.port=6379
redis.auth=Xunmeizongmu_2019
redis.target=55
redis.target=66

2.判断是否存在文件夹、是否存在文件、是否为空

if [[ -f "/root/test/sed-test" ]]; then
    echo "存在 /root/test/sed-test文件"
else
    echo "不存在 /root/test/sed-test文件"
fi

if [[ -d "/root/test" ]]; then
    echo "存在 /root/test文件夹"
else
    echo "不存在 /root/test文件夹"
fi

if [[ -n "str" ]]; then
    echo "不为空"
else
    echo "为空"
fi

效果:

node114:~/test # ./shfile 
存在 /root/test/sed-test文件
存在 /root/test文件夹

 

3.判断上一个命令是否执行成功

mysql -h 192.168.51.115 -u root -pXM_zm2019 -e "show master status;"
if [ $? -ne 0 ];then
    echo "失败"
else
    echo "成功"
fi

结果:

node117:/home # ./t
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+--------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                          |
+------------------+----------+--------------+------------------+--------------------------------------------+
| mysql-bin.000003 |   172890 |              |                  | 0dd4f579-cf47-11ec-8419-00155d0a3233:1-493 |
+------------------+----------+--------------+------------------+--------------------------------------------+
成功
node117:/home # 

 

4.延迟几秒

sleep 30s

 

posted @ 2022-05-12 16:42  官萧何  阅读(33)  评论(0)    收藏  举报