Home > OS >  Call a bash scripts with a bash script. Check the exit code and ignore the output of the called bash
Call a bash scripts with a bash script. Check the exit code and ignore the output of the called bash

Time:12-30

I wrote a script, where I call other scripts (without a parameter) and than check the return value of the script (it should be 1).

yourfilenames=$(ls ./)
for FILE in $yourfilenames
do
Points=0
./$FILE 
if [ $? -ne 0 ];
then
    Points=$(($Points   1))
fi
done

The problem is a script like this:

Usage()
{
clear
    printf "${red}ERROR (An Error has occured.)${reset}\n\n"
    sleep 1.5
    
    read -n 1 -s -r -p "Please press a key to continue..."
    clear
}

if [ $# -gt 2 ] || [ $# -lt 1 ]
then
    Usage
    exit 1
fi

And also:

echo "What do u want to know?(Quit with "q")"
while (true)
do
    read input_string
    case $input_string in
.
.
.

q)  break
    ;;

Q)  break
    ;;

*)  echo "This function is not possible"
    ;;
    esac
done
echo
echo "Bye"

exit 0

My script should check the return value of like 50 scripts automated, but my script is stopping because I need to press a key to continue it (In the example above).

How can my script "ignore" these Input requests? Can my script also ignore the "sleep 1.5" ?

I tried it with >/dev/null but that doesnt work, I just dont see the output on my console. I still need to press a key.

CodePudding user response:

Pipe the yes command to the command. It provides a continuous supply of y answers to any questions.

Also, don't parse the output of ls. It will fail if any of the filenames have whitespace in them.

And Points needs to be initialized before the loop. You're setting it back to 0 before each script.

Points=0

for eachfile in *
do
    if ! yes | ./"$eachfile" >/dev/null
    then
        ((Points  ))
    fi
done

Needless to say, you should only do this if you know that the scripts don't do anything dangerous based on the response to the prompts.

You can override the sleep commands by creating a directory with a symlink sleep that points to /bin/true, and put this directory at the beginning of $PATH.

  •  Tags:  
  • bash
  • Related