EpsilonAlpha
EpsilonAlpha

Reputation: 35

If File Exist is always false

I found a couple of other questions like this but their answers don't fit in my case.

For a video stream recording project I wrote a script that searches for a "free name" which will be given back to the recording program so the same file will not be overwritten:

 #!/bin/bash
 AUFPFAD="$1"
 DATEINUMMER=0
 DATEISTRING=""
 ERLEDIGT="nein"
 TESTING=1


 if  [ $DATEINUMMER -eq 0 ]; then
     if [ -d "$AUFPFAD/temp.mp4" ]; then
         if [ $TESTING -eq 1 ]; then
             echo "Die Datei $AUFPFAD/temp.mp4 ist vorhanden!"
         fi
         DATEINUMMER=$DATEINUMMER+1;
     else
         if [ $TESTING -eq 1 ]; then
             echo "Die Datei $AUFPFAD/temp.mp4 ist nicht vorhanden!"
         fi
         DATEISTRING="temp.mp4";
     fi
 else
     while [ $ERLEDIGT == "nein" ]
     do
         if [ -d "$AUFPFAD/temp$DATEINUMMER.mp4" ]; then
             if [ $TESTING -eq 1 ]; then
                 echo "Die Datei $AUFPFAD/temp$DATEINUMMER.mp4 gibt's schon, also eines weiter und erneut prüfen"
             fi
             DATEINUMMER=$DATEINUMMER+1;
         else
             if [ $TESTING -eq 1 ]; then
                 echo "Die Datei $AUFPFAD/temp$DATEINUMMER.mp4 gibt's nicht, erstelle sie damit wir testen ob's dann weiter geht"
             fi
             DATEISTRING="temp$DATEINUMMER.mp4";
             ERLEDIGT="ja";
         fi
     done
 fi

 if [ $TESTING -eq 1 ]; then
     echo $DATEISTRING;
 else
     return $DATEISTRING;
 fi

To test the Script I created the folder "test", and created with touch the file "temp.mp4" inside that folder:

root@Ubuntu-1604-xenial-64-minimal /test # ls
temp.mp4
root@Ubuntu-1604-xenial-64-minimal /test #

But when I run the script I get this result:

root@Ubuntu-1604-xenial-64-minimal /test # /root/Dropbox/Skripte/skripte_CX30/filecheck.sh /test
Die Datei /test/temp.mp4 ist nicht vorhanden!
temp.mp4
root@Ubuntu-1604-xenial-64-minimal /test #

and that doesn't make sense because the file is there and has the correct name, which should work.

if [ -d "$AUFPFAD/temp.mp4" ]; then

going in TRUE but it's obviously FALSE and tbh I don't get it why.

What I have tested:

  1. Putting the expression without " in:

    if [ -d $AUFPFAD/temp.mp4 ]; then
    

    same result.

I'm pretty sure I overlooked something.

Upvotes: 2

Views: 1244

Answers (1)

Marcel
Marcel

Reputation: 3258

Use -e to check if a file exists:

if [ -e "$AUFPFAD/temp.mp4" ]; then

-d is used for directories.

The following operators returns true if file exists:

   -b FILE
          FILE exists and is block special
   -c FILE
          FILE exists and is character special
   -d FILE
          FILE exists and is a directory
   -e FILE
          FILE exists
   -f FILE
          FILE exists and is a regular file
   -g FILE
          FILE exists and is set-group-ID
   -G FILE
          FILE exists and is owned by the effective group ID
   -h FILE
          FILE exists and is a symbolic link (same as -L)
   -k FILE
          FILE exists and has its sticky bit set
   -L FILE
          FILE exists and is a symbolic link (same as -h)
   -O FILE
          FILE exists and is owned by the effective user ID
   -p FILE
          FILE exists and is a named pipe
   -r FILE
          FILE exists and read permission is granted
   -s FILE
          FILE exists and has a size greater than zero
   -S FILE
          FILE exists and is a socket
   -t FD  file descriptor FD is opened on a terminal
   -u FILE
          FILE exists and its set-user-ID bit is set
   -w FILE
          FILE exists and write permission is granted
   -x FILE
          FILE exists and execute (or search) permission is granted

Upvotes: 3

Related Questions