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. 剑指Offer(二十三):二叉搜索树的后序遍历序列

    剑指Offer(二十三):二叉搜索树的后序遍历序列 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

  2. wireshark 抓包再利用TCP socket发送包里的payload是可以实现登陆的

    用户密码可被批量破解 在用户使用手机端登录时,对数据进行抓包分析. 多次抓包分析后,可得到几个关键TCP数据包. 根据前面逆向编写出的解密算法,使用socket进行数据发包测试: 可以模拟APK进行用 ...

  3. RSA 加密 解密 (长字符串) JAVA JS版本加解密

    系统与系统的数据交互中,有些敏感数据是不能直接明文传输的,所以在发送数据之前要进行加密,在接收到数据时进行解密处理:然而由于系统与系统之间的开发语言不同. 本次需求是生成二维码是通过java生成,由p ...

  4. test20190805 夏令营NOIP训练20

    100+0+0=100,由于第二题写挂rank 1就没了 山 xyz现在站在一个斜坡面前 这个斜坡上依次排布这n座山峰,xyz打算爬上其中的一座 因为xyz体力不好,所以他只能爬上最矮的一座山 又因为 ...

  5. onclick与click的区别

    用法: Obj.click(function(){ }); Obj.onclick=function(){ } 相同:效果一样. 区别: 用户或浏览器执行的某种动作,例如click load,mous ...

  6. 八.Protobuf3更新消息类型(添加新的字段)

    Protobuf3 更新消息类型 如果现有的消息类型不满足你的所有需求——例如,你希望消息格式有一个额外的字段——但是你仍然希望使用用旧格式创建的代码,别担心!在不破坏任何现有代码的情况下更新消息类型 ...

  7. 关于JS变量和作用域详解

    ECMAScript 变量: 1.基本类型值(简单数据段) 2.引用类型值(可能由过个值构成的对象) → 保存在内存中的对象 ------ 动态属性: 只能给引用型值动态添加新属性,以便将来使用. - ...

  8. 学到了林海峰,武沛齐讲的Day37 完

    day1   多用户同时刻下载上传程序分析 day2   htlm介绍 觉得收货的季节到了 day3   htlm介绍 day4   htlm介绍 关键字介绍  Toray大仙 Toray大仙 day ...

  9. redis详解(包含使用场景)

    本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么这么快4.redis的数据类型,以及每种数据类型的使用场景5.redis的过期策略以及内存淘汰 ...

  10. GreenPlum failover,primary和mirror切换实验 -- 重要

    GP failover,primary和mirror切换实验 http://blog.sina.com.cn/s/blog_9869114e0101k1nc.html 一.恢复失败的segment出现 ...