unix - ": No such file or directory" when file exists -
i'm having problems code, it's saying file not exist, when know does. code follows:
#! /bin/ksh echo "enter file name" read filename if [ -e $filename ];then less -m $filename gedit $filename else echo "$filename not file" fi
i've missed blindingly obvious... wish knew was. anyway, appreciated.
i see 2 options here:
- a typo in the variable name
- pressing enter without file name.
either way can resolved adding set -u
top of script, or using parameter expansion test: ${...:?}
finally, it's better deal exceptions first, , try report happens. error message in original script far removed actual test, , not reflect test did. in case, it's better let system tell what's wrong.
below example
#! /bin/ksh echo "enter file name" read filename && [[ -e ${filename:?} ]] || exit less -m ${filename:?} || exit gedit ${filename:?}
Comments
Post a Comment