shell编程中的符号

单引号和双引号含义区别
单引号和双引号都能关闭shell对特殊字符的处理。不同的是,双引号没有单引号严格,单引号关闭所有有特殊作用的字符,而双引号只要求shell忽略大多数,具体的说,就是①美元符号②反引号③反斜杠,这3种特殊字符不被忽略。 不忽略美元符号意味着shell在双引号内部也进行变量名替换。


下面用一个简单的shell程序要说明一下。


debian:~/learn/shell# cat phonebook

Alice Chebba 973-555-2015

Barbara Swingle 201-555-9257

Liz Stachiw 212-555-2298

Susan Goldberg 201-555-7776

Susan Topple 212-555-4932

Tony Iannino 973-555-1295

Stromboli Pizza 973-555-9478

debian:~/learn/shell#


debian:~/learn/shell# cat lu

# Look someone up in the phone book

grep "$1" phonebook

debian:~/learn/shell#


这是正确的lu程序,下面是运行结果。


debian:~/learn/shell# ./lu 'Susan T'

Susan Topple 212-555-4932

debian:~/learn/shell# ./lu Tony

Tony Iannino 973-555-1295

debian:~/learn/shell#


如果lu写成①grep $1 phonebook或者②grep '$1' phonebook,就会出现下面的错误结果(为什么?)。


①的结果:


debian:~/learn/shell# ./lu Tony //这种情况结果正确

Tony Iannino 973-555-1295

debian:~/learn/shell# ./lu 'Susan T' //这种情况结果错误

grep: T: No such file or directory

phonebook:Susan Goldberg 201-555-7776

phonebook:Susan Topple 212-555-4932

debian:~/learn/shell#


②的结果:


debian:~/learn/shell# ./lu Tony //这种情况结果错误

debian:~/learn/shell# ./lu 'Susan T' //这种情况结果也错误

debian:~/learn/shell#

Shell编程基础:单引号和双引号含义区别 - 详文链接:http://www.zg80.cn/html/caozuoxitong/Linuxjiaocheng/200810/Shellbianchengjichu-danyinhaoheshuangyinhaohanyiqubie_25146.html

 

 

shell编程中的[] (()) [[]]符号
[ expression ] 用于test操作

n1 -eq n2 Check if n1 is equal to n2.

n1 -ge n2 Check if n1 is greater than or equal to n2.


n1 -gt n2 Check if n1 is greater than n2.
n1 -le n2 Check if n1 is less than or equal to n2.
n1 -lt n2 Check if n1 is less than n2.
n1 -ne n2 Check if n1 is not equal to n2.
str1 = str2 Check if str1 is the same as string str2.
str1 != str2 Check if str1 is not the same as str2.
str1 < str2 Check if str1 is less than str2.
str1 > str2 Check if str1 is greater than str2.
-n str1 Check if str1 has a length greater than zero.
-z str1 Check if str1 has a length of zero.
-d file Check if file exists and is a directory.
-e file Checks if file exists.
-f file Checks if file exists and is a file.
-r file Checks if file exists and is readable.
-s file Checks if file exists and is not empty.
-w file Checks if file exists and is writable.
-x file Checks if file exists and is executable.
-O file Checks if file exists and is owned by the current user.
-G file Checks if file exists and the default group is the same as the current user.
file1 -nt file2 Checks if file1 is newer than file2.
file1 -ot file2 Checks if file1 is older than file2.

(( expression )) 用于数值计算

val++ post-increment
val-- post-decrement
++val pre-increment
--val pre-decrement
! logical negation
∼ bitwise negation
** exponentiation
<< left bitwise shift
>> right bitwise shift
& bitwise Boolean AND
| bitwise Boolean OR
&& logical AND
|| logical OR

[[ expression ]] 高级的字符串比较 支持正则表达式

示例:
$ cat test
#!/bin/bash
# using pattern matching
if [[ $USER == r* ]]
then
echo "Hello $USER"
else
echo "Sorry, I don’t know you"
fi
$ ./test
Hello rich
$

本文来自: (www.91linux.com) 详细出处参考:http://www.91linux.com/html/article/shell/20081130/14102.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值