dimanche 27 mars 2016

Test file existence in Bash

I want to test existence of a file using a Bash script of but get "no file exists..." message even if the file ACTUALLY exists:

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

What is the proper way to test file existence with this kind of construct? This works perfectly with another file (".txt" file).

Aucun commentaire:

Enregistrer un commentaire