Bash

时间:2016-03-27 17:36:22

标签: bash file shell testing

我想使用Bash脚本来测试文件的存在但得到"没有文件存在..."即使文件ACTUALLY存在,也会显示消息:

#!/bin/bash
# Usage : myscript.sh AAAAMMJJ
# where AAAAMMJJ is the argument passed to the script  

d_date=$1

# No accurate content here...
# $d_date value is 20160708
# $d_year value is 2016
# $d_month value is 07
# $d_day value is 08

# Directory path
p_path="/home/user/mydir/${year}"

# Filename is something like: my-file_20160708z.html
f_file="${$p_path}/my-file_${d_date}z.html"

# Testing if file exists
[ ! -f "${f_file}" ] && echo "File OK" || echo "no file..."

使用这种构造测试文件存在的正确方法是什么?这与另一个文件(" .txt"文件)完美配合。

1 个答案:

答案 0 :(得分:2)

您写道:

# Testing if file exists
[ -f "${f_file}" ] && echo "File OK" || echo "no file..."

你应该写的:

man test

说明

-f

  

(表达)

     

EXPRESSION是真的

     

!表达

     

EXPRESSION是假的

因此,当你只写一个表达式时,成功就意味着真实。

  

-f FILE

     

存在FILE并且是常规文件

所以,如果你想测试一个存在的文件,你不会通过放置!来否定spriteBatch.setProjectionMatrix(camera.combined);测试,这会做相反的事情。

相关问题