LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   locate directory and change permissions in bash script (https://www.linuxquestions.org/questions/linux-newbie-8/locate-directory-and-change-permissions-in-bash-script-897103/)

tony1969 08-13-2011 06:05 AM

locate directory and change permissions in bash script
 
i am trying to write a script that does the following..

1. checks if a directory exists
2. changes permisssions of the directory

i have written a script but it returns a message to say that the specified
directory does not exist (but it does).

my question is how to i search the entire file system as directory could potenially be anywhere. would cd or su be of any use here.

advanced apologies incase this is a dumb question but it's my first
thanks tony.

repo 08-13-2011 06:10 AM

Welcome to LQ.
Perhaps you can post the script which gives the error.

Kind regards

grail 08-13-2011 07:02 AM

I am with repo. Without seeing what you have done it is not possible to help further without just doing the work for you.

tony1969 08-13-2011 07:04 AM

my script is as follows...

#!/bin/bash
# author: t.holloran
# date: july 22 2011
# Purpose: locate directory and change permissions.

clear
directory_name=dname

function exall()
{
chmod 740 $dname
ls -sail | head
}
echo "Please enter a directory_name"
read dname
echo $dname " will now be revised"
zenity --question # are you sure you want to proceed NO YES

if [ -d $dname ]
then
echo "the present permissions are:"
cd $dname ; ls -sail | head
echo "requested changes have now been applied as below"

cd $dname
exall

else
echo "directory " $dname "does not exist!"
echo "please check and try again"
fi
#END

grail 08-13-2011 09:30 AM

Ok ... let us start at the beginning:

1. Please place code in [code][/code] tags so it can be read better and formatting is kept.

2. bash is a top down language, meaning you have a line:
Code:

directory_name=dname
The issue here is dname has not been set yet so directory_name will always be blank.

3. You need to provide more information to the user. You have asked for a directory name when in fact it is relative, meaning that any directory given must be in the current directory
to be accessed. Is this what you intended?

4. The following code does not make sense to me:
Code:

cd $dname ; ls -sail | head
echo "requested changes have now been applied as below"

cd $dname
exall

a. Are we interested in the directory or its contents?
b. head will just return everything from the ls command so what is the point?
c. requested changes have now been applied as below ... what changes? nothing has been done yet. Maybe it is a grammatical error here.
d. cd into the same directory again which implies that a directory with the same name is inside the one you already went into .. is this correct? if not this line will throw an error.
e. Now you call a command which is trying to change the permissions on yet another directory or file with the same name as the original directory??? Again I am not sure this is what you want.

5. Lastly, the function does the same pointless ls / head combination.


Now I know i didn't specifically answer any questions, but if you look into the above it should help you toward an answer.

I am happy to answer any further questions if you have them based on what I have written.


All times are GMT -5. The time now is 09:50 PM.