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"外面格式

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