shell里的` ` $( ) ${ } expr $(( ))
转自:http://blog.sina.com.cn/s/blog_6151984a0100ekz2.html
所有UNIX命令,要取结果或输出,都要用$( )或反引号` `
|
tt=` file test.sh ` echo $tt |
|
#sh test.sh test.sh: ASCII text |
| tar -zcvf lastmod.tar.gz `find . -mtime -1 -type f -print` |
| 将过去24小时(-mtime –2则表示过去48小时)内修改过的文件tar在一起 |
反引号` `和$(
)缺陷也相同,没回车换行,容易把多行合并
|
[mac@machome ~]$ vi test.sh echo `ps -ef | grep syslog` |
|
[mac@machome ~]$ sh test.sh root 1363 1 0 12:15 ? 00:00:00 syslogd -m 0 mac 15032 15030 0 18:13 pts/2 00:00:00 grep syslog |
|
[macg@machome ~]$ vi test.sh var=$(ls -l) echo $var |
|
[macg@machome ~]$ sh test.sh total 44 -rw-rw-r-- 1 macg macg 126 Jun 8 23:40 1 -rw-rw-r-- 1 macg macg 0 Jun 8 17:53 100 -rw-rw-r-- 1 macg macg 16 Jun 9 19:00 111.txt -rw-rw-r-- 1 macg macg 15 Jun 9 19:00 22.txt -rw-rw-r-- 1 macg macg 15 Jun 9 19:00 33.txt -rw-rw-r-- 1 macg macg 23 Jun 12 19:20 test.sh |
wc-l常与$(
)合用取其输出,计算行数,进而确定循环次数
|
count=$(more iftmp|wc -l) while [ $i -le $count ] |
echo
+$( ) ――echo内也可执行命令,并通过$( )取该命令的输出再显示
常见使用:echo内,用$( )取awk和sed对某些命令修饰后的效果
|
[macg@machome ~]$ vi test #choose a iinterface to config /sbin/ifconfig -a | awk '$2=="Link" {print($1,$3)}' >iftmp echo "choose one of interface to config" i=1 count=$(more iftmp|wc -l) while [ $i -le $count ] do echo "$i)-------- $(sed -n "${i}p" iftmp)" 用sed 修饰输出 ii=$(($i+1)) done |
|
[macg@machome ~]$ sh test choose one of interface to config 1)-------- eth0 encap:Ethernet 2)-------- lo encap:Local 3)-------- sit0 encap:IPv6-in-IPv4 give the interface a host name[such as:xxx-hme0]: |
${
}和$( )的区别:${ } 是做什么用的? --------避免在echo中变量被连读
${i}是区分其后紧接的字符串,避免“连读”
|
$ vi ttt.sh GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|" GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|" echo $GONE |
|
$ sh ttt.sh (ServerType|BindAddress|Port|AddModule|ClearModuleList|AgentLog|RefererLog|RefererIgnore|FancyIndexing |
${i}可以在任何场合替代普通变量$i
|
ttt=${REMOTEHOST} echo ${ttt} |
ttt=$REMOTEHOST echo $ttt |
|
[macg@localhost ~]$ sh ttt.sh 192.168.1.11 |
[macg@localhost ~]$ sh ttt.sh 192.168.1.11 |
expr 数学表达式
shell语言里没有运算公式,必须借助expr语句实现:
| expr 变量 ‘+-x /’ 变量 | expr $a '+' $b |
| expr 常数 ‘+-x /’ 常数 |
[mac@machome ~]$ expr 3 '+' 2 5 |
运算符号都要加引号,单引号双引号均可
输出是结果
为什么if
expr 3 '+' 2 得5,但if仍走then ? 非0应该走else啊
因为expr表达式返回的是"返回值",不是"结果"
|
[mac@machome ~]$ vi test.sh expr 3 '+' 2 echo $? |
|
[mac@machome ~]$ sh test.sh 5 输出5 0 返回0 只要加法成功,返回值就是0,0为真 |
expr
表达式计算完后如何获得结果?——用 $(expr … ) 或反引号` expr …`
其实就是取其输出,expr的输出就是结果
|
var = expr $a '+' $b echo "final is $var" |
var=$(expr $a '+' $b) echo "final is $var" |
|
test.sh: line 7: var: command not found 加法完成后结果赋值出错 final is |
final is 7 |
expr在if语句里,不能用
[ ],因为这里把它当作command
|
[mac@machome ~]$ vi test.sh if [ expr 3 '+' 2 ] ; then echo no5 else echo is5 fi |
|
[mac@machome ~]$ sh test.sh test.sh: line 1: [: too many arguments is5 |
改成
|
[mac@machome ~]$ vi test.sh if expr 3 '+' 2 ; then expr 3 '+' 2相当于command echo no5 else echo is5 fi |
|
[mac@machome ~]$ sh test.sh 5 no5 |
不过如果要进行结果判定,加$(),就必须用[ ]了,因为把它当作表达式了
|
[macg@machome ~]$ vi test.sh if [ $(expr 3 '+' 2) != 5 ];then 而且[和$中间必须有空格 echo no5 else echo is5 fi |
|
[macg@machome ~]$ sh test.sh is5 |
另一种运算格式i++如何实现?------
i=$(($i+1))
|
while [ $i -le $count ] do command i=$(($i+1)) done |
((
))与[ ]作用完全相同
|
echo input: read i i=$(($i+1)) echo $i |
echo input: read i i=$[$i+1] echo $i |
|
[macg@localhost ~]$ sh ttt.sh input: 6 7 |
[macg@localhost ~]$ sh ttt.sh input: 6 7 |
反面证明((
))与[ ]完全相同--------if (( ))
|
if (( $# != 3 )); then echo "usage: $0 host user passwd" fi |
if [ $# != 3 ]; then echo "usage: $0 host user passwd" fi |
|
[macg@localhost ~]$ sh ttt.sh 1 2 usage: ttt.sh host user passwd |
[macg@localhost ~]$ sh ttt.sh 1 2 usage: ttt.sh host user passwd |
随机推荐
- 认识 service worker
离线缓存可以提升用户体验,可以节省网络资源,但是,浏览器对资源缓存和自定义网络请求的控制一直不够完善,service worker 的出现就是为了解决这些问题 它可以解决目前离线应用的问题,同时也可以 ...
- NoSQL数据库介绍(4)
4 键/值存储 讨论了经常使用的概念.技术和模式后.第一类NoSQL数据存储会在本章进行研究. 键/值存储通常有一个简单的数据模型:一个map/dictionary,同意客户按键来存放和请求 ...
- Android_YouthArea之ApeendTextView
这次给我自己的项目打个广告:http://sj.qq.com/myapp/detail.htm?apkName=com.youthcommunity 这款APP 不同于SoHOT是积极的,是年轻人的信 ...
- 一文读懂实用拜占庭容错(PBFT)算法
在区块链中有一个著名的问题,就是拜占庭将军问题,对于拜占庭将军问题,网上的文章已经多得不要不要了,今天和大家分享的是其相关的实用拜占庭容错算法,一起来看看吧. 实用拜占庭容错算法(Practi ...
- 开发笔记--java.lang.OutOfMemoryError: PermGen space异常处理
第一次到公司开发项目,比起之前的小项目来说这次的项目特别大,以至于运行之后出现了java.lang.OutOfMemoryError: PermGen space的异常,从字面意思上来看是内存溢出的原 ...
- php 生成bing词典能导入的xml(有道词典->bing词典)
编程以来一直用网易有道词典查单词.翻译:最近一直在看英文方面的资料,于是越来越对有道词典(划词.广告,本来想转灵格斯的,但灵格斯没有android版)不满意,一番试用后决定转bing词典,于是想把有道 ...
- 7-4 汉密尔顿回路(25 分) 【STL】
7-4 汉密尔顿回路(25 分) 著名的"汉密尔顿(Hamilton)回路问题"是要找一个能遍历图中所有顶点的简单回路(即每个顶点只访问 1 次).本题就要求你判断任一给定的回路是 ...
- 移动端 (H5) 调试工具 -- vconsole
最近在改一个移动端项目,在手机上调试贼头疼,什么日志都看不到,分析不了bug问题. 然后我同事给我介绍了一个移动端的调试神器 -- vconsole 有了这个神器,领导再也不用担心我的工作啦!!! 0 ...
- MySQL 中事务的实现
在关系型数据库中,事务的重要性不言而喻,只要对数据库稍有了解的人都知道事务具有 ACID 四个基本属性,而我们不知道的可能就是数据库是如何实现这四个属性的: 在这篇文章中,我们将对事务的实现进行分析, ...
- .gitignore文件配置:keil工程文件类型【转】
本文转载自:https://blog.csdn.net/u010160335/article/details/80043965 .gitignore源文件下载链接:git管理keil工程.gitign ...