mardi 14 juin 2016

Why does this work if I use 'echo' but doesn't otherwise?

So I'm currently just trying to traverse through my current directory where I'm calling the following bash script that prints 'We found a .c file' every time one is found. I have an if statement to check for args because I will be extending the script where if no args are found it will run anyway, and one arg will tell the script the directory to look in.

The issue is, this code does not work:

if [ -z "$#" ]
    then
        for i in *.c; do
            echo "We found a .c file"
       done
fi

But then if I add the echo "Test" in, it works?

if [ -z "$#" ]
    echo "Test"
    then
        for i in *.c; do
            echo "We found a .c file"
       done
fi

I'm new to bash and no clue why this is happening. Can anyone help me out?

Aucun commentaire:

Enregistrer un commentaire