shell——for&while&select
来源:互联网 发布:考研对未来的意义 知乎 编辑:程序博客网 时间:2024/04/29 09:50
本文通过实例来分析shell中for、while、select的使用。
1、select表达式是bash的一重扩展应用,擅长于交互式场合,永和可以从一组不同的值中进行选中
select var in ... ; do
break ;
done
... now $var can be used ...
例如:用户根据设定值进行选则
echo "what is your favourite OS? "
select var in "LINUX" "Windows" "Unbutu" "Other" ; do
break;
done
echo "You have selected $var"
执行后:
[qatest@nmsg-42 ~]$ ./select.sh
what is your favourite OS?
1) LINUX
2) Gnu Hurd
3) fREE bsd
4) Other
#? 3
You have selected fREE bsd
2、在shell中可以使用while循环,只要测试表达式条件为真,则while循环将一直运行。关键字break用来跳出循环,而关键字continue则可以跳过一个循环的余下部分,直接跳到下一次循环中,while中为方括号[],且有空格
while ... ; do
...
done
例如:while和case结合
while :
do
echo -n "input a letter: "
read var
case "$var" in
[a-z]) echo "Lower case letter" ;;
[A-Z]) echo "Upper case letter" ;;
[0-9]) echo "Digit" ;;
*) echo "other letter" ;;
esac
done
执行后:
[qatest@nmsg-42 ~]$ ./case.sh
input a letter: a
Lower case letter
例如:判断min和max,然后输出,#while循环注意为方括号[],且注意空格
min=1
max=10
while [ $min -le $max ]
do
echo $min
min=`expr $min + 1`
done
例如:#双括号形式,内部结构有点像C的语法,注意赋值:i=$(($i+1)), 即输出4的整数是多少,结果为4和8
i=1
while(($i<10))
do
if(($i%4==0))
then
echo $i
fi
i=$(($i+1))
done
3、在shell中for会查看一个字符串列表(字符串用空格分割),并将其赋给一个变量
for var in ... ; do
...
done
例如:输出1到10,#数字段形式
for a in {1..10}
do
echo "a is $a"
done
例如:#详细列出(字符且项数不多)
for File in 1 2 3 4 5
do
echo $File
done
例如:#对存在的文件进行循环,查找.sh文件,然后输出每个文件的名字
for shname in `ls *.sh`
do
name=`echo "$shname" | awk -F. '{print $1}'`
name=`echo "$shname" | awk -F "|" '{print $1}' #显示扩展名`
echo $name
done
例如:#查找循环(ls数据量太大的时候也可以用这种方法)
for shname in `find . -type f -name "*.sh"`
do
name=`echo "$shname" | awk -F/ '{print $2}'`
echo $name
done
例如:#((语法循环--有点像C语法,但记得双括号,输出3、6、9
for((i=1;i<10;i++))
do
if((i%3==0))
then
echo $i
continue #如果用break,则只会输出3
fi
done
例如:#seq形式 起始从1开始
for i in `seq 100`
do
if((i%3==0))
then
echo $i
continue
fi
done
通过以上示例可以很好的看到select、while、for的使用。
- shell——for&while&select
- shell if case while select for
- shell编程中for/while/util/case/select/break/continue
- for&while loop/shell
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- shell循环:for循环、while循环、until循环和select循环
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- Shell 脚本之循环语句(for,while,until)以及case,select,break,continue实例详解
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- shell编程中for,while,util,case,select,break, continue用法详解
- shell 循环 for while until ....
- shell ----- 循环 for && while && util
- shell:for和while用法
- shell 中for、while循环
- shell循环,for 、while、until
- BloomFilter——大规模数据处理利器
- 同一个tomcat添加多个项目的报webAppRootKey错问题
- 【启发式搜索】[POJ 1077]Eight
- URL的井号
- hdu 1863 畅通工程-----------最小生成树模板题
- shell——for&while&select
- VS2010应用程序的打包与图标设置
- NAT详解
- seo优化必须知道的sitemap.xml的写法
- solr5.2.1
- HDU 2795 Billboard(线段树)
- 结构型模式-适配器模式
- 1230 火星 A+B
- 路由和NAT的区别与联系