Menu Close

for 循环 和 While 语句

与其他编程语言类似,Shell支持for循环。

for循环一般格式为:

#!/bin/sh
for i in 1 2 3 4 5
do
  echo "Looping ... number $i"
done

与其他编程语言类似,Shell也支持while循环。while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。

while循环一般格式:

while condition
do
    command
done

例如:

#!/bin/bash
i=1
while(( $i<=5 ))
do
    echo $i
    let "i++"
done

你可以利用while语句设计出一些好玩的游戏

#!/bin/sh
while :
do
  echo "Please type something in (^C to quit)"
  read INPUT_STRING
  echo "You typed: $INPUT_STRING"
done

除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Leave the field below empty!

Posted in 外壳脚本