Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找“中的哪个参数为假”;如果;在bash脚本中_Bash_If Statement - Fatal编程技术网

查找“中的哪个参数为假”;如果;在bash脚本中

查找“中的哪个参数为假”;如果;在bash脚本中,bash,if-statement,Bash,If Statement,我的目录中应该有三个文件。我需要知道它们中的哪一个在不同的时间丢失 我正在尝试使用“if”(放入crontab)的bash脚本 if[-f file1]&&&[-f file2]&&&[-f file3];然后 echo“所有三个都存在”>>日志文件 其他的 echo“不存在”>>日志文件 fi 我知道对所有文件分别使用嵌套的“if”来获取它是可能的。但我不想对每个文件使用单独的“if”。 我也知道可以使用for循环。但我想知道上述方法是否可行——将脚本保持在最小大小 谢谢 一般来说,无法确定

我的目录中应该有三个文件。我需要知道它们中的哪一个在不同的时间丢失
我正在尝试使用“if”(放入crontab)的bash脚本

if[-f file1]&&&[-f file2]&&&[-f file3];然后
echo“所有三个都存在”>>日志文件
其他的
echo“不存在”>>日志文件
fi
我知道对所有文件分别使用嵌套的“if”来获取它是可能的。但我不想对每个文件使用单独的“if”。 我也知道可以使用for循环。但我想知道上述方法是否可行——将脚本保持在最小大小


谢谢

一般来说,无法确定
&&
链中的哪个条件失败

但是,使用
for
循环并没有那么糟糕:

success=true
for f in file1 file2 file3; do
    if ! [ -f $f ]; then
        success=false
        echo "$f is not present" >> logfile
    fi
done
if $success; then
    echo "All three exist" >> logfile
fi

它还可以让您确定是否存在多个文件,以防这些信息有价值。

我正在考虑以下内容:

for i in {1..3} 
  do  
    ...do some things...file$i 
    ...do more things...
  done

但这将增加一个抽象层,而不是仅三个文件所需的抽象层。

创建一个函数来为您执行测试,然后使用该函数的副作用来检测布尔快捷方式:

##
# Test that a file exists. Here we use standard output to just visibly see
# that the function is running, but for a more programmatic solution, 
# store state: You could use a shared variable to store the names of files
# that you know exist, or you could just keep a counter of the number of times
# this function is run. Use your imagination.
file_exists() {
  printf 'Testing that %s exists\n' "$1"
  test -f "$1"
}

if file_exists file1 && file_exists file2 && file_exists file3; then
    …
fi

嗯。这是作弊,但是你可以做
test-f file1 | test-f file2 | test-f file3
,你会得到与测试类似的结果,但是你可以查看PIPESTATUS,看看到底是什么失败了。请不要这样做-它会创建n个子shell,不会创建布尔快捷方式,但是嘿。
##
# Test that a file exists. Here we use standard output to just visibly see
# that the function is running, but for a more programmatic solution, 
# store state: You could use a shared variable to store the names of files
# that you know exist, or you could just keep a counter of the number of times
# this function is run. Use your imagination.
file_exists() {
  printf 'Testing that %s exists\n' "$1"
  test -f "$1"
}

if file_exists file1 && file_exists file2 && file_exists file3; then
    …
fi