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.

  • 1
    If you are the owner of the file, you don't need sudo to chmod. – jordanm Aug 5 '13 at 19:44
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 6 '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.

  • I fixed the missing quote mark in the code above, but the missing quote mark is not missing in my code. – Andrew Aug 5 '13 at 19:13
  • @Andrew revised my answer – tdlive aw'sum Aug 5 '13 at 19:19
  • The a+w revision worked. I haven't been using if statements for very long and apparently it was checking for write permissions for everyone. Thanks – Andrew Aug 5 '13 at 19:27
  • The "u" is not current user, it's the owner of the file. – jordanm Aug 5 '13 at 19:44
  • Got "current user" from here, will update – tdlive aw'sum Aug 5 '13 at 19:45
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"
  • This doesn't explain why that code is better than the question, or why it is better than the other answers. – RalfFriedl Jun 23 at 12:44
  • This code is simple and I haven't compared it with others. For a beginner looping in shell might be difficult to understand – Super Cool Bucket Jun 23 at 13:53
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.