Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2502546
  • 博文数量: 709
  • 博客积分: 12251
  • 博客等级: 上将
  • 技术积分: 7905
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-17 00:00
个人简介

实现有价值的IT服务

文章存档

2012年(7)

2011年(147)

2009年(3)

2008年(5)

2007年(74)

2006年(431)

2005年(42)

分类: LINUX

2005-11-13 22:13:23

example about linux shell

#!/bin/bash
echo "Hello world!"
echo "Hello $LOGNAME,It's nice talking to you"
echo "U present working on a machine is `uname -n`"
echo "U present working directory is $(pwd)"
echo "Bye for now $LOGNAME.The time is $(date +%T)"

#===========================================
NAME="Tiger"
export name

#===========================================
DESC=$(awk -F: '{print $2}' database.txt)
echo $DESC

#===========================================
exec date #替换当前shell,新建立一个shell执行date
#===========================================
echo -e "Who are u ?"
read answer
echo "Hello $answer,Welcome!"

#===========================================
if [$LOGNAME !=root ]
then
 echo "Must have root privileges to run this program"
 exit 1
fi
cat <Warning:All jobs in the printer ques will be removed.
Please turn of the printer now.Press return when you are read to contine.Otherwise press Control C.
EOF
read JUNK #just to wait for user response
echo
/etc/rc.d/init.d/lpd stop
echo -e " please turn the printer on now."
echo "presss return to continue"
read JUNK
echo
/etc/rc.d/init.d/lpd start
#===========================================
wc < /etc/passwd
cat /etc/passwd|wc
#===========================================
条件                判断是否为真
字符判断:
[string1=string2]   string1等于string2
[string==string2] string1等于string2
[string!=string2] string1不等于string2
[string]  string 不为空
[-z string]  string长度为0
[-n string]  string长度为n
[-l string]  string长度

整形判断:
[int1 -eq int2]  int1不等于int2
[int1 -ne int2]  int1不等于int2
[int1 -gt int2]  int1大于int2
[int1 -ge int2]  int1大于等于int2
[int1 -lt int2]  int1小于int2
[int1 -le int2]  int1小于等于int2

逻辑判断
[[pattern1 && pattern2]] p1和p2都为真
[[pattern1 || pattern2]] p1或p2为真
[[!pattern]]   p不匹配


-b 特定块文件
-c 特定字符文件
-d 目录存在
-e 文件存在
-f 非目录普通文件存在
-G 文件存在并属于一个特定的Gid
-g 设置Set-group-id
-k 拈贴位设置
-L 文件是一个符号联接
-p 文件是一个管道
-O 文件存在并属于一个有效果的uid
-r 文件可读
-S 文件是一个套接字
-s 文件size非零
-t fd已在终端打开
-u 设置Set-user-id
-w 文件可写
-x 文件可执行

#!/bin/bash
file=./test

if [ -d $file ]
then
 echo "$file is a directory"
elif [ -f $file ]
then
 if [ -r $file -a -w $file -a -x $file]
 then
  echo "you have read,write,and execte
   permission on $file"
 fi
else
 echo "$file is neither afile nor a directory."
fi

#===========================================
null命令,用:表示。是一个内建的什么都不做的命令。

#===========================================
case命令:
case variable in
value1)
 command(s)
 ;;
value2)
 command(s)
 ;;
*)
command(s)
 ;;
esac

#!/bin/bash
echo -n "Choose a foreground color for your xterm window:"
read color
case "$color" in
[Bb]l??)
 xterm -fg blue -fn terminal &
 ;;
[Gg]ree*)
 xterm -fg darkgreen -fn terminal &
 ;;
red | orange)
 xterm -fg "$color" -fn terminal &
 ;;
*)
 xtern -fn terminal
 ;;
esac

echo "Out of case cammnad"
#===========================================
for 命令:
for variable in word_list
do
 command(s)
done

#!/bin/bash
for pal in Tom Dick Harry Joe
do
 echo "HGi $pal"
done
echo "Out of loop"

---------------
#cat sayit
tom
patty
ann
jake

#!/bin/bash
for person in $(cat sayit)
do
 mail $person < letter
 echo $person was sent a letter.
done
echo "The letter has been sent."

for name in $*
do
 echo $name
done
#===========================================
while命令:
while command
do
 command(s)
done
#!/bin/bash
num=0
while(($num <10))
do
 echo -n "$num"
 let num+=1
done
echo -e " After loop exits,continue running here"


------------------
echo "Who was the 2nd U.S. president to be impeached?"
read answer
while [[ "$answer" !="Bill Clinton" ]]
do
 echo "Wrong try again!"
 read answer
done
echo you got it!

#===========================================
util 命令
#!/bin/bash
util who |grep linda
do
 sleep 5
done
talk

-----------------
cat hour
!/bin/bash
let hour=0
until ((hour>24)) or [ $hour -gt 24 ]
do
 case "$hour" in
 [0-9]|[0-1])
  echo "Good morning!"
  ;;
 12)
  echo "Lunch time." 
  ;;
 1[3-7]
  echo "Siesta time."
  ;;
 *)
  echo "Good night"
  ;;
 esac
 let hour+=1
done
#===========================================
select 菜单
#!/bin/bash
ps3="Select a program to execute: "
select program in 'ls -F' pwd date
do
 $program
done

--------------
1) ls -F
2) pwd
3) date
#? 1
p315.sh*
#? 2
/home/luser623/shell
#? 3
Thu Nov 10 22:12:26 CST 2005
#? 2
/home/luser623/shell
#? 3
Thu Nov 10 22:12:29 CST 2005


--------------------------
#!/bin/bash
ps3="select choose one of the three boys :"
select choice in tom dan guy
do
 case $choice in
 tom)
  echo Tom is a cool dude!
  break
  ;;
 dan|guy)
  echo dan an guy are both wonderful.
  break
  ;;
 *)
  echo "$choice is not one of your choices" 1>&2
  echo "Try again."
  ;;
 esac
done

#===========================================
shift命令:
shift [n],把位置参数向左移n位,当没有表明n时,n=1
#!/bin/bash
set joe marry tom sam

shift       #now s* 只剩下marry tom sam,joe已经删除
set $(date) #Thu Mar 16 10:02 PST 2000
shift 5
echo %*     #只剩下2000
shift 2
shift:shift count must b <=$#    #出错信息

-------------
#!/bin/bash
while(($#>0))
do
        echo $*
        shift
done
[luser623@LBaseE1 shell]$ ./p318.sh 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9
4 5 6 7 8 9
5 6 7 8 9
6 7 8 9
7 8 9
8 9
9
%2

阅读(1661) | 评论(0) | 转发(0) |
0

上一篇:kill process

下一篇:something about linux shell 2

给主人留下些什么吧!~~