Shell脚本备忘录

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 引用变量用法

  • 直接引用

    [root@localhost testShell]# x=1024
    [root@localhost testShell]# echo $x
    x
    
  • 利用双引号

    [root@localhost testShell]# x=1024
    [root@localhost testShell]# echo "x=$x"
    x=1024
    
  • 使用 ${ } 作为单词边界

    [root@localhost testShell]# x=1024
    [root@localhost testShell]# echo "x=${x}-123"
    x=1024-123
    
  • 使用 ${#} 获取变量字符串长度

    [root@localhost testShell]# x=1024
    [root@localhost testShell]# echo "length=${#x}"
    length=4
    

2.2 引用脚本或函数参数

  • 1 表示 Shell 脚本文件名,n 从 2 开始表示第 n 个参数,第 2 个参数是 $2

    [root@localhost testShell]# echo 'echo $1 $2 $3' > hello.sh
    [root@localhost testShell]# cat hello.sh 
    echo $1 $2 $3
    [root@localhost testShell]# sh hello.sh 1 2 3
    1 2 3
    
  • 使用 $# 获取脚本或函数参数的个数

    [root@localhost testShell]# echo 'echo $#' > hello.sh
    [root@localhost testShell]# cat hello.sh 
    echo $#
    [root@localhost testShell]# sh hello.sh 1 2 3 4 5 6 
    6
    

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. =赋值的时候,两边不能出空格,不然会被认为是命令

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值