1.shell脚本(filename.sh)
#!/bin/bash
curl http://ip:port/url -X POST -d '{"param1":"param1","param2":"param2"}' --header "Content-Type: application/json"
2.linux下执行.sh脚本
# 转换格式-使用sed命令
sed -i "s/\r//" filename.sh
# 添加权限
chmod +x ./filename.sh
# 执行shell脚本
./filename.sh
3.shell相关操作
1)将字符串"hello world"写入文件file.txt中
echo "hello world" > file.txt
2)Linux shell 获得字符串所在行数及位置的方法
(1)用sed -n '/查询的字符串/=' 文件
例如:
[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]#
[root@root]# sed -n '/exe/=' test
5
(2)用grep -n
例如:
[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]# cat test | grep -n exe
5:exe
[root@root]# cat test | grep -n exe | awk -F ":" '{print $1}'
5
3)判断字符串是否为空
if[-z "$str"] (-n为非空)
4)查文件第几行到第几行的内容
用sed命令
sed -n ‘5,10p’ filename
这样你就可以只查看文件的第5行到第10行。
5)按照空格分割字符串
STR="123 A1 A2"
ARR=($STR)
echo "${ARR[0]}:${ARR[1]}:${ARR[2]}
6)字符串截取
(1) 使用 # 号截取右边字符
使用#号可以截取指定字符(或者子字符串)右边的所有字符,具体格式如下:
${string#*chars}
例如:string="var=hhh"
${string#*var} 结果为hhh
(2) %号截取,删除右边字符,保留左边字符
例如:http://www.aaa.com/
%/* 表示从右边开始,删除第一个 / 号及右边的字符
echo ${var%/*}
结果是:http://www.aaa.com
%%/* 表示从右边开始,删除最后(最左边)一个 / 号及右边的字符
echo ${var%%/*}
结果是:http:
7)字符串替换
sed -i "s|data_collector_path|${workPath}|g" data-collector-envVar.sh