1

I am trying to write a script that runs on boot that checks to see if the boot filesystem is read only, if it is, then run fsck to fix permissions and reboot.

Point 1: I am having trouble figuring out how to grep just the permissions line from a ls command.

Point 2: I am also having trouble figuring out how to check if that command returns a string without w in it.

For example, an ls -l command returns a line similar to the following:

drwx-----+ 5 Admin staff 170 Oct 12 05:41 Documents

I want to grab just the following string:

drwx-----+

Then, check if it does not contain write permissions.

Below is the script I have so far.

#!/bin/bash

$DIR='home'

#If $DIR has only read permissions run loop
if [[ #point 1 = #point 2 ]] then
    #fix permissions on $DIR
    umount ${DIR}
    fsck
    reboot
fi
  • It's easy to do what you want : if ls -l dir | awk '{print $1}'|grep w; then ... but it makes totally no reason to do that in such compilated and resourse-consuming way. – gena2x Nov 11 '16 at 15:50
  • @gena2x Thanks for your comment, however, is there a way to check if a single directory contains write permissions? That command returns the permissions of each directory within the directory I specified. – Nicholas Adamou Nov 11 '16 at 15:53
  • Why do you parse ls output? The shell has some native features to test permissions of files (from the callers point of view); alternatively, there is the stat command. Or you may use the find command with a suitable search expression. – countermode Nov 11 '16 at 15:54
1

Do not ever parse the output of ls. Scripting 101.

man find
man findmnt

The root file system is never mounted read-only if the system has reached the multiuser target or runlevel. This means that the only user who can ever find the root file system mounted read-only is root. And root can write everywhere regardless of permissions. Therefore, to check whether the root filesystem is readonly you can simply try to touch a file:

if touch /testfile ; then
# The root filesystem is read-write
  rm /testfile
else
# The root filesystem is read-only
# Do something about it
fi

However, this should not be needed. The system should drop into a single-user shell if it cannot mount the root file system read-write.

3
if ! [[ -w "$DIR" ]]; then
    # $DIR is not writeable
fi

This will apply for the current user though. Don't know if that's what you want.

EDIT: If you do need to extract the permissions string, a simple way is by using stat:

stat -c %A "$DIR"

returns something like:

-rwxr-xr-x
  • 1
    is not writeable... by the caller. Here, the caller is most likely root, so will have write access as long as the filesystem is not mounted read-only regardless of the permissions. – Stéphane Chazelas Nov 11 '16 at 15:54
  • See my edit just now. – Muzer Nov 11 '16 at 15:56

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.