BASH:控制在进入任何条件后退出 CASE 逻辑 - 无法理解原因

BASH : control exits CASE logic after entering any condition - Unable to understand the reason

我有一个输入文件,其中包含以下数据:

driver2:y
driver5:y
driver3:n
driver1:y
driver4:y

要求是,对于每个驱动程序,如果值为“y”,则脚本必须检查是否存在与该驱动程序相关的一组文件。如果它们都存在,那么下一步应该只执行与驱动程序相关的步骤。

主脚本从input.txt文件中逐行读取。我为此使用了 CASE。

我的问题是当我尝试从 CASE 逻辑调用 function_exists() 时(如您所见,在脚本中进行了注释)或者如果我使用任何“IF”条件而不是 function_exists() 执行相同的检查,CASE 逻辑中的特定部分正在执行(比如 driver2,“driver2)”下的所有代码都在执行)并且脚本控件自动退出 CASE 逻辑并且脚本正在结束,没有从输入文件中读取剩余的行。

脚本正在读取所有条目,但是当我使用函数和 IF 条件时,CASE 逻辑出现问题。

我无法理解脚本出了什么问题,我在这里犯了什么错误。有人可以帮我解决这个问题吗?

#!/bin/sh

function_exists ()
{
ssh $remote_server "test -e "
if [ $? == 0 ]
        then
        echo "file found : "
        else
        echo "file not found : "

fi
echo "***** exiting function"
}

remote_server="vmlinux1"
input="input.txt"

while IFS= read -r line
do
        IFS=":"
        read -ra arr <<< "$line"
        driver=${arr[0]}
        export driver=$driver
        driverInstall=${arr[1]}
        echo "read value" $driver $driverInstall

        if [ $driverInstall == 'y' ]; then
                case $driver in

                driver1)
                echo "entered driver 1"
                #function_exists /opt/installer/file1.txt
                echo "***** control came back to $driver"
                ;;

                driver2)
                echo "entered driver 2"
                #function_exists /opt/installer/file2.txt
                echo "***** control came back to $driver"
                continue
                ;;

                driver4)
                echo "entered driver 4 - for directory check"
                #function_exists /opt/installer/directory1
                echo "***** control came back to $driver"
                ;;

                *)
                echo "${driver} - NOTHING DEFINED"
                ;;

                esac
        else
        echo "${driver} - SKIPPED "
        fi

done < "$input"

echo "Script ended 
========================================"

将“ssh”标准输入连接到 nirvana,否则 ssh 会吃掉剩余的行。

ssh $remote_server "test -e " < /dev/null
# you can also use ssh -n 

read value driver2 y
entered driver 2
file not found : /opt/installer/file2.txt
***** exiting function
***** control came back to driver2
read value driver5 y
driver5 - NOTHING DEFINED
read value driver3 n
driver3 - SKIPPED
read value driver1 y
entered driver 1
file found : /opt/dev/python/scrapper/main.py
***** exiting function
***** control came back to driver1
read value driver4 y
entered driver 4 - for directory check
file not found : /opt/installer/directory1
***** exiting function
***** control came back to driver4
Script ended
========================================

The problem is that your script runs ssh commands and by default ssh reads from stdin which is your input file. As a result, you only see the first line processed, because the command consumes the rest of the file and your while loop terminates.

This happens not just for ssh, but for any command that reads stdin, including mplayer, ffmpeg, HandBrakeCLI, and more.

To prevent this, pass the -n option to your ssh command to make it read from /dev/null instead of stdin. Other commands have similar flags, or you can universally use < /dev/null