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. OpenStack是什么,OpenStack详解

    1. OpenStack是什么 OpenStack官方的解释很官方,而且从不同角度,也有不同的理解,OpenStack可以理解为一个云操作系统 OpenStack旗下包含了一组由社区维护的开源项目,他 ...

  2. 关于C3P0-mySQL关于url的细节问题

    1.为url设置?useUnicode=true&characterEncoding=UTF-8 为了统一编码,我们会为数据库封装的实体类加上上面的那句话,但是C3P0数据库连接池是xml配置 ...

  3. machine learning (3)---Linear Algebra Review

    Matrix Vector Multiplication 左边的矩阵向量相乘法比右边的更简洁而且计算高效 Matrix Matrix Multiplication 可以同时计算12个结果(4个房子面积 ...

  4. 3. 控制反转(IoC)和依赖注入(DI)

    1).控制反转是应用于软件工程领域中的,在运行时被装配器对象来绑定耦合对象的一种编程技巧,对象之间耦合关系在编译时通常是未知的.在传统的编程方式中,业务逻辑的流程是由应用程序中的早已被设定好关联关系的 ...

  5. Eclipse中安装Spring IDE

    ====>在线安装 1.寻找Spring IDE插件更新地址:http://marketplace.eclipse.org/content/spring-ide 2.复制对应Eclipse版本的 ...

  6. HDU - 3709 - Balanced Number(数位DP)

    链接: https://vjudge.net/problem/HDU-3709 题意: A balanced number is a non-negative integer that can be ...

  7. Tensorflow细节-P42张量的概念及使用

    1.运行以下代码 import tensorflow as tf a = tf.constant([1.0, 2.0], name="a") b = tf.constant([2. ...

  8. DBA 有哪些工作

    首先,我们看看DBA的工作有哪些?DBA的工作实际上都是围绕数据库展开,包含但不限于这些工作: 1. 数据库.主机.操作系统.交换机.存储选型,预算,架构设计,部署,参数优化: 2. 数据库备份.恢复 ...

  9. piplinedb 团队加入confluen

    这个消息对于使用pipelinedb 的人来说,可能有点不好,因为官方已经明确说明了,pipelinedb 截止到1.0 版本,将不再维护了, 基本就要靠社区了,但是pipelinedb 团队还是比较 ...

  10. mybatis-helper

    mybatis-helper 在mapper层可以跳转到sql xml中 只需要在idea plugin中搜索即可.