现在的位置: 首页 > 综合 > 正文

shell中for循环的使用 (ex22.sh)

2014年06月05日 ⁄ 综合 ⁄ 共 556字 ⁄ 字号 评论关闭
#!/bin/bash
# Listing the planets.

for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto
do
  echo $planet  # Each planet on a separate line.
done

echo; echo

for planet in "Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto"
    # All planets on same line.
    # Entire 'list' enclosed in quotes creates a single variable.
    # Why? Whitespace incorporated into the variable.
do
  echo $planet
done

echo; echo "Whoops! Pluto is no longer a planet!"

exit 0

[root@localhost shell]# ./ex22.sh
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Pluto

Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto

Whoops! Pluto is no longer a planet!

抱歉!评论已关闭.