bash - 如果文件存在始终为 false

标签 bash shell file

我发现了其他几个类似的问题,但他们的答案不适合我的情况。

对于视频流录制项目,我编写了一个脚本来搜索“自由名称”,该名称将返回给录制程序,以便同一个文件不会被覆盖:

 #!/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

为了测试脚本,我创建了文件夹“test”,并使用touch在该文件夹内创建了文件“temp.mp4” :

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

但是当我运行脚本时,我得到了这个结果:

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 #

这没有意义,因为文件就在那里并且具有正确的名称,这应该可以工作。

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

输入TRUE,但显然是FALSE,而且我不明白为什么。

我测试过的内容:

  1. 将不带 " 的表达式放入:

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

    相同的结果。

我很确定我忽略了一些事情。

最佳答案

使用-e检查文件是否存在:

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

-d 用于目录

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

关于bash - 如果文件存在始终为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39660982/

相关文章:

shell - Unix 命令行上简洁且可移植的 "join"

java - 我正在尝试从android studio内部存储中的.txt文件中读取文本

linux - sed:在文件中的一行之前插入一个带有反斜杠和换行符的宏

Python 3 不会从 Git Bash 命令行运行

shell - PowerShell:需要为某些字符串和显示名称+路径搜索几种不同文件类型的内容

linux - 使用变量替换创建文件并替换字符串中的某些字符

BASH 命令提示符 : add characters behind command

linux - 无法在linux中的for循环中处理目录的文件

PHP $_FILES 计数不正确

java - 获取java war文件中的资源文件夹