DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Shell Scripting (http://www.daniweb.com/forums/forum113.html)
-   -   Script to Change file permissions (http://www.daniweb.com/forums/thread199231.html)

grvs Jun 23rd, 2009 8:08 am
Script to Change file permissions
 
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)

#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

sknake Jun 23rd, 2009 8:56 am
Re: Script to Change file permissions
 
1.
#!/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.

grvs Jun 23rd, 2009 10:38 am
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.

sknake Jun 23rd, 2009 10:51 am
Re: Script to Change file permissions
 
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:
sk@sk:~$ CURDIR=`pwd`
sk@sk:~$ echo ${CURDIR}
/home/wheel/sk
sk@sk:~$ cd /tmp
sk@sk:/tmp$ echo ${CURDIR}
/home/wheel/sk

sknake Jun 23rd, 2009 10:54 am
Re: Script to Change file permissions
 
This is a better illustration of using ticks. It evaluates the command instead of taking the literal value:
sk@sk:/tmp$ CURDIR=pwd
sk@sk:/tmp$ echo ${CURDIR}
pwd
sk@sk:/tmp$ CURDIR=`pwd`
sk@sk:/tmp$ echo ${CURDIR}
/tmp

grvs Jun 23rd, 2009 11:55 am
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.

sknake Jun 23rd, 2009 12:03 pm
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:

sk@sk:/tmp$ if test -f /etc/passwd
> then
> echo "it exists"
> else
> echo "it doesnt exist"
> fi
it exists

grvs Jun 23rd, 2009 1:19 pm
Re: Script to Change file permissions
 
ok.... that helps..
thanks again.


All times are GMT -4. The time now is 2:54 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC