943,589 Members | Top Members by Rank

Ad:
Jun 23rd, 2009
0

Script to Change file permissions

Expand Post »
Hi

I am new to unix and want to learn shell scripting.
Right now I am use cygwin for windows but sooner I will switch to ubantu.

So when I wrote my first script (just copied from the book I was reading), I came to know I have to give execute permission to the script file.

So now I want to create a script which will take the file as input and will just change the permissions such that It wont effect read/write permission but only grant the execute permissions.

So I tried to write something like this (code mixed with psuedocode is written below)

Shell Scripting Syntax (Toggle Plain Text)
  1. #GrantExPer
  2. #Usage: GrantExPer <filename>
  3.  
  4. str1='ls -la ' #first string
  5. str2=$1 # second string contains file name
  6. echo $str2 # this works
  7. comm1=$str1 $str2 #command1 by concatenating string 1 and string 2, this doesn't work for me
  8. $comm1 #execute command1
  9. var1=$1 #get output of the above command in some var1
  10. #find out current permissions using var1 and calculate some integer $newPermission (which contains new permissions)
  11. chmod $newPermission $str2 #str2 still contains file name I think
  12.  

Please guide me for:
1. How to make a right command. I tried it by concatenate 2 strings. (line 4-7)
2. Then how to execute it. (line 8)
3. Is there any command which shows me file permissions in a number format like 644 rather than -rw-r--r--. To me number format seems easier to manipulate.


Thanks
Similar Threads
Reputation Points: 27
Solved Threads: 0
Junior Poster in Training
grvs is offline Offline
70 posts
since Jun 2008
Jun 23rd, 2009
1

Re: Script to Change file permissions

1.
bash Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. if [ "$1" = "" ]; then
  3. echo "usage: $0 [filename]"
  4. exit 1
  5. fi
  6.  
  7. if ! test -f $1
  8. then
  9. echo "Invalid file name"
  10. exit 1
  11. fi
  12.  
  13. chmod u+x $1

2. Put the above code in grant.sh, chmod a+x grant.sh, ./grant.sh

3.
You can use 'stat' to get octal permissions. There are a number of approaches outlined at http://www.lockergnome.com/linux/200...c-permissions/ on getting octal perms.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 23rd, 2009
0

Re: Script to Change file permissions

thanks...

other than error handling there is only one line...
chmod u+x $1
and that's how we learn... I spent more than 1 hour on this....

Thank you very much...

but i still want to know: how to concatenate two strings to make a command and then how to execute that command. Or do we need to always find a workaround.
Last edited by grvs; Jun 23rd, 2009 at 10:41 am.
Reputation Points: 27
Solved Threads: 0
Junior Poster in Training
grvs is offline Offline
70 posts
since Jun 2008
Jun 23rd, 2009
0

Re: Script to Change file permissions

Shell Scripting Syntax (Toggle Plain Text)
  1. sk@sk:~$ STR1='i'
  2. sk@sk:~$ STR2='d'
  3. sk@sk:~$ "${STR1}${STR2}"
  4. uid=1000(sk) gid=110(wheel) groups=110(wheel),4(adm),43(utmp)

You can also use `` ticks to execute commands from output:
Shell Scripting Syntax (Toggle Plain Text)
  1. sk@sk:~$ CURDIR=`pwd`
  2. sk@sk:~$ echo ${CURDIR}
  3. /home/wheel/sk
  4. sk@sk:~$ cd /tmp
  5. sk@sk:/tmp$ echo ${CURDIR}
  6. /home/wheel/sk
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 23rd, 2009
0

Re: Script to Change file permissions

This is a better illustration of using ticks. It evaluates the command instead of taking the literal value:
Shell Scripting Syntax (Toggle Plain Text)
  1. sk@sk:/tmp$ CURDIR=pwd
  2. sk@sk:/tmp$ echo ${CURDIR}
  3. pwd
  4. sk@sk:/tmp$ CURDIR=`pwd`
  5. sk@sk:/tmp$ echo ${CURDIR}
  6. /tmp
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 23rd, 2009
0

Re: Script to Change file permissions

thanks...

Now I understand how it works on shell prompt.
I will try to find out if this works when written in a script.

edit: it works in scripts also...
thanks again.
Last edited by grvs; Jun 23rd, 2009 at 11:58 am.
Reputation Points: 27
Solved Threads: 0
Junior Poster in Training
grvs is offline Offline
70 posts
since Jun 2008
Jun 23rd, 2009
0

Re: Script to Change file permissions

The prompt and script are basically identical. If it works on the prompt it works in a script

There may be a few minor differences but I can't think if any. Here is an example:

Shell Scripting Syntax (Toggle Plain Text)
  1. sk@sk:/tmp$ if test -f /etc/passwd
  2. > then
  3. > echo "it exists"
  4. > else
  5. > echo "it doesnt exist"
  6. > fi
  7. it exists
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 23rd, 2009
0

Re: Script to Change file permissions

ok.... that helps..
thanks again.
Reputation Points: 27
Solved Threads: 0
Junior Poster in Training
grvs is offline Offline
70 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: BMI calculator?
Next Thread in Shell Scripting Forum Timeline: Arithmetic Evaluation and Pipes in BASH





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC