Asaf Nevo
Asaf Nevo

Reputation: 11678

Shell Script check if file exists, and has read permissions

In my shell script i'm trying to check if a specific file is exists and if it has reading permissions.

My file's path has spaces in it.

I quoted the file path:

file='/my/path/with\ some\ \spaces/file.txt'

This is the function to check if the file exists:

#Check if file exists and is readable
checkIfFileExists() {
    #Check if file exists
    if ! [ -e $1 ]; then
        error "$1 does not exists";
    fi

    #Check if file permissions allow reading
    if ! [ -r $1 ]; then
        error "$1 does not allow reading, please set the file permissions";
    fi
}

Here I double quote to make sure it gets the file as a one argument:

checkIfFileExists "'$file'";

And I receive an error from the bash saying:

[: too many arguments

Which make me thinks it doesn't get it as a one argument.

But in my custom error, I do get the whole path, and it says it doesn't exists.

Error: '/my/path/with\ some\ \spaces/file.txt' does not exists

Although it does exists, and when I tried to read it with "cat $file" I get a permission error..

what am I'm doing wrong?

Upvotes: 3

Views: 18731

Answers (2)

tripleee
tripleee

Reputation: 189387

The proper way to quote when you require variable interpolation is with double quotes:

if [ -e "$1" ]; then

You need similar quoting throughout the script, and the caller needs to quote or escape the string -- but not both. When you assign it, use one of these:

file='/my/path/with some spaces/file.txt'
# or
file=/my/path/with\ some\ spaces/file.txt
# or
file="/my/path/with some spaces/file.txt"

then use double quotes around the value to pass it in as a single argument:

checkIfFileExists "$file"

Again, where you need the variable's value to be interpolated, use double quotes.

For a quick illustration of what these quotes do, try this:

vnix$ printf '<<%s>>\n' "foo bar" "'baz quux'" '"ick poo"' \"ick poo\" ick\ poo
<<foo bar>>
<<'baz quux'>>
<<"ick poo">>
<<"ick>>
<<poo">>
<<ick poo>>

Furthermore, see also When to wrap quotes around a shell variable?

Upvotes: 2

Vishnu
Vishnu

Reputation: 220

if [[ -e $1 ]];then
 echo it exists
else
 echo it doesnt
fi

if [[ -r $1 ]];then
  echo readable
else
  echo not readable
fi

Upvotes: -1

Related Questions