免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1197 | 回复: 0
打印 上一主题 下一主题

linux下的(while until for select read shift break..) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-19 10:34 |只看该作者 |倒序浏览

                               
               
                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
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45689/showart_363228.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP