how to check that the "file path" entered by the user has the valid file.
I think we discussed a similar requirement in post #13 of this thread.
File tests are mostly the same in shell/perl.
Read the manpages of "test" --> man test
I usually do this with file command. If the exit status is returned in success, it means the file or the path is okay.
verifying a single file and then checking the exit status.
# file /usr/share/readline/inputrc
/usr/share/readline/inputrc: ASCII English text
# echo $?
0
#
verify the existing of directory
# file /usr/share/readline
/usr/share/readline: directory
# echo $?
0
#
Similarly, verifying a non existing file with exit status.
# file /usr/share/readline/ThisWillbeAnError
/usr/share/readline/ThisWillbeAnError: ERROR: cannot open `/usr/share/readline/ThisWillbeAnError' (No such file or directory)
#echo $?
1
#
alternatively, you can do a test on the file/path using
[[ -d /path/to/directory ]]
in
if else
structure.
like:
if [[ -d /home/user ]]; then
echo "/home/user is a directory"
fi
let me elaborate it a bit..
the path entered by the user is supposed to be something like "$ABCD/defef/geft.txt" and i want to check if anything out of "$ABCD/defef/geft.txt" is mistyped. The script should exit.
and if, only $ABCD or defef or gef.txt is entered by the user, still script should exit.
i tried this
$Input_filename=$ARGV[0];
if (!-e $Input_filename $$ !-d $Input_filename)
{
print "the file does not exist, Please enter a valid file name";
exit;
}
please suggest where i am mistaken...
why are you using "$$"...are you by any chance refering to pid of the current shell script or this is a typo for "&&" which is AND in condition matching..
shouldn't be that
if (!-e $Input_filename && !-d $Input_filename)
---------- Post updated at 02:02 PM ---------- Previous update was at 01:54 PM ----------
2ndly
if you making test using if-else try using [[ ]] braces or the test built-in. This is more approperiate...never used ( ) for testing
if [[ ! -e $Input_filename && !-d $Input_filename ]]
or
if test ! -e $Input_filename -a -d $Input_filename
hi busyboy...
That '$$' was a typing mistake.
thanks for the code its working fine....
welcome