Script to Change file permissions

Thread Solved
Reply

Join Date: Jun 2008
Posts: 47
Reputation: grvs is an unknown quantity at this point 
Solved Threads: 0
grvs grvs is offline Offline
Light Poster

Script to Change file permissions

 
0
  #1
Jun 23rd, 2009
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
When I was in 10th i thought I knew all the maths, when I came to graduate level, I thought there is something I didn't know about, and when I completed my PhD, i knew that I don't know anything about maths.
life's like math
oh btw... I haven't done PhD
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,134
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 552
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Script to Change file permissions

 
1
  #2
Jun 23rd, 2009
1.
  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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 47
Reputation: grvs is an unknown quantity at this point 
Solved Threads: 0
grvs grvs is offline Offline
Light Poster

Re: Script to Change file permissions

 
0
  #3
Jun 23rd, 2009
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.
When I was in 10th i thought I knew all the maths, when I came to graduate level, I thought there is something I didn't know about, and when I completed my PhD, i knew that I don't know anything about maths.
life's like math
oh btw... I haven't done PhD
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,134
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 552
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Script to Change file permissions

 
0
  #4
Jun 23rd, 2009
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
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,134
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 552
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Script to Change file permissions

 
0
  #5
Jun 23rd, 2009
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
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 47
Reputation: grvs is an unknown quantity at this point 
Solved Threads: 0
grvs grvs is offline Offline
Light Poster

Re: Script to Change file permissions

 
0
  #6
Jun 23rd, 2009
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.
When I was in 10th i thought I knew all the maths, when I came to graduate level, I thought there is something I didn't know about, and when I completed my PhD, i knew that I don't know anything about maths.
life's like math
oh btw... I haven't done PhD
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,134
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 552
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Script to Change file permissions

 
0
  #7
Jun 23rd, 2009
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
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 47
Reputation: grvs is an unknown quantity at this point 
Solved Threads: 0
grvs grvs is offline Offline
Light Poster

Re: Script to Change file permissions

 
0
  #8
Jun 23rd, 2009
ok.... that helps..
thanks again.
When I was in 10th i thought I knew all the maths, when I came to graduate level, I thought there is something I didn't know about, and when I completed my PhD, i knew that I don't know anything about maths.
life's like math
oh btw... I haven't done PhD
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC