4

I'm new to bash and I can't get a script working. This is the code that I have:

#!/bin/bash
file=~/scripts/text.txt

if [ -e $file ]
then echo "The file exists"

elif [ ! -e $file ]
then echo "The file does NOT exist"

fi

sleep 1s

if [ ! -w $file ]
then { sudo chmod u+w $file; } && { echo "The file is now writable"; }

elif [ -w $file ]
then echo "The file is already writable"
fi

The top part of the script, that checks if the file exists, works just fine. The bottom part, that checks if the file is writable, partly works. It will change the permissions of the file. But after that, with write permissions enabled, it will still echo "The file is now writable" instead of "The file is already writable"

Like I said, I'm new to bash so any help would be appreciated. Please keep it simple, because if statements are about as complex as I can go with my current knowledge.

slm
  • 369,824
Andrew
  • 41

4 Answers4

3

It's weird that you're using sudo to change the permissions on the file, even though it's in your home directory. Does the file belong to you? chmod u+w only gives write permission to the owner of the file. If you are the owner, you don't need sudo. If you aren't the owner, you do need sudo, but chmod u+w only gives the owner write permission, it doesn't grant you permission to write to the file. So after running chmod u+w, the file is still not writable by you, which is what [ -w … ] tests.

Run ls -l ~/scripts/text.txt to check the ownership and permissions on the file.

Aside from this, always put double quotes around variable substitutions. (Or you can learn all the rules and make your script hard to maintain by people who don't know all the rules.) Annoyingly, $file in shell syntax doesn't mean “take the value of the variable file”, it means “take the value of the variable file, parse it as a list of wildcard patterns, and replace each pattern by the list of matching files if there are matching files”. This extra stuff is turned off if the substitution happens inside double quotes: "$file" means “take the value of the variable file”.

Also, you don't need all these braces.

if [ ! -w "$file" ]
then
  sudo chmod u+w "$file" && echo "The file is now writable"
elif [ -w "$file" ]
then
  echo "The file is already writable"
fi

Since [ -w "$file" ] is the negation of [ ! -w "$file" ], the elif test will always succeed if it is reached. (Unless there's a race condition, in which case the elif test that tests the same condition again may return a different result, which is bad.) So you can and should simplify this to

if [ ! -w "$file" ]
then
  sudo chmod u+w "$file" && echo "The file is now writable"
else
  echo "The file is already writable"
fi
  • I will try to remember this information in the future. Like I said this is one of my first scripts and I had no idea that I should use double quotes around variables. – Andrew Aug 06 '13 at 19:27
2

You're missing a quote.

echo "The file is now writable;

Should be

echo "The file is now writable";

Now that you fixed that, I think that you're only changing permissions for root (not 100% familiar with the alphabetical permissions scheme) as u stands for the owner, which is root.

You may want to try using a or o, now making the line

then { sudo chmod a+w $file; } && { echo "The file is now writable"; }

Additional things: You may want to exit 1 when the file doesn't exist.

0

Try out below code.

#!/bin/bash
file=~/scripts/text.txt
# To check existence of the file
[ -e $file ] && E="The file exists" || E="The file does NOT exists"
echo "$E"

# checking whether the file is writable or not and changing permissions

[ -w $file ] && F="The file is already Writable" || F="The file is now Writable"
chmod u+w $file 
echo "$F"
0

Tested with below script and worked fine

Below script will test for write condition for currently login user, if write permission was not there it will add write permission only if file owner is same current login user

If in case if you want write permission to all then use sudo chmod a+w path

#!/bin/bash
path="/scripts/text.txt"
if [[ -f $path ]]
then
echo "$path file exsists"
if [[ -w $path ]]
then
echo "$path file is writable"
else
echo "$path file is not writable"
echo "Now we are adding write acess to the file"
chmod u+w $path
if [[ -w $path ]]
then
echo "$path  file is writable now"
fi
fi
else
echo "$path file doesnt exsists"
fi