You are not logged in.
Pages: 1
Alright, this is pathetic that I have to ask but I can't figure it out for the life of me. I want a simple bash script that will take an argument (file name) and check if it exists. I have the following
if test -e $1
then
echo "true"
else
echo "false"
fi
It works, but only if the argument has no spaces. For example, the following don't work:
fileexists.sh "file name.jpg"
fileexists.sh file name.jpg
As you can see, I've tried escaping the space, putting the entire file name in spaces, I've tried putting quotes around the $1 in the script, etc. Please someone put me out of this misery :x
I am a gated community.
Offline
put quotes around the $1 parameter.
this works for me:
if [ -e "$1" ] ; then
echo 'true'
else
echo 'false'
fi
Offline
if [ -f "$@" ]; then
echo true
else
echo false
fi
The $@ allows testing against all arguments, which is needed even when your spaces are escaped.
There are a bunch of conditional tests in bash. For a complete list, type man bash, then search for the phrase "CONDITIONAL EXPRESSIONS".
Offline
Dusty, that is too weird that your way works but the way I put quotes around the $1 (below) didn't.
if test -e "$1"
Ah well, just glad to be able to use it. Thanks guys!
I am a gated community.
Offline
Pages: 1