zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Shell脚本备忘录

shell 脚本 备忘录
2023-06-13 09:12:25 时间

大家好,又见面了,我是你们的朋友全栈君。

目录

1. jq

jq可以对json数据进行分片、过滤、映射和转换,和sed、awk、grep等命令一样

1.1 安装

yum -y install jq

1.2 几个常用例子

以这个json结构为例子进行解析,假设文件命名为:json.txt

[{ 
   
	"name": "站长工具",
	"url": "http://tool.chinaz.com",
	"address": { 
   
		"city": "厦门",
		"country": "中国"
	},
	"arrayBrowser": [{ 
   
		"name": "Google",
		"url": "http://www.google.com"
	}, { 
   
		"name": "Baidu",
		"url": "http://www.baidu.com"
	}]
}, { 
   
	"name": "站长之家",
	"url": "http://tool.zzhome.com",
	"address": { 
   
		"city": "大连",
		"country": "中国"
	},
	"arrayBrowser": [{ 
   
		"name": "360",
		"url": "http://www.so.com"
	}, { 
   
		"name": "bing",
		"url": "http://www.bing.com"
	}]
}]

1.2.1 取出数组index=0的内容

cat json.txt | jq '.[0]'

1.2.2 取出数组index=0的name的内容

cat json.txt | jq '.[0].name'

1.2.3 以key-value的格式取出数组index=0的name和city

cat json.txt | jq '.[0] | {name:.name, city:.address.city}'

1.2.4 以key-value的格式取出所有数组的name和city

cat json.txt | jq '.[] | {name:.name, city:.address.city}'

1.2.5 以key-value的格式取出数组index=0的name和arrayBrowser的index=1的url

cat json.txt | jq '.[0] | {name:.name, url: .arrayBrowser[1].url}'

1.2.6 以key-value的格式取出所有数组的name和city并放在一个数组里(前后加上[])

cat json.txt | jq '[.[] | {name:.name, city:.address.city}]'

1.2.7 以key-value的格式取出所有数组的name和city并放在一个数组里并修改name为name2,city为city2

cat json.txt | jq '[.[] | {name2:.name, city2:.address.city}]'

2. $

  • $0 :Shell 的命令本身
  • $1-9 :表示 Shell 的第几个参数
  • $? :显示最后命令的执行情况
  • $# :传递到脚本的参数个数
  • $$ :脚本运行的当前进程 ID 号
  • $* :以一个单字符串显示所有向脚本传递的参数
  • $! :后台运行的最后一个进程的 ID 号
  • $- :显示 Shell 使用的当前选项
  • $(命令) :执行并获取命令输出

2.1 引用变量用法

2.2 引用脚本或函数参数

2.3 上条命令的返回值

使用 $? 上条命令的返回值。

0:表示没有错误,其他任何数值:表示有错误。

[root@localhost testShell]# echo 111 > 1.sh
[root@localhost testShell]# rm 2.sh
rm: cannot remove ‘2.sh’: No such file or directory
[root@localhost testShell]# echo $?
1
[root@localhost testShell]# rm 1.sh 
rm: remove regular file ‘1.sh’? y
[root@localhost testShell]# echo $?
0

2.4 执行并获取命令输出

[root@localhost testShell]# echo $(date)
Tue Aug 17 06:50:29 EDT 2021

2.5 获取当前进程 ID

[root@localhost testShell]# echo $$
80329

2.6 获取后台运行的最后一个进程 ID

[root@localhost testShell]# echo 'ping www.baidu.com' > ping.sh
[root@localhost testShell]# tail -f ping.sh &
[1] 88991
[root@localhost testShell]# echo $!
88991
[root@localhost testShell]# kill $!
[root@localhost testShell]# echo $!
88991
[1]+  Terminated              tail -f ping.sh

2.7 获取 Shell 选项

[root@localhost testShell]# echo $-
himBH

3. “

被`包含的内容会直接执行

4. 去掉字符串最外面双引号

echo "内容" | sed 's/\"//g'

5. debug模式

set -x

6. 字符替换

/要替换的字符串(只找第一个)/替换成的字符串 //要替换的字符串(全部替换)/替换成的字符串

[root@localhost testShell]# url=www.baiud.com
[root@localhost testShell]# echo ${net/baidu/google}
www.google.com
[root@localhost testShell]# echo ${net//./-}
www-baidu-com
[root@localhost testShell]# echo ${net/./-}
www-baidu.com

7. 字符串截取

#查找的字符串 %查找的字符串

[root@localhost testShell]# url=www.baiud.com
[root@localhost testShell]# echo ${url#*www.}
baidu.com
[root@localhost testShell]# echo ${url%*.com}
www.baidu

8. =赋值的时候,两边不能出空格,不然会被认为是命令

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/152901.html原文链接:https://javaforall.cn