Special File Types   «Prev 

Creating Shell Script

Here are the steps you would follow to create a simple shell script:
  1. You have already created a text file named trial.sh, whose contents are the same as those given in this lesson, in your home directory (/home/user1). Type head trial.sh to view the contents of the trial.sh file.
  2. List all details of only trial.sh by typing ls –l trial.sh.
  3. You can see that this file is not executable. Using the chmod command and the appropriate arguments, make the file executable by a user. Use symbolic mode.
    Solution:
    chmod u+x trial.sh
  4. Using the ls –l command, list the file again to make sure your changes have taken place.
  5. You can see that the file has the correct information in the first line (#!/bin/sh), and that it has the correct permissions (-rwxrw-r). However, as you learned in the last exercise, you must first set the current path to execute trial.sh. Update your path now.
    Solution: PATH=$PATH:.
  6. Export this PATH to a system variable.
    Solution: export PATH
  7. Execute the trial.sh file by typing trial.sh.
  8. Note that the shell runs, and uses the date command to render on the screen. This program will run indefinitely unless you press CTRL-C to end it. End the program.
  9. Run trial.sh again. This time, however, run it in the background. To do this, type trial.sh &. The & character automatically runs processes in the background.
  10. Now, with your trial.sh program running in the background, type ps to find out what types of processes are running.
  11. Note that by executing trial.sh, you have also begun another sh process. This is because the sh interpreter process executed your command. The trial.sh program is currently running in the background. Type fg to make the trial.sh program run in the foreground again.
  12. This program will run for 600 seconds unless you press CTRL-C to end it.
  13. You are now back at your login shell and are not running any extra shells or processes.