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. STL详细介绍(更新中~~~)

    目录 string string的常见构造函数 string与char *(或const char*)之间的转换 string 转化为const char* const char* 转化为string ...

  2. k8s安装之kube-state-metrics.yaml

    概述 已经有了cadvisor.heapster.metric-server,几乎容器运行的所有指标都能拿到,但是下面这种情况却无能为力: 我调度了多少个replicas?现在可用的有几个? 多少个P ...

  3. 《发际线总是和我作对》第九次团队作业:【Beta】Scrum meeting1

    项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十三 团队作业9:Beta冲刺与团队项目冲刺 团队名称 发际线总和我作队 作业学习目标 (1)掌握软件黑盒测试技术:(2)掌握软件 ...

  4. CTSC2010 珠宝商

    珠宝商 题目描述 Louis.PS 是一名精明的珠宝商,他出售的项链构造独特,很大程度上是因为他的制作方法与众不同.每次 Louis.PS 到达某个国家后,他会选择一条路径去遍历该国的城市.在到达一个 ...

  5. JDK5不定参数方法

    jdk5之后出现了调用方法时传递不定参数的情况,如下所示: List<Intrger> list1 = Arrays.asList(new Integer[]{1,2,3}); List& ...

  6. 从零实现jQuery的extend

    前言 jQuery 的 extend 是 jQuery 中应用非常多的一个函数,今天我们一边看 jQuery 的 extend 的特性,一边实现一个 extend! extend 基本用法 先来看看 ...

  7. 判断 List map set 是否为空

    //如果object为null,则设置为defaultValue ObjectUtils.defaultIfNull(object, defaultValue); //判断集合是否为null List ...

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

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

  9. MySQL-linux系统下面的配置文件

    一般linux 上都放在 /etc/my.cnf   ,window 上安装都是默认可能按照上面的路径还是没找到, window 上  可以登录到mysql中  使用   show variables ...

  10. python 之 字符串的常用方法

    split()函数: 返回以指定的字符将字符串分割成多个元素的列表 my_str = 'name is wangxiaoming' print(my_str.split()) #默认不写参数表示按空格 ...