murpholinox
murpholinox

Reputation: 673

bash script showing file permissions gives wrong output

I am learning bash and wish you guys could help me out what's going on with the following script

#!/bin/bash

#primer if
if [ -f $file1 ]; then
        echo "file1 is a file"
else
        echo "file1 is not a regular file"
fi
#segundo if
if [ -r $file1 ]; then
        echo "file1 has read permission"
else
    echo "file1 doesnot have read permission"
fi
#tercer if
if [ -w $file1 ]; then
        echo "file1 has write permission"
else
    echo "file1 doesnot have write permission"
fi
#cuarto if
if [ -x $file1 ]; then
        echo "file1 has execute permission"
else
    echo "file1 doesnot have execute permission"
fi

It looks to me that it does not matter if I change the file permissions because output is always the same

fmp@eva00:~/Books/2012/ubuntu_unleashed$ ./script.sh 
file1 is a file
file1 has read permission
file1 has write permission
file1 has execute permission
fmp@eva00:~/Books/2012/ubuntu_unleashed$ ll file1
-rw-r--r-- 1 fmp fmp 0 Aug 30 13:21 file1
fmp@eva00:~/Books/2012/ubuntu_unleashed$ chmod 200 file1
fmp@eva00:~/Books/2012/ubuntu_unleashed$ ./script.sh 
file1 is a file
file1 has read permission
file1 has write permission
file1 has execute permission
fmp@eva00:~/Books/2012/ubuntu_unleashed$ ll file1
--w------- 1 fmp fmp 0 Aug 30 13:21 file1
fmp@eva00:~/Books/2012/ubuntu_unleashed$ chmod 000 file1 
fmp@eva00:~/Books/2012/ubuntu_unleashed$ ll file1
---------- 1 fmp fmp 0 Aug 30 13:21 file1
fmp@eva00:~/Books/2012/ubuntu_unleashed$ ./script.sh 
file1 is a file
file1 has read permission
file1 has write permission
file1 has execute permission

file1 can be empty or not and still output is the same, doing the same test

Can anybody explain to me what is wrong?

Thanks

BTW the script here is a modified version of compare3 on page 233 of ubuntu unleashed book 2011 edition (Book site http://ubuntuunleashed.com/)

Upvotes: 3

Views: 549

Answers (4)

glenn jackman
glenn jackman

Reputation: 246744

This is the reason all your tests are succeeding: since the variable is null, you get

if [ -f  ]; ...
if [ -r  ]; ...
if [ -w  ]; ...
if [ -x  ]; ...

Because you're using single brackets, bash only sees a single word for the test condition. When the test command sees only a single argument, the result is true if the word is not empty, and in each of these cases, the word contains 2 characters.

Of course the fix is to declare the variable. Also, you should be using bash's conditional construct

if [[ -f $file ]]; ...
if [[ -r $file ]]; ...
if [[ -w $file ]]; ...
if [[ -x $file ]]; ...

When the double brackets is used, bash will see 2 words in the condition even if the variable is empty or null.

Upvotes: 3

Jiri Kremser
Jiri Kremser

Reputation: 12837

or remove the dolar sign, or replace $file1 with $1 and use the script as ./script.sh file1

Upvotes: 2

dinox0r
dinox0r

Reputation: 16039

Change the $file1 variable for $1 or add the following at the begining of the script (after the #!/bin/bash):

set file1=$1

So you can invoke your script like this:

./script.sh file

Upvotes: 2

phsym
phsym

Reputation: 1384

file1 variable is undefined.

you should add at the beginning of your script file1="file1"

Upvotes: 4

Related Questions