shell脚本 查询转发接口、连接数据库、发送http请求

shell查询默认网卡,ip route和route -n的作用一样,都可以查出
在这里插入图片描述

nc=$(ip route | grep default | awk '{print $5}')     #查询默认网卡
  if [[ x${nc} == "x" ]]; then       #如果nc是否为空的,执行exit 1 退出
    bomb "find_nc_error"
  fi

function bomb(){
    echo $1
    exit 1
}

shell从指定文件里读取内容

file=/home/amber/read.txt
while read -r line  #从file文件中读取文件内容赋值给line(使用参数r会屏蔽文本中的特殊符号,只做输出不做转译)
do
 one=`echo $line  | awk '{print $1}' ` #从第一行读出的按空格分割后第一个字符赋值给text,注意这里分割从1开始
 two=`echo $line  | awk '{print $2}' `
done  < $file

shell修改文件内容

#sed -i 's/被替换的内容/要替换成的内容/' file (-i 直接修改并保存)
sed -i 's/first/second/' /home/amber/read.txt

shell连接数据库

# 这里注意=前后不需要空格
id=1e3d3sedfr23edsedswq1ef
name=Jack

mysql="mysql -uxxx -pxxx -hx.x.x.x -Pxx xxx"
# 注意当执行语句时要加``
user=`$mysql -e "select uuid from users where id='$id' and name='$name'"`

# 这里查出的内容是包含列名uuid所以要切割
uuid=`echo $port|awk '{print $2}'`
if [[ x${port_id} == x ]]; then
    bomb not_find_user_from_db
fi

shell发送http请求

id=1e3d3sedfr23edsedswq1ef
HEADER="-H Content-Type:application/json -H callbackurl:http://10.196.72.12:5000"
URL=10.196.72.12:8000/createUser
BODY=$(cat<< EOF
{
    "user": {
        "id": "",
        "name": "Jaclyn",
        "desc": "",
        "binding": {
            "uuid": "$id",
            "father": "Belly"
        	}
    	}
	}
EOF
)
echo $BODY
response=$(curl -i -X POST $HEADER -d "$BODY" $URL)

# 判断curl的返回内容里是否又status这个关键字
if [[ $response =~ "status" && $response =~ "true" ]];then
    echo $response
    echo "====create user success===="
else
    echo $response
    echo "====create user error=====" 
fi

请求http常见error: message=Syntax error: offset=70, error=invalid character ‘f’ after object key:value pair"

解决:发送的json格式不对,检查下key和value是否都加了"","$id"外面格式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章