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:

  1. a typo in the variable name
  2. 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

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -