top of page

Creating an executable file in UNIX

Updated: Oct 20, 2023

1. Creating a shell script:

To create a shell script, you open a text file in a text editor such as vi editor

For example, let’s say that you decide to write a shell script for the Korn shell that automates the repetitive task of creating a file and folder structure for a new travel package.

You open a new text file in vi and – in this case – name it add_pkg_files.

The names you need to use for the new files and directories depend on the names of the travel packages. Therefore, the shell script needs to prompt the user to supply the package name. It then needs to read the name and store it as a variable.

#!/bin/ksh
clear
echo "Please enter the name of the new travel package: "
read PNAME

You need to create a directory for the new package in /usr/shared, and you need to create “bookings” and “info” sub-directories in the package directory.

You do this using a series of mkdir statements.

#!/bin/ksh
clear
echo "Please enter the name of the new travel package: "
read PNAME
ls /usr/shared/$PNAME || mkdir /usr/shared/$PNAME
cd /usr/shared/$PNAME
mkdir bookings
mkdir info

You need to create three empty text files in the new folder hierarchy – one for flights, one for accommodation, and one for bookings.

You do this using a series of touch statements.

#!/bin/ksh
clear
echo "Please enter the name of the new travel package: "
read PNAME
ls /usr/shared/$PNAME && mkdir /usr/shared/$PNAME
cd /usr/shared/$PNAME
mkdir bookings
mkdir info
touch /bookings/$PNAME_bookings
touch /info/$PNAME_accom
touch /info/$PNAME_flights

Finally, you want the script to display a message to the user that confirms that the files and folders have been created successfully.

#!/bin/ksh
clear
echo "Please enter the name of the new travel package: "
read PNAME
ls /usr/shared/$PNAME && mkdir /usr/shared/$PNAME
cd /usr/shared/$PNAME
mkdir bookings
mkdir info
touch /bookings/$PNAME_bookings
touch /info/$PNAME_accom
touch /info/$PNAME_flights
echo "Files and folders for $PNAME created in /usr/shared."

After you’ve saved and closed the text file containing your new script, you can confirm its existence using the ls -l command. In this example, you’ve called the script add_pkg_files.

This command reveals that the file doesn’t have execute permissions set for it. Therefore, you can’t run it as a script.

$ ls -l
total 2
-rw-r--r--  1  vincep  vincep  326  Feb 11 11:56  add_pkg_files
$

When you specify a set of permissions, you need to specify the permissions that apply to

  1. the file’s owner

  2. users in the file owner’s group

  3. everyone else

You can specify permissions in either symbolic or numeric form.

In symbolic form, you use the letters r, w, and x to denote read, write, and execute permissions respectively.

To make a script executable using symbolic form, you use the chmod +x command.

$ chmod +x add_pkg_files
$

In numeric form, you specify permissions using a three-digit octal code. The numbers from 0 to 7 represent increasingly open permissions, with 0 specifying no access and 7 specifying read, write, and execute permissions.

The first digit applies to the file’s owner, the second to the owner’s group, and the third to all other users.

Let’s say that you need to allow everyone to execute a script, but you don’t want anyone else to be able to write to the script.

You can do this by specifying the script’s permissions as 755.

$ chmod 755 add_pkg_files
$

Let’s say that you’re a member of a group of developers and you want other members of the group to be able to edit your script. On the other hand, you don’t want general users to be able to use the script at all.

In this case, you would need to specify the script’s permissions as 770.

$ chmod 770 add_pkg_files
$

Running ls -l again reveals that the script file is now executable.

$ ls -l
total 2
-rwxrwx---  1  vincep  vincep  326  Feb 11 11:56  add_pkg_files
$

2. Running a shell script:

You can execute a shell script by

  1. executing it directly

  2. invoking the shell

  3. using your environment

executing it directly

Direct execution is the simplest way to execute a script. You navigate to the directory in which the script is stored and type its name at the shell prompt, preceded by ./.

Instead of navigating to the script’s directory, you can specify its full file path.

invoking the shell

Invoking the shell to execute a script involves running an instance of the shell, and specifying the path to the script as well as the script name. This allows you to specify shell options.

using your environment

Using your environment to execute a script requires that the script is located in a directory that is found in the $PATH environmental variable. This allows you to execute the script by simply typing its name, regardless of your current working directory.

To run the add_pkg_files script using direct execution, you type ./add_pkg_files at the command prompt.

This causes the shell to spawn a child shell process, also called a subshell. The script executes in the subshell, which returns you to the main shell once the script has finished running.

$ ./add_pkg_files

You can run the script in the background, which allows you to use the main shell while the subshell executes the script.

$ ./add_pkg_files &

You can run the script by invoking the shell. To do this in the Korn shell, for example, you type /bin/ksh add_pkg_files. As with direct execution, the script runs in a subshell.

$ /bin/ksh add_pkg_files

If you have added your script directory to the $PATH statement in your .profile file, you don’t need to use ./ or the shell to run the script.

In this case, you only need to type the name of the script. This causes the script to run in a subshell.

$ add_pkg_files

If you don’t want to run the script in a subshell, you can precede it with the keyword source. This causes the current main shell to run each command in the script in sequence.

You can’t do anything else with the main shell while the script is executing.

$ source add_pkg_files

You use the at command to run a script at a specified future time.

In this example, you specify that the script must run at 6:00 pm on October 12.

You can also use more informal time specifications like 1pm tomorrow or even teatime, which means 4:00 pm.

$ at 6pm Oct 12 add_pkg_files

You use the cron command to run a script at regular intervals.

You need to make an entry in the /etc/crontab file, specifying the name of the script and the interval at which to run it.

Then you run the cron command, which starts a process that checks the /etc/crontabs file each minute and executes any jobs scheduled for the current minute.

$ cron

You use the batch command to run a script as soon as system load levels are low.

When you run a script with the batch command, the shell queues it as a pending job until the system load average falls below 1.5.

$batch add_pkg_files

*EOD


10 views0 comments

Recent Posts

See All

Understanding UNIX shell scripts

Understanding UNIX shell scripts Audience: This tutorial assumes you have little knowledge about UNIX/Linux Operating System and its...

Comments


IT solutions to achieve operational excellence

bottom of page