Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239327
  • 博文数量: 91
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-12 09:38
文章分类

全部博文(91)

文章存档

2017年(1)

2011年(1)

2008年(15)

2007年(74)

我的朋友

分类: LINUX

2007-08-12 10:18:31

if
 syntax:
  if expression1
  then
  commands1
  elif expression2
  then
  commands2
  .....
  else
  commands3
  fi
  description
  The if command is used to define a confitinal statement. There are three possible formats for using the if command:
  if-then-fi
  if-then-else-fi
  if-then-elif-then ...fi
  如果表达式写成一行,就得用;隔开.
  if expression1; then commands1; else commands2; fi
  test
  syntax:
  test expr
  expr是一个所能理解的选项,返回0(为真), 其它的为假 []为test的缩写
  [ expressions ]
  frequenctly used options
  -a file        true if file exist
  -d file        true if file exist and is a directory
  -b file        true if file exist and are blocks
  -c file        true if file exist and are characters
  -g pathname true if file or directory exist and set the SGID
  -G path    true if path exist and the group match the current user's group ID(path存在并且其组和当前的组ID匹配时为真)
  -k pathname    true if file or directory exist and set the stick bit
  -p file        true if file exist and is a named pipe
  -u file     true if file exist and set the SUID
  -o pathname    true fi file or directory is exist and is belong the user which assiged UID by the currently process(当pathname指定的文件或目录存在时并且被当前进程的有效用户ID所指定的用户拥有时返回真)
  -O file        true if the user is the ownership of the file(当运行的用户是其属主时为真)
  -e pathname    true if file or directory exist
  -f file        true if file exist and is regular file
  -L file        true if file exsit and is symbolic link
  -h file        true if file exist and is symbolic link
  -r file        true if the file exist and is readable
  -s file        true if file exist and has a size is greater than zero
  -S file        true if file is socket
  -t des        true if des is associate the file descriptor of terminate device(当des是与终端设备相关联的文件描述符时为真)
  -w file        true if file exist and writable
  -x file     true if file exist and executable
  -n string    true if the length of string nonzero
  -z string    true if the length of string is zero
  file1 -ot file2    true if file1 is older than file2
  string1 =string2 true if strings are equal
  string1 != string2 true if the strings are not equal
  !expr        true if expr is false
  expr1 -a expr2    true if exprs are true(same as &&)
  [expr1]&&[expr2] true if expr1 and expr2 are true
  [expr1]||[expr2]    true if expr1 or expr2 is true
  expr1 -o expr2    true if expr1 or expr2 is true(same as ||)
   test command'
s digit compare operator
  int1 -eq int2 true if ints are equal
  int1 -ne int2 true if ints are not equal
  int1 -lt int2    true if int1 less than int2
  int1 -le int2    true if int1 less than and equal to int2
  int1 -gt int2     true if int1 greater than int2
  int1 -ge int2     true if int1 greater than and equal to int2
  
  example: if test -r file; then
          echo "file is exist"
          fi
       using the [] form instead, the same test looks like this:
       if [ -r file ]; then
       echo "file is exist"
       fi
       
       if [ $prompt != "no" ]; then
        echo 'printer'
       fi
if [ -d /usr/local -a -d /usr/bin ];then
       echo "the two directories are exist"
       fi
       the same result is look like following
       if [ -d /usr/local ]&& [ -d /usr/bin ]
       then echo "the two directories are exist"
       fi
       if test ! -f /usr/local; then
       echo "/usr/local is a directory"
       fi
   
       #first judge /usr/kbd and /etc/kbdtype is true? second do option -a
       if [ -f /usr/kbd -a -f /etc/kbdtype ]; then
        kbd 'cat /etc/kbdtype' #kbd keyboard drivers
        fi
 
case
 syntax
  case string
   in
   regex1)
   commands1
   ;;(same as break)
  regex2)
  commands2
  ;;
  ....
 esac
 description
  Choose string from among a series of possible regular expressions. if string matches regular expression regex1, perform the subsequent command1. if string matches regex2, perform commands2. proceed down the list of regular expressions until one is found. To catch all remainings, use *) at the end.
  example:
  #using the case command
  FRUIT=kiwi
  case $FRUIT in
    apple) echo "the fruit is apple";;#(;; same as C language's break)
    orange) echo "the fruit is oriange";;
    banana) echo "the fruit is banana";;
    * ) echo "the fruit is not in the list"
  esac
  #the $- contain all options of shell lists; it is a simple method to judge a shell is runing in interactive or noninteractive through judge the $- is contain the character i? if the currently shell is runing noninteractive mode, then it do nothing, else it will modify the promptor, PS1 and the searching path of commands
  专用变量$-是一个包含有所有shell选项的列表,在本例中,你可以确定该列表包含字母i是判断shell是运行在交互模式还是在非交互模式下的最简单方法.在本例中,如果shell运行在交互模式下你将设置提示符,PS1以及命令搜路径PATH.否则什么都不干.
  case $- in
   *i*) #an interactive shell
    PS1="uname -n'
$

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

上一篇:没有了

下一篇:linux下的shell variables

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