Shell脚本备忘录

Shell脚本备忘录目录1.jq1.1安装1.2几个常用例子1.2.1取出数组index=0的内容1.2.2取出数组index=0的name的内容1.2.3以key-value的格式取出数组index=0的name和city1.2.4以key-value的格式取出所有数组的name和city1.2.5以key-value的格式取出数组index=0的name和arrayBrowser的index=1的url1.2.6以key-value的格式取出所有数组的name和city并放在一个数组里(前后加上[])1.

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • html怎么动态获取系统时间_代码实现获取当前的地理位置

    html怎么动态获取系统时间_代码实现获取当前的地理位置HTML+JS动态获取当前时间效果图:说明:JavaScript中Date对象创建Date对象的语法:varmyDate=newDate()常用Date对象方法:方法描述Date()返回当日的日期和时间getDate()从Date对象返回一个月中的某一天(1~31)getDay()从Date对象返回一周中的某…

    2022年9月23日
    0
  • 自动化运维平台(后端python+django)「建议收藏」

    自动化运维平台(后端python+django)「建议收藏」Django的MTV模式Django的MTV模式本质上和MVC是一样的,也是为了各组件间保持松耦合关系,只是定义上有些许不同,Django的MTV分别是值:M代表模型(Model):负责业务对象和数据库的关系映射(ORM)。T代表模板(Template):负责如何把页面展示给用户(html)。V代表视图(View):负责业务逻辑,并在适当时候调用Model和Tem…

    2022年5月16日
    44
  • redis windons安装教程

    redis windons安装教程redis windons安装教程

    2022年4月24日
    38
  • [RoCE]Flow Control

    [RoCE]Flow Control概览RoCE可以实现lossless无损网络环境,在二层网络上做到可靠网络传输,从而对原本在光纤网络环境下的应用在以太网环境下提供相同的服务,而不必对应用逻辑和上层协议更改。实现无损的方法有GlobalPause,PFC,DroplessReceiveQueue。1.什么是802.3xFlowControl(GlobalPause)?以太网标准(802.3)设计时是不可…

    2022年5月20日
    50
  • spring jar包 以及 jdbcTemplate 相关jar包下载[通俗易懂]

    spring jar包 以及 jdbcTemplate 相关jar包下载[通俗易懂]下面是阿帕奇官网下载spring相关的jar包链接:http://commons.apache.org/proper/commons-logging/download_logging.cgi如果你是要找jdbcTemplate相关jar包,下面是网盘分享。若是文件里没有你想要的jar包,可以点击上面的链接找到你想要的jar包。链接:https://pan.baidu.com/s/1…

    2022年5月14日
    43
  • 一分钟速算口诀_速算口诀表完整版

    一分钟速算口诀_速算口诀表完整版“一分钟速算口诀”:两位数相乘,在十位数相同、个位数相加等于10的情况下,如62×68=4216计算方法:6×(6+1)=42(前积),2×8=16(后积)。一分钟速算口诀中对特殊题的定理是:任意两位数乘以任意两位数,只要魏式系数为“0”所得的积,一定是两项数中的尾乘尾所得的积为后积,头乘头(其中一项头加1的和)的积为前积,两积相邻所得的积。如(1)33×46=1518(个位数相加小于10,所以十

    2022年10月23日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号