shell编程系列3--命令替换
shell编程系列3--命令替换 命令替换 命令替换总结
方法1 `command`
方法2 $(command) 例子1:
获取系统的所有用户并输出 for循环能以空格、换行、tab键作为分隔符 [root@localhost shell]# cat example_1.sh
#!/bin/bash
# index=
for user in `cat /etc/passwd | cut -d ":" -f `
do
echo "this is $index user: $user"
index=$(($index + ))
done [root@localhost shell]# sh example_1.sh
this is user: root
this is user: bin
this is user: daemon
this is user: adm
this is user: lp
this is user: sync
this is user: shutdown
this is user: halt
this is user: mail
this is user: operator
this is user: games
this is user: ftp
this is user: nobody
this is user: systemd-network
this is user: dbus
this is user: polkitd
this is user: sshd
this is user: postfix
this is user: ajie
this is user: chrony
this is user: deploy 例子2:
根据系统时间计算今年或明年
echo "this is $(date +%Y) year"
echo "this is $(( $(date +%Y) + 1)) year" [root@localhost shell]# echo "This is $(date +%Y) year"
This is year
[root@localhost shell]# echo "This is $(($(date +%Y) + 1)) year"
This is year
[root@localhost shell]# echo "$((20+30))" 例子3:
根据系统时间获取今年还剩下多少星期,已经过了多少星期
# 今天是今年的第多少天
date +j # 今年已经过去了多少天
echo "this year have passed $(date +%j) days"
echo "this year have passed $(($(date +%j) / 7)) weeks" # 今年还剩余多少天
echo "there is $((365 - $(date +%j))) days before new year"
echo "there is $(((365 - $(date +%j)) / 7 )) weeks before new year" 总结:
``和$()两者是等价的,但推荐初学者使用$(),易于掌握;缺点是极少数UNIX可能不支持,但``两者都支持
$(())主要用来进行整数运算,包括加减乘除,引用变量前面可以加$,也可以不加$ $(( ( + ) / ))
num1=;num2=
((num++));
((num--));
$(( $num1 + $num2 * )) 语法不是很严格 是否加$都会计算
[root@localhost shell]# num1=
[root@localhost shell]# num2=
[root@localhost shell]# echo "$(($num1 + $num2))" [root@localhost shell]# echo "$((num1 + num2))" .例子4:
判断nginx进程是否存在,如果没有需求拉起这个进程 [root@localhost shell]# cat example_3.sh
#!/bin/bash
# nginx_process_num=$(ps -ef|grep nginx|grep -v grep|wc -l) if [ $nginx_process_num -eq ];then
systemctl start nginx
fi
shell编程系列3--命令替换的更多相关文章
- (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令
原文(C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:( ...
- shell编程系列23--shell操作数据库实战之mysql命令参数详解
shell编程系列23--shell操作数据库实战之mysql命令参数详解 mysql命令参数详解 -u 用户名 -p 用户密码 -h 服务器ip地址 -D 连接的数据库 -N 不输出列信息 -B 使 ...
- shell编程系列1--shell脚本中的变量替换
shell编程系列1--shell脚本中的变量替换 变量替换总结: .${变量#匹配规则} # 从头开始匹配,最短删除 .${变量##匹配规则} # 从头开始匹配,最长删除(贪婪模式) .${变量%匹 ...
- shell编程系列12--文本处理三剑客之sed利用sed修改文件内容
shell编程系列12--文本处理三剑客之sed利用sed修改文件内容 修改命令对照表 编辑命令 1s/old/new/ 替换第1行内容old为new ,10s/old/new/ 替换第1行到10行的 ...
- shell编程系列9--文本处理三剑客之sed概述及常见用法总结
shell编程系列9--文本处理三剑客之sed概述及常见用法总结 sed的工作模式:对文本的行数据一行行处理,如下图 sed(stream editor),是流编辑器,依据特定的匹配模式,对文本逐行匹 ...
- shell编程系列4--有类型变量:字符串、只读类型、整数、数组
shell编程系列4--有类型变量:字符串.只读类型.整数.数组 有类型变量总结: declare命令和typeset命令两者等价 declare.typeset命令都是用来定义变量类型的 decla ...
- C#)Windows Shell 编程系列5 - 获取图标
原文 C#)Windows Shell 编程系列5 - 获取图标 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows Shell 编程系列4 - 上下 ...
- (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单
原文 (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单 接上一节:(C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开这 ...
- shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中
shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...
随机推荐
- 网站检测空链、死链工具(Xenu)
网站常用检测空链.死链工具,Xenu是很小但是功能强大的检查网站404链接的软件,支持多线程,无需安装可直接打开使用.步骤如下: 网站的链接一般都成千上万,如果存在大量的空链接将大大的影响用户体验,怎 ...
- 《hello-world》第九次团队作业:【Beta】Scrum meeting 1
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十三 团队作业9:Beta冲刺与团队项目验收 团队名称 <hello--wor ...
- 《奋斗吧!菜鸟》 第九次作业:Beta冲刺 Scrum meeting 1
项目 内容 这个作业属于哪个课程 任课教师链接 作业要求 https://www.cnblogs.com/nwnu-daizh/p/11056511.html 团队名称 奋斗吧!菜鸟 作业学习目标 掌 ...
- 《CoderXiaoban》第八次团队作业:Alpha冲刺1
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 Coderxiaoban团队 作业学习目标 (1)掌握软件测试基 ...
- Oracle数据库的分页
Oracle的分页 ORACLE支持一个关键字ROWNUM,ROWNUM是一个伪列,该列不存在于任何一张表中,但是每张表都可以查询该列. 而该列在结果集的中值是结果集中每条记录的"行号&qu ...
- nginx添加系统服务(start|stop|restart|reload)
nginx添加系统服务 1.编写脚本,名为nginx #vim /etc/init.d/nginx #!/bin/bash#chkconfig: - 99 20 #description: Nginx ...
- python - ORM 查询
1. 正常查询: ## 效率低,因为每次查询都是查询表和关联表的所有数据 ret = User.objects.all() for item in ret: print(item.name,item. ...
- house买房原理,2019,第一版
,购买框架 1,通过自己的买房预算金额 和 pre-approval 确定你要的房屋总价, 估计到自己可以接受的房子,卖方也喜欢这样的买家,但不一定能拿全额贷款 2,pre-approval对信用分数 ...
- EasyUI日期时间框DateTimeBox
WEB DEMO 日期时间框 DateTimeBox <!DOCTYPE html> <html> <HTML> <head> <HEAD> ...
- flutter踩坑小记:The number of method references in a .dex file cannot exceed 64K.
The number of method references in a .dex file cannot exceed 64K. 这句话的意思翻译出来是:.dex文件中的方法引用数不能超过64K. ...