check if it's there

Dear,

I want to check if the file in the if code is there, but I don't know how to do it?
if [ ??? ]

Can someone assist me please?

Thanks!

kopie()
{
    echo " Geef de naam van bestand + plaats vanwaar gekopieerd moet worden en waarnaar : "
    echo -n " Plaats : "
    read plaats
    echo -n " Geef de naam van het te kopi�ren bestand : "
    read bestand
    echo -n " Directory naarwaar : "
    read naarwaar
    if [ $plaats/$bestand]
}

You can test for the existence of a file

if [ -f $plaats/bestand ] ; then   # note the spaces around the brackets
    echo "found it"
else
   echo "cannot find it"
fi
if [ -f $plaats/$bestand ] ; then
...

Thanks

I've got this now.
I still have 1 problem, in the last if [ -f $naarwaar/$bestand ] it always goes into the else, even if it's true.
If this last issue is fixed then I have my first working script in Ubuntu !!

Someone?

kopie()
{
    echo -n " Plaats van waar: "
    read plaats
    echo -n " Geef de naam van het te kopi�ren bestand : "
    read bestand
    echo -n " Directory naar waar : "
    read naarwaar
    if [ -f $plaats/$bestand ]
    then
        echo "Bestand $bestand bestaat in $plaats"
        if [ -f $naarwaar/$bestand ]
        then
            echo " Bestand bestaat reeds in $naarwaar"
        else
            echo "$naarwaar bestaat niet, of $bestand staat reeds in $naarwaar"
        fi
    else
    echo "Bestand $bestand bestaat niet in $plaats, of de directory $plaats bestaat niet"
    echo -n "Kopi�ren niet uitgevoerd"
    fi
}
kopie()
{
plaats="" ; naarwaar="" ; bestand=""
    echo -n " Plaats van waar: "
    read plaats
    echo -n " Geef de naam van het te kopi�ren bestand : "
    read bestand
    echo -n " Directory naar waar : "
    read naarwaar
[[ ! -d "$plaats" || ! -d "$naarwaar" ]] && echo "Warning: $plaats or $naarwaar is not a directory"
[[ ! -r "$plaats/$bestand" ]] && echo "cannot read  $plaats/$bestand"
if [ -f "$plaats/$bestand" ]
    then
        echo "Bestand $bestand bestaat in $plaats"
        if [ -f "$naarwaar/$bestand" ]
        then
            echo " Bestand bestaat reeds in $naarwaar"
        else
            echo "$naarwaar bestaat niet, of $bestand staat reeds in $naarwaar"
        fi
    else
    echo "Bestand $bestand bestaat niet in $plaats, of de directory $plaats bestaat niet"
    echo -n "Kopi�ren niet uitgevoerd"
    fi
}

OK,
this solves my question.
1 more thing, why do you type that first line? It works fine without it.
plaats="" ; naarwaar="" ; bestand=""

Thanks!

You true, maybe it is a totally useless caution from the pranoid novice scripter i am.

I initially mentionned the first line because if you had previously tested your function and that those variables were existing in your environnement (for any reasons), if those variables were still holding some nested values from previous test, it could have been an additionnal potential source of unexpected results so i just forced them to empty to make sure your test would run from cleaned variables.

But now that you've fixed your code and checked it is working as expected also without it, you can of course remove the line.

:slight_smile:

1 Like