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

全部博文(91)

文章存档

2017年(1)

2011年(1)

2008年(15)

2007年(74)

我的朋友

分类: LINUX

2007-08-19 10:34:37

while
 syntax
   while
    test-commands
   do
    commands
   done
 description
  execute test-command(usually a test commnad) and if the exit status is zero, perform commands, repeat. opposite until.
until
 syntax
  until
   test-commands
  do
   commands
  done
 description
  execute test-commands(usually a test command) and if the exit status is nonzero(that is, the test fails), perform commands; repeat. opposite of while.
  嵌套的while loop
  while cmd1
  do
   commands
   while cmd2
   do
    commands
   done
  commands
 done
 
  example:
  #using while loop
  x=0
  while [ $x -lt 10 ]
  do
  echo $x
  x=$(($x+1))
  done
  #using until loop
  until [ $x -lt 0 ]
   do
   echo $x
   x=$(($x-1))
   done
  #using multiple while loop
 x=0
 while [ $x -lt 10 ]; do
  y="$x"
  while [ $y -ge 0 ]; do
   printf "$y"
   y=$(($y-1))
  done
  echo x=$(($x+1))
 done
 #while associate with stdin redirection(while循环和输入重定向,从文件中读出一行)
 #!/bin/bash
#calculate the line counts of a file(计算一个文件的行数)
if [ -f "$1" ]
 then
 i=0
 while read LINE
 do
  i=$(($i+1))
 done<"$1"
echo $i
fi
#invoking like this: source ./script_filename file(scan file)

for
 syntax
 for x in list
  do
  commands
  done
 description
  assign each word in list to x in turn and execute commands. if list is omitted, it is assumed that positional parameters from the commands line, which are storecd in $@, are to be used.
 example
 for i in 0 1 2 3 4 5 6 7 8 9
 do
echo $i
 done
 
 example2:
 #!/bin/bash
 #copy all files of $HOME/files/ to $HOME/c/(复制当前主目录下的目录为files里的所有文件到主目录下的目录c中)
 for j in $HOME/files/*
 do
 cp $HOME/files/* $HOME/c
done

select
 syntax
 select variable in list
 do
 case variable in
  var1) commands;;
  var2) commands;;
  .....
  varN) commands
 esac
 done
 example:
 #!/bin/bash
select digit in 1 2 3 4 5
 do
case $digit in
 1|2) printf "this is the digit 1 and 2 \n";;
 3) printf "this is the digit 3\n";;
 4) printf "this is the digit 4\n";;
 *) printf "this the other digit\n";break
esac
done

  change the value of ps3 to change the prompter of select loop:
  $PS3="please make a selection => "; export PS3
  
break
 syntax
 break [n]
 description
  Exit from the innermost(most deeply nested) for, whie, or until or from the n innermost levels of the loop
  
 example:
 for i in 1 2 3 4 5
 do
  mkdir -p /mnt/backup/docs/ch0${i}
  if [ $? -eq 0 ]
  then
  for j in doc c h m pl sh
  do
  cp $HOME/docs/ch0${i}/*.${j} /mnt/backup/docs/cho${i}
  if [ $? -ne 0 ]; then break 2;
  fi
  else
   printf "could not make backup directorys.\n"
  fi
  done

fi

done

 
continue
  syntax
  continue [n]
  skip remaining commands in a for, while, or until loop, resuming whith the next iteration of the loop(or skipping n loops)
  example
  for FILE in $FILES
   do
   if [ ! -f $FILE ]
   echo "ERROR, the $FILE is not a file"
   continue
   fi
   #process file
   done
   
exit
  syntax
  exit [n]
  Exit a shell script with status n. The value for n can be 0(success) or nonzero(failure). if n is got given, the exit status is that of the most recent command.
  
  example:
  if ! test -f somefile
   then
   echo "somefile is not a file"
   exit 1
  fi
  
read
syntax
 read [option] variable1 [variable2 .....]
 Read one line of standard input, and assign each word to the corresponding variablewith all remain words assinged to the last variable.
 
 example
  echo "Enter last-name, height, and weight"
  read lastname everythigelse
  echo $lastname
  echo $everythingelse
 The name entered is placed in variable $lastname; all of the other values, includeing the spaces between them, are placed in $everythingelse.
 
return
 syntax
 return [n]
 This command is used inside a function to exit the function with status n. if n is omitted, the exit status of the previously executed command is returned.
 
shift
  shift [n]
  shift positional parameters down n elements, if n is omitted, the default is 1, so $2 become $1, $3 become $2, and so on.(省去前n个元素,如果n 省略,就是省去第一个参数)
  
  如:
#!/bin/bash
if [ $# -lt 2 ]; then
 echo "the options less than 2"
 exit 1
fi
shift
for i in $@
 do
case $i in
 witi) echo "this is file1";;
 apple) echo "this is a apple";;
 orange) echo "$2 this is the second item,the '$#' is $# and the '$*' is $* and
the $@"
;;
 *) echo "this is other";;
esac
done
#the script terminate
invoking it: $source ./file2 kiwi apple orange banana

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