unix - Check if a file exists or not in shell in a particular directory -
this question might seem silly, stumped, not sure wrong. anyways, have piece of code checks if particular file exists in directory or not. code works fine normal files, normal files mean.. of sort "abc.xlsx or xaga1.xlsx". problem when pass file spaces, seems fail, mean.. check fails. i'll go straight code:
if [ ! -e "$file" ] echo_time "error: $file not exist - aborting" >> $filechangedlog exit 1 fi
if check in log file output being redirected, can see following line:
[03/17/16 11:07:09] error: "/opt/just4share/tellabs/tellabs_nodes 2.xlsx" not exist - aborting
but if ls error path following output:
[netcool@hjkpnlin03 scripts]$ ls -lrt "/opt/just4share/tellabs/tellabs_nodes 2.xlsx" -rw-rw-r--+ 1 netcool ncoadmin 17412 mar 17 10:43 /opt/just4share/tellabs/tellabs_nodes 2.xlsx
this confirms file exists however, code fails find it. doing wrong here? :|
edit: have tried -f switch in place of -e switch
well, figured problem, appending double quote character path variable $file in attempt enclose path in double quotes, removing double quotes did trick.
old code:
file="\"$directory/$file\""
new code:
file="$directory/$file"
Comments
Post a Comment