LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 10-14-2009, 07:36 AM   #1
kaplan71
Member
 
Registered: Nov 2003
Posts: 716

Rep: Reputation: 39
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.
 
Old 10-14-2009, 07:58 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,512
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
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.
 
Old 10-14-2009, 08:07 AM   #3
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
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.
 
Old 10-14-2009, 08:11 AM   #4
kaplan71
Member
 
Registered: Nov 2003
Posts: 716

Original Poster
Rep: Reputation: 39
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.
 
Old 10-14-2009, 08:20 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,089

Rep: Reputation: 207Reputation: 207Reputation: 207
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.
 
Old 10-14-2009, 08:32 AM   #6
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Quote:
Originally Posted by kaplan71 View Post
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
 
Old 10-14-2009, 09:53 AM   #7
kaplan71
Member
 
Registered: Nov 2003
Posts: 716

Original Poster
Rep: Reputation: 39
Hi there --

Thanks for the help everyone. The script created by rizhun is working great.
 
Old 10-14-2009, 07:57 PM   #8
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 1,353

Rep: Reputation: 205Reputation: 205Reputation: 205
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.
 
Old 10-14-2009, 10:32 PM   #9
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,029

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
I suggest ditching the whole thread and studying "find" instead. :P

Example:

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question of syntax kaplan71 Linux - Software 1 05-05-2009 02:45 PM
question about C++ syntax bx.s Programming 3 07-27-2006 07:50 PM
C++ syntax question jiml8 Programming 6 06-20-2006 04:19 PM
Syntax question satimis Linux - Newbie 9 09-23-2004 07:47 AM
Syntax question satimis Linux - Newbie 4 10-08-2003 09:33 PM


All times are GMT -5. The time now is 08:11 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration