如何在shell脚本中重复该过程

时间:2015-08-06 09:37:24

标签: shell sh

您好我是shell脚本的新手。在我的shell脚本中,我想重复这个过程。

script.sh
echo "Enter the city name"
read cityname
echo "Enter the state name"
read statename
pig -x mapreduce mb_property_table_updated.pig city=$cityname state=$statename
echo "Do you want to run for another city"

如果是,则表示我想再次重复这些过程,否则它将转移到下一个过程。非常感谢。

3 个答案:

答案 0 :(得分:1)

使用while循环

line=yes
while [ "$line" = yes ]
do
echo "Enter the city name"
read cityname
echo "Enter the state name"
read statename
pig -x mapreduce mb_property_table_updated.pig city=$cityname state=$statename
echo "Do you want to run for another city"
read line
done

还有for循环

for((;;))
do
echo "Enter the city name"
read cityname
echo "Enter the state name"
read statename
pig -x mapreduce mb_property_table_updated.pig city=$cityname state=$statename
echo "Do you want to run for another city"
read answer
if [ "$answer" = "yes" ]
then
continue
else
break
fi
done

答案 1 :(得分:1)

我会做这样的事情:

while [ "$e" != "n" ]; do
    echo "Enter the city name"
    read cityname
    echo "Enter the state name"
    read statename
    pig -x mapreduce mb_property_table_updated.pig city=$cityname state=$statename
    echo "Do you want to run for another city (y/n)"
    read e
done

答案 2 :(得分:0)

For completeness, there is also an often overlooked until loop in shell.

until [ "$answer" = no ]; do
    echo "Enter the city name"
    read cityname
    echo "Enter the state name"
    read statename
    pig -x mapreduce mb_property_table_updated.pig city=$cityname state=$statename
    echo "Do you want to run for another city"
    read answer
done