Linux Topic
   >  Shell Scripts
   >  Using Comments
   >  Passing Arguments to Scripts
   >  Using Conditional Statements
   >  Using Loop Statements
   >  Reading and Writing Files
   >  Script Permissions
   >  Cron and Scheduling
   >  A Scripting Example
   >  Other Scripting Languages


Shell Script Permissions

Checking Shell Script Permissions

Before you can run a shell script, you need to assign it Execute permission. This is simple and is done using the chmod command. To first check your current permissions, use the ls -l (-or ll for short) command to list the properties of your file:

$ ls -l
-rw-r--r-- 1 fredb users 210 2011-01-24 18:24<

The permissions are shown in the first part of the listing (-rw-r--r--) and are not obvious ot first glance. The first "-" can be ignored as this relates to the directory / if the su bit is set, but the rest may look like greek to you at present. To explain how this is interpreted, we need to step back a little and explain how Linux privileges work.

There are three basic privileges in Linux:

  • r : read privilege
  • w : write privilege
  • x : execute privilege

On top of this, there are three types of user that you can grant access to:

  • owner : the owner of the file (-the owner is set with the chown) command). The first 3 bytes of the lloutput relate to the owner
  • group : the group of users assigned to this file (-the group is also set with the chown) command). The next 3 bytes of the ll output relate to the group
  • others : anyone else. The last 3 bytes of the ll output relate to anyone who is not the owner or a member of the group assigned to this file

Each of these three types of user can have any combination of r/w/x privilege, hence the -rw-r--r-- is telling you:

  • the owner of the file (fredb) has read and write privilege to this file (rw-)
  • any users belonging to the group assigned to the file (users) will have only read privilege to this file (r--)
  • anyone else has read privilege (r--)

If you find your script does not have execute (x) permission for the users who need to run it, you'll need to use chmod to assign it as per the following section.

Setting Shell Script Permissions

There are a couple of alternative syntaxes for chmod but as this guide is aimed at new Linux users, we're going to use the (slightly) more user friendly version. The general syntax we need to use to assign execute privilege is:

chmod <user type>+x <filename>

The <user type> needs to be one of the following:

  • u : the (user) owner of the file
  • g : the user group assigned to the file
  • o : anyone else (others)
  • a : all users (owner, group and others)

So. let's try out a few examples: first let's assign ourselves as owner execute permissions:

$ chmod u+x

Now let's assign execute permissions to the group:

$ chmod g+x

Finally, let's assign execute permissions to the world (-not something you really ought to do without a lot of thought, I know):

$ chmod o+x

Of course, we could have reduced our work by two thirds by using:

$ chmod a+x

Note: if you ever need to remove permissions, use the same syntax, but substitute a minus (-) for the plus (+) - for example: chmod o-x

HomeSite IndexDesktop GuideServer GuideHints and TipsHardware CornerVideo SectionContact Us

 sitelock verified Firefox Download Button