是否在条件循环多个读取命令shell脚本

问题描述:

产生的问题,我有一个程序,它是这样:是否在条件循环多个读取命令shell脚本

fun_a() 
{ 
    echo "other statement" 
    read user_input 
    if [[($user_input == "Y") || ($user_input == "y")]];then 
     fun_b 
    else 
     ...... 
    fi 
} 
fun_b() 
{ 
    echo "some other statement" 
    echo "any statement" 
    read user_inp 
    if [[($user_inp == "Y") || ($user_inp == "y")]];then 
    do something 
    else 
     .......... 
    fi 
} 
echo "any statement" 
read user_inp 
if [[($user_inp == "Y") || ($user_inp == "y")]];then 
    fun_a 
else 
    ...... 
fi 

我没有得到预期从“读”命令的输出。是否因为在下一个读取语句被调用之前if块没有关闭?如果是这样,建议一些解决方案

你不需要每次都阅读,而且你不需要在每个函数中检查“Y”。如下面的脚本,你可以看到“阅读”只需要一次。

#!/bin/bash 
fun_a() 
{ 
    echo 'function_a' 
    echo "Input: ";read content2 
    echo 'content2: ' $content2 
    echo 'content: '$content 
} 
echo "Input: "; read content 
echo 'content: '$content 
fun_a 

>>> ./script.sh 
Input: 
first 
content: first 
function_a 
Input: 
second 
content2: second 
content: first 
+0

埃里克,需求是这样的,除非用户不想继续,我们不应该继续,目前的功能应该只调用用户输入的下一个功能。除了印刷一行外,还有很多东西。如果用户i/p是Y,那么正在生成的o/p正在被传送到下一个功能。 – prince623

+0

然后,我会建议您创建一个更多功能 “如果[[($ user_input ==”Y “)||($ user_input ==”y“)]] return XXX” 然后您可以简单地使用返回值决定要做什么。你知道如何修改吗? – Eric