Unix Shell Scripts   «Prev  Next»
Lesson 8Setting the file permissions
ObjectiveSet up a file so it can be executed as a script.

Setting the File Permissions

Question: What steps need to be taken to set up a file, so it can be executed as a Unix Shel script?
To set up a file so it can be executed as a Unix Shell script, you need to follow several steps. These steps ensure that the file is properly formatted, recognized by the Unix shell as a script, and has the appropriate permissions to be executed. Here's a step-by-step guide:
  1. Create the Script File:
    • You can use any text editor to create a new file. Common text editors in Unix include `vi`, `vim`, `nano`, and `gedit`. For example, to create a file named `my_script.sh` using `nano`, you would type `nano my_script.sh` in the terminal and press Enter.
  2. Add the Shebang Line at the Top of the File:
    • The very first line of the script should be a shebang (`#!`) followed by the path to the interpreter that should execute the script. For a shell script, this is typically `#!/bin/bash` for Bash scripts or `#!/bin/sh` for Bourne shell scripts. This line tells the system that this file is a script and specifies the interpreter to be used.
    • Example:
  3. Write Your Script:
    • Below the shebang line, write the commands that you want your script to execute. Each command should be on a new line. You can use all commands that you normally use in the terminal, control structures like loops and conditionals, and script-specific syntax.
  4. Save and Close the File:
    • After you have written your script, save the changes and exit the text editor. For example, in `nano`, you would press `Ctrl + O` to save the file and `Ctrl + X` to exit.
  5. Make the Script Executable:
    • By default, new files are not executable. You need to change the file permissions to make your script executable. This can be done with the `chmod` command. To make `my_script.sh` executable, you would use the command `chmod +x my_script.sh`.
    • This command modifies the file's permissions, adding the executable (`x`) permission for the file's owner (and optionally for the group and others, depending on what's required).
  6. Execute the Script:
    • Once the script is executable, you can run it. There are a couple of ways to do this:
      • By specifying the interpreter explicitly: `bash my_script.sh` or `sh my_script.sh`, depending on the shebang line you used.
      • By invoking the script file directly, if it's in your PATH or by specifying the path to the script. If you're in the same directory as the script, you can use `./my_script.sh` to execute it.
  7. Optional: Add Script to PATH:
    • If you want your script to be executable from anywhere, not just the directory where it's located, you can add its directory to your PATH environment variable or move/copy the script to a directory that's already in your PATH, such as `/usr/local/bin`.
  8. Troubleshooting:
    • If your script doesn't run as expected, check for syntax errors or permissions issues. You can use `bash -n my_script.sh` to check for syntax errors without executing the script.
    • Ensure the file has Unix line endings (`LF`) if you're editing the script on a Windows machine, as Windows line endings (`CRLF`) can cause issues in Unix environments.
Following these steps will allow you to set up and execute a Unix Shell script. Remember that the exact shebang line and shell commands you use will depend on the specific shell you're writing the script for (e.g., Bash, Bourne Shell, Zsh, etc.).

Once you have a shell script created as a text file containing valid commands, you must set the file permissions on that text file so that UNIX can execute the script.
UNIX file permissions are divided into Read, Write, and Execute permission for the owner of a file, the group the owner belongs to, and all other users on the system. Before you can execute a script, you must at least activate the Execute permission[1] option for the owner of the file (that’s you). If you want other users on the UNIX system to have permission to execute the script, you can activate the group or other Execute permission. Even the i>UNIX root user[2] (superuser)) cannot execute a script until the Execute permission option has been set. Until then, UNIX simply does not recognize the file as something that can be run.

Viewing Permissions

To view the permissions of your script file, use the ls command with the -l option. The set of letters on the left side of the line shows the file permissions, three for the owner, three for users in your group, and three for other users on the system.
If all of these were set, you would see rwxrwxrwx. This is what you will see if you are using your DispersedNet Labs account (the file size, owner name, and date will differ, of course, but notice the permissions on the left side of the line):
% ls -l welcome
rw-r—r-- 1  nwells  students   13  Sep 2  16:39

Setting Permissions

To set or change the file permissions on your script file so they include the Execute permission option., use the chmod command.In the following example, the parameter u refers to the user, or owner of the file. x refers to the Execute permission. The command looks like this:
% chmod u+x welcome

You can also set the Execute permission for the group or others by adding a g or an o, respectively. To set the permission for all three, the command would look like this:
% chmod ugo+x welcome

Or, if you prefer the abbreviated version, you can use the letter a, for all (user, group, and other).
% chmod a+x welcome

File Permissions - Exercise

Click on the Exercise link below to try setting up correct file permissions in your UNIX Labs account.
Setting File Permissions - Exercise
With the file permissions set, you are ready to run your script in the next lesson.

[1]Execute permission: A file permission within UNIX file systems that allows a program to be launched or executed.
[2]UNIX root user: The administrative account on a UNIX system; the superuser who has all access to the system.

SEMrush Software8