Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4523417
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: Python/Ruby

2011-11-04 15:30:43

脚本包含了一个计数集,用户将其赋予一个新值就可改变它,脚本然后将当前值100加入一个新值。 用户输入一个新值改变其值,如果输入回车键,则不改变它,打印当前值,脚本退出。如果用户用y或Y响应新值,将提示用户输入变量。如果键入回车键,原址人未变。加入一个增量,首先测试是否为数字,如果是,加入计数COUNTOR中,然后显示新值。

  1. #!/bin/bash
  2. # ifcounter.sh
  3. COUNTER=100
  4. echo "Do you wish to change the counter value crretnly set at $COUNTER ?[y..n]"
  5. read ANS
  6. if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]
  7. #yes user wants to change the value
  8. then
  9.     echo "enter a sensible value"
  10.     read VALUE
  11.     # simple test to see if it's numeric,add any number to VALU
  12.     # then check out return
  13.     # code
  14.     expr $VALUE + 10 > /dev/null 2>&1
  15.     STATUS=$?
  16.     # check return code of the expr
  17.     if [ "$STATUS" = "" ] || [ "$STATUS" != "0" ]
  18.     # send errors to standard error
  19.     then
  20.         echo "you either entered nothing or a non-numeric" >&2
  21.         echo "sorry now exiting..counter stays at $COUNTER" >&2
  22.         exit 1
  23.     fi
  24.     # if we are here,then it's a numer,so add it to COUNTER
  25.     COUNTER=$(expr $COUNTER + $VALUE)
  26.     echo "Counter stays at $COUNTER"
  27. else
  28.     # if we are here then user just hit return instedad of entering a number
  29.     # or answered n to the change a value prompt
  30.     echo "Counter stays at $COUNTER"
  31. fi

  1. ywx@ywx:~/Desktop/linux_shell$ ./ifcounter.sh
  2. Do you wish to change the counter value crretnly set at 100 ?[y..n]
  3. y
  4. enter a sensible value
  5. 569
  6. Counter stays at 669










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