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)

#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.


7 Years
Discussion Span
Last Post by grvs
Featured Replies
  • 1
    sknake 1,622   7 Years Ago

    1. [code=bash] #!/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 [/code] 2. Put the above code in grant.sh, chmod a+x grant.sh, ./grant.sh 3. You can use 'stat' … Read More



if [ "$1" = "" ]; then
  echo "usage: $0 [filename]"
  exit 1

if ! test -f $1
  echo "Invalid file name"
  exit 1

chmod u+x $1

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

You can use 'stat' to get octal permissions. There are a number of approaches outlined at http://www.lockergnome.com/linux/2003/07/11/many-ways-to-see-numeric-permissions/ on getting octal perms.

I actually first disliked as he solved the problem for me, but then I saw there were few hidden things for me. Also the solution was just of two words, so now I cant even blame him for the solution... 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.

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}
sk@sk:~$ cd /tmp
sk@sk:/tmp$ echo ${CURDIR}

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}
sk@sk:/tmp$ CURDIR=`pwd`
sk@sk:/tmp$ echo ${CURDIR}


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.


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
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.