Chinaunix首页 | 论坛 | 博客
  • 博客访问: 298391
  • 博文数量: 240
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-04 18:14
文章分类

全部博文(240)

文章存档

2017年(8)

2014年(4)

2013年(15)

2012年(4)

2011年(14)

2010年(55)

2009年(140)

我的朋友

分类: LINUX

2009-07-11 16:56:33

 
 

if语句:
if [ "22" -lt "33" ]
then
 echo "22 less than 33"
else
 echo "no"
fi

 

case语句:
echo "enter a number"
read ans
case $ans in
1)
echo "you numer is $ans"
;;
2)
echo "you number is 2"
;;
[3-9])
echo "you number is $ans"
;;
*)
echo "You are input not a sinle number!"
esac

 

while语句:
i=1
while [ $i -lt 10 ]
do
 echo $i >> newfile
 let i=i+2
done

 
for语句:
for server in `cat /etc/hosts`
do
 echo $server
done

for((i=0; i<10; i++))
do
 echo $i
done

 

特殊的变量:
$0 这个程序的执行名字
$n 这个程序的第n个参数值,n=1...9
$* 这个程序的所有参数
$# 这个程序的参数个数

$$ 这个程序的PID
$! 执行上一个背景指令的PID
$? 上一个指令的返回值
$@ 表示带的参数

 

文件比较运算符
-b file 若文件存在且是一个块特殊文件,则为真
-c file 若文件存在且是一个字符特殊文件,则为真
-d file 若文件存在且是一个目录,则为真
-e file 若文件存在,则为真
-f file 若文件存在且是一个规则文件,则为真
-g file 若文件存在且设置了SGID位的值,则为真
-h file 若文件存在且为一个符合链接,则为真
-k file 若文件存在且设置了"sticky"位的值
-p file 若文件存在且为一已命名管道,则为真
-r file 若文件存在且可读,则为真
-s file 若文件存在且其大小大于零,则为真
-u file 若文件存在且设置了SUID位,则为真
-w file 若文件存在且可写,则为真
-x file 若文件存在且可执行,则为真
-o file 若文件存在且被有效用户ID所拥有,则为真
-L file 若文件为符号链接,则为真
 
-z string 若string长度为0,则为真
-n string 若string长度不为0,则为真
string1 = string2 若两个字符串相等,则为真
string1 != string2 若两个字符串不相等,则为真
 
int1 -eq int2 若int1等于int2,则为真
int1 -ne int2 若int1不等于int2,则为真
int1 -lt int2 若int1小于int2,则为真
int1 -le int2 若int1小于等于int2,则为真
int1 -gt int2 若int1大于int2,则为真
int1 -ge int2 若int1大于等于int2,则为真
 
expr1 -a expr2 若expr1和expr2都为真则整式为真
expr1 -o expr2 若expr1和expr2有一个为真则整式为真

file1 -nt file2 若file1 比 file2 新,则为真
file1 -ot file2 若file1 比 file2 旧,则为真

 

 
条件变量替换:
Bash Shell可以进行变量的条件替换,既只有某种条件发生时才进行替换,替换
条件放在{}.
(1) ${value:-word}
当变量未定义或者值为空时,返回值为word的内容,否则返回变量的值.
(2) ${value:=word}
与前者类似,只是若变量未定义或者值为空时,在返回word的值的同时将
word赋值给value
(3) ${value:?message}
若变量以赋值的话,正常替换.否则将消息message送到标准错误输出(
此替换出现在Shell程序中,那么该程序将终止运行)
(4) ${value:+word}
若变量以赋值的话,其值才用word替换,否则不进行任何替换
(5) ${value:offset}
${value:offset:length}
从变量中提取子串,这里offset和length可以是算术表达式.
(6) ${#value}

变量的字符个数
(7) ${value#pattern}

${value##pattern}

去掉value中与pattern相匹配的部分,条件是value的开头与pattern相匹配
#与##的区别在于一个是最短匹配模式,一个是最长匹配模式.

(8) ${value%pattern}
${value%%pattern}
(7)类似,只是是从value的尾部于pattern相匹配,%%%的区别与#与##一样

(9) ${value/pattern/string}
${value//pattern/string}
进行变量内容的替换,把与pattern匹配的部分替换为string的内容,///的区
别与上同
注意:上述条件变量替换中,(2),其余均不影响变量本身的值

 

 

 

一个备份文件的脚本:
#!/bin/bash
# Backs up all files modify in 24 hours in current directory.

if [ $# != 1 ]
then
  echo "Usage: `basename $0` filename"
  exit 1
fi
echo "Please wait..."
tar cvf - `find . -mtime -1 -type f -print` > $1.tar
gzip $1.tar
exit 0

 

 

阅读(706) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~