gpt4 book ai didi

linux - 条件循环中的多个读取命令是否会在 shell 脚本中产生问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:19:58 30 4
gpt4 key购买 nike

我有一个程序是这样的:

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

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

最佳答案

您不需要每次都阅读,实际上也不需要在每个函数中检查“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

关于linux - 条件循环中的多个读取命令是否会在 shell 脚本中产生问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46300256/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com