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

Time:05-07

I have an input file, which has below data :

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

The requirement is, for each driver if the value is "y" then the script has to check for the existence of a set of files that are related to that driver. If all of them exist, then only that driver related next step should be performed.

The main script reads line by line from the input.txt file. and I'm using CASE for this.

My problem is when I try to call the function_exists() from the CASE logic (which are commented in the script as you can see) or If I use any "IF" conditions instead of function_exists() to perform the same check, that particular part in the CASE logic is executing (say driver2, all the code under "driver2)" executes) and the script control is automatically exiting the CASE logic and script is ending, without reading the remaining lines from the input file.

The script is reading all entries, but something is wrong with the CASE logic when I'm using functions and IF conditions.

I'm unable to understand what went wrong with the script, what mistake I'm doing here. Can someone help me with this?

#!/bin/sh

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

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 
========================================"

CodePudding user response:

Connect "ssh" standard input to nirvana otherwise ssh eats remaining lines.

ssh $remote_server "test -e $1" < /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

  • Related