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--命令替换的更多相关文章

  1. (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令

    原文(C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:( ...

  2. shell编程系列23--shell操作数据库实战之mysql命令参数详解

    shell编程系列23--shell操作数据库实战之mysql命令参数详解 mysql命令参数详解 -u 用户名 -p 用户密码 -h 服务器ip地址 -D 连接的数据库 -N 不输出列信息 -B 使 ...

  3. shell编程系列1--shell脚本中的变量替换

    shell编程系列1--shell脚本中的变量替换 变量替换总结: .${变量#匹配规则} # 从头开始匹配,最短删除 .${变量##匹配规则} # 从头开始匹配,最长删除(贪婪模式) .${变量%匹 ...

  4. shell编程系列12--文本处理三剑客之sed利用sed修改文件内容

    shell编程系列12--文本处理三剑客之sed利用sed修改文件内容 修改命令对照表 编辑命令 1s/old/new/ 替换第1行内容old为new ,10s/old/new/ 替换第1行到10行的 ...

  5. shell编程系列9--文本处理三剑客之sed概述及常见用法总结

    shell编程系列9--文本处理三剑客之sed概述及常见用法总结 sed的工作模式:对文本的行数据一行行处理,如下图 sed(stream editor),是流编辑器,依据特定的匹配模式,对文本逐行匹 ...

  6. shell编程系列4--有类型变量:字符串、只读类型、整数、数组

    shell编程系列4--有类型变量:字符串.只读类型.整数.数组 有类型变量总结: declare命令和typeset命令两者等价 declare.typeset命令都是用来定义变量类型的 decla ...

  7. C#)Windows Shell 编程系列5 - 获取图标

    原文 C#)Windows Shell 编程系列5 - 获取图标 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows Shell 编程系列4 - 上下 ...

  8. (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单

    原文 (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单 接上一节:(C#)Windows Shell 编程系列2 - 解释,从“桌面”开始展开这 ...

  9. shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中

    shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...

随机推荐

  1. 163data.com.cn data

    163data.com.cn是什么?终于搞清楚了...   查看文章     163data.com.cn是什么?终于搞清楚了... 2008-05-31 00:41 一场误会,真TN的无聊的吓人从日 ...

  2. Webpack快速入门

    什么是Webpack 顾名思义它是一个前端打包工具,通过给定的入口文件自动梳理所有依赖资源(包括css.图片.js等),并按照配置的规则进行一系列处理(转es5.压缩等),打包生成适合现代生产环境要求 ...

  3. C++处理异常

    C++处理异常

  4. service worker(二)之主页面与service worker通信

    实现一个主页面发送消息,worker搜到信息向所有的页面派发消息(当前页面除外) msg.html <!DOCTYPE html> <html lang="en" ...

  5. 学习Spring-Data-Jpa(四)---Naming命名策略,源码跟踪

    1.首先在Entity实体中,命名方式有两种: 一种是显示命名,即通过@Table的name属性指定对应的数据库表名称,@Column的name属性指定实体字段对应数据库字段的名称. 另一种是隐式命名 ...

  6. mysqldump 原理(转载)

    mysqldump 备份过程可以描述为: (1) 先发出一条 flush tables 关闭实例上所有打开的表(2) 创建一个全局锁,FLUSH TABLES WITH READ LOCK获得 db ...

  7. Oracle CAST() 函数 数据类型的转换

    CAST()函数可以进行数据类型的转换. CAST()函数的参数有两部分,源值和目标数据类型,中间用AS关键字分隔. 以下例子均通过本人测试. 一.转换列或值 语法:cast( 列名/值 as 数据类 ...

  8. (尚008)Vue条件渲染

    1.test008.html <!DOCTYPE html><html lang="en"><head> <meta charset=&q ...

  9. C# Dictionary使用

    Dictionary<string, int> illegParking = new Dictionary<string, int>(); 键:inData.LOTID 值:i ...

  10. Comet OJ Contest #13 简要题解

    C2 首先用并查集维护\(1\)的连通块,然后用另外一个并查集维护第\(i\)行中,第\(j\)列之后的第一个\(0\)的位置,就是如果当前位置是\(1\)那么它的父亲是它右边的格子,否则是它自己. ...