shell里的` ` $( ) ${ } expr $(( ))
shell expr
2023-09-14 08:58:46 时间
转自: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 |
count=$(more iftmp|wc -l) while [ $i -le $count ] |
常见使用: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)" 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]: |
$ 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 |
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 |
shell语言里没有运算公式,必须借助expr语句实现:
expr 变量 ‘+-x /’ 变量 | expr $a '+' $b |
expr 常数 ‘+-x /’ 常数 |
[mac@machome ~]$ expr 3 '+' 2 5 |
输出是结果
因为expr表达式返回的是"返回值",不是"结果"
[mac@machome ~]$ vi test.sh expr 3 '+' 2 echo $? |
[mac@machome ~]$ sh test.sh 5 0 |
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 |
[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 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 |
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 (( $# != 3 )); then fi |
if [ $# != 3 ]; then 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 |
相关文章
- 06·Shell编程-企业级实战练习
- 用 shell 脚本做自动化测试
- linux查找文件夹命令find_用shell命令查看目录下的文件
- Shell正则表达式一览表
- Shell常用命令大全[通俗易懂]
- shell循环语句
- Shell脚本的调试和错误处理
- 变量Linux查看Shell变量的简易方法(linux查看shell)
- shell搭建Linux环境,让csh Shell拯救你的每一天(cshlinux)
- Linux下自定义Shell脚本(linux指定shell)
- shell用Linux写Shell脚本:有趣又实用(linuxwrite)
- 数据使用Shell指令读取MySQL数据库(shell读取mysql)
- 深入探索Linux下Shell的设置(linux设置shell)
- Linux教程:如何开启Shell命令行界面(linux开启shell)
- 服务器shell权限挑战极限:C语言获取Linux服务器Shell权限(c获取linux)
- 脚本编写Linux下的Shell脚本:提升工作效率(linux编写shell)
- Mastering Linux: The Ultimate Guide to Connecting to Your System with Shell(shell连接linux)
- 实现无障碍操作,轻松掌控:远程连接Linux Shell教程(linux远程shell)
- Linux登录Shell:开拓新世界的大门(linux登录shell)
- 命令深入了解Linux,学会常用Shell命令(linux常用shell)
- 探索Linux的Shell编程之旅(linux的shell编程)
- Linux下Shell脚本编程实践(linux的shell脚本)
- Linux Shell编程:改变你的工作效率(linux的shell编程)
- 脚本Linux 中的 Shell 脚本:智能化自动化的提速器(linux中 shell)
- MySQL 中的表达式(Expr)优化技巧(mysql 中expr)
- MySQL的命令行工具MySQL Shell介绍(mysql_shell)
- Oracle Expr掌控数据库的专业利器(oracle expr)