在linux环境中我怎么才将以下内容添加到optoperatesh脚本中第四行注意不能删除原脚本中第四行内容。需要添加的内容:# 检测服务端口是否可用# 定义变量server_ip=localhostserver_port=9040check_url=http$server_ip$server_porthealthInforesult=$curl --connect-timeout 3 $chec
使用sed命令可以实现在指定行添加内容,不影响原文件的内容。具体操作如下:
-
打开/opt/*/operate.sh文件,找到第四行的内容,记录其行号。
-
执行以下命令,将需要添加的内容写入一个临时文件temp.txt:
echo -e '# 检测服务端口是否可用\n# 定义变量\nserver_ip="localhost"\nserver_port="9040"\ncheck_url="http://${server_ip}:${server_port}/healthInfo"\nresult=$(curl --connect-timeout 3 "${check_url}" 2>&1)\nTRUEINFO=$(echo $result | sed 's/<\/br>/\n/g')\nERROINFO=$(echo $result | sed 's/.*Connection timed out.*/警告:$APP_NAME 服务端口检测超时,请排查!/')\ncurl -H "Content-Type: application/json;charset=utf-8" -d "{\"msgtype\": \"markdown\",\"markdown\": {\"content\":\"# [开发环境]-[JAVA后端]-[f存活探测]\n >## 请注意:过量的变更内容会导致输出为空.\n$TRUEINFO \"}}" "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=04a90303-2a83-4fd3-b315-9b12cf25484a"\ncurl -H "Content-Type: application/json;charset=utf-8" -d "{\"msgtype\": \"markdown\",\"markdown\": {\"content\":\"# [开发环境]-[JAVA后端]-[f存活探测]\n >## 请注意:过量的变更内容会导致输出为空.\n$ERRINFO \"}}" "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=04a90303-2a83-4fd3-b315-9b12cf25484a"' > temp.txt
- 执行以下命令,在第四行之前插入temp.txt文件中的内容:
sed -i '4 r temp.txt' /opt/*/operate.sh
- 删除临时文件:
rm temp.txt
执行完以上步骤后,需要添加的内容就会被插入到/opt/*/operate.sh文件的第四行之前。
原文地址: https://www.cveoy.top/t/topic/biMD 著作权归作者所有。请勿转载和采集!