| | |
Script to Change file permissions
Thread Solved
![]() |
•
•
Join Date: Jun 2008
Posts: 47
Reputation:
Solved Threads: 0
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)
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
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)
#GrantExPer #Usage: GrantExPer <filename> str1='ls -la ' #first string str2=$1 # second string contains file name echo $str2 # this works comm1=$str1 $str2 #command1 by concatenating string 1 and string 2, this doesn't work for me $comm1 #execute command1 var1=$1 #get output of the above command in some var1 #find out current permissions using var1 and calculate some integer $newPermission (which contains new permissions) chmod $newPermission $str2 #str2 still contains file name I think
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
life's like math
oh btw... I haven't done PhD
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.
bash Syntax (Toggle Plain Text)
#!/bin/bash if [ "$1" = "" ]; then echo "usage: $0 [filename]" exit 1 fi if ! test -f $1 then echo "Invalid file name" exit 1 fi 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.
•
•
Join Date: Jun 2008
Posts: 47
Reputation:
Solved Threads: 0
thanks...
other than error handling there is only one line...
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.
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
life's like math
oh btw... I haven't done PhD
Shell Scripting Syntax (Toggle Plain Text)
sk@sk:~$ STR1='i' sk@sk:~$ STR2='d' sk@sk:~$ "${STR1}${STR2}" 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)
sk@sk:~$ CURDIR=`pwd` sk@sk:~$ echo ${CURDIR} /home/wheel/sk sk@sk:~$ cd /tmp sk@sk:/tmp$ echo ${CURDIR} /home/wheel/sk
This is a better illustration of using ticks. It evaluates the command instead of taking the literal value:
Shell Scripting Syntax (Toggle Plain Text)
sk@sk:/tmp$ CURDIR=pwd sk@sk:/tmp$ echo ${CURDIR} pwd sk@sk:/tmp$ CURDIR=`pwd` sk@sk:/tmp$ echo ${CURDIR} /tmp
•
•
Join Date: Jun 2008
Posts: 47
Reputation:
Solved Threads: 0
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.
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
life's like math
oh btw... I haven't done PhD
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:

There may be a few minor differences but I can't think if any. Here is an example:
Shell Scripting Syntax (Toggle Plain Text)
sk@sk:/tmp$ if test -f /etc/passwd > then > echo "it exists" > else > echo "it doesnt exist" > fi it exists
![]() |
Similar Threads
- How to set file permissions with C#? (C#)
- how to change file name? (VB.NET)
- Calling perl from java and file permissions (Perl)
- Python FTP script to upload file (Python)
- setting permissions locally (Perl)
- Convert a string to an integer (PHP)
- Logon Script - Batch file assistance required (Windows NT / 2000 / XP)
- Why my shell script doesn't delete a file ?? (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: BMI calculator?
- Next Thread: Arithmetic Evaluation and Pipes in BASH
| Thread Tools | Search this Thread |






