LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   If...then...else syntax question (http://www.linuxquestions.org/questions/linux-software-2/if-then-else-syntax-question-761815/)

kaplan71 10-14-2009 07:36 AM

If...then...else syntax question
 
Hi there --

I am writing a script that is checking the ownership and permissions of a directory. If the directory in question does not have the correct ownership and permissions, the script will run the appropriate commands to give it the correct settings.

I need some help with the if...then...else syntax. The idea here is the following:

Code:

If <directory> no eq = <ownership root:root> && <permissions 755>
then chown root:root <directory> && chmod 755 <directory>
else exit
fi

What would the correct syntax be for the If line of the loop in question?

Thanks.

catkin 10-14-2009 07:58 AM

Assuming you are writing in bash script ...

There is no built in test for the conditions you want to test so you will have to get the information from the output of a command such as stat or ls.

rizhun 10-14-2009 08:07 AM

There's no built-in to check these things, so assuming you're writing this in bash, you'll have to get the info yourself:

Code:

#!/bin/bash

# setup variables
file="/absolute/path/to/file"
permissions=$(stat ${file} | awk '/Access: \(/ {print $2}' | cut -c3-5)
owner=$(ls -l ${file} | awk '{print $3}'
group=$(ls -l ${file} | awk '{print $4}'

# check permissions
[[ ${permissions} -ne 755 ]] && {
  chmod 755 ${file}
}

# check owner
[[ ${owner} == "root" ]] || {
  chown root ${file}
}

# check group
[[ ${group} == "root" ]] || {
  chgrp root ${file}
}

Have fun.

kaplan71 10-14-2009 08:11 AM

Hi there --

Thanks for your reply. The script is being written within the bash shell. I included the command syntax shown below in the script:

Code:

ls -l / |grep /archive
It occurred to me that it would probably be better for me to have the if...else loop, which immediately follows the above line, work in the following way:

Code:

If /archive eq = <ownership root:root> && <permissions 755>
then exit
else chown root:root /archive && chmod 755 /archive
fi

The question I have still centers on the correct syntax to use on the If line.

Guttorm 10-14-2009 08:20 AM

Hi

I'm a bit lazy and I write scripts sometimes. Instead of checking different things, and correcting if incorrect, I simply skip the checks.

So instead of something like

if permessions are incorrect
chmod ...
fi

This is easier:
chmod ...

If really doesn't matter if the permissions was wrong before...

Also, if you want to make a directory if it doesn't exist, you could write this:

if [ ! -e /somedir ] ; then
mkdir /somedir
fi

Simpler:
mkdir /somedir &>/dev/null

This means if the directory exists before, it will make an error message, but it's sent to /dev/null so it doesn't matter.

But if the script should check for incorrect things and give error message, then you have to write the checks.

rizhun 10-14-2009 08:32 AM

Quote:

Originally Posted by kaplan71 (Post 3718835)
The question I have still centers on the correct syntax to use on the If line.

Errrm... Maybe I should have explained; this is a short-hand 'if' statement:

Code:

[[ ${permissions} -ne 755 ]] && {
  chmod 755 ${file}
}

It could also be written as:

Code:

if [ ${permissions} -ne 755 ]; then
  chmod 755 ${file}
fi

There's no need for an If-Then-Else scenario.

@Guttorm -- You sir, are diiiirty ;)

kaplan71 10-14-2009 09:53 AM

Hi there --

Thanks for the help everyone. The script created by rizhun is working great.

Shadow_7 10-14-2009 07:57 PM

Sounds like it'd be easier to just make a list of directories that need changing. Then a second script to change them.

ls -l ./* | grep -i "^d" | grep -v -i "root:root" | tee -a list.txt

This also lets you check for false positives (/proc /dev / ???) and remove them prior to update.

cat list.txt | while read LINE; do THIS=`echo $LINE | awk 'print { $1 }'`; chown root:root $THIS; done

Or something like that. a) it's simpler and b) the list itself is a sort of built in backup. But I guess it depends on if it's a one off, or a regular automated thing that you're trying to do.

i92guboj 10-14-2009 10:32 PM

I suggest ditching the whole thread and studying "find" instead. :P :)

Example:

Code:

find . \! \( -user i92guboj -group i92guboj -perm 755 \) | xargs ls -l


All times are GMT -5. The time now is 10:53 AM.