Technofunction

Linux Shell Scripting Basics

Shell scripts are text files containing commands to be executed.

Shell scripts are useful for:

  • Automating commonly used commands
  • Performing system administration and troubleshooting
  • Creating simple applications
  • Manipulation of text or files.

A shell script is simply a text file containing commands. Scripts are useful for automating processes that you perform repeatedly at the command line. For example suppose every morning when you log in, you perform the following operations:

  • Check the system dare.
  • Look at the calendar for thr current month.
  • Check your email.
  • Display the list of logged in users.

Instead of entering the commands to perform steps 1-4 individually, you could create a shell script containing those commands. Every morning you could enter just one command to perform all these steps. In addition the key benefit of using Linux is using the powerful, yet simple commands. These commands may be put together in a script to perform complex operations or automate procedures such as adding batches of users.

Linux users involved with system administration and troubleshooting often work with shell scripts. Many of these are created during the installation of the operating system. Consider the script /etc/profile. This file is the system- wide user login script which runs whenever a user logs into the system. System administrators and troubleshooters will consider operations performed in this script when troubleshooting user login problems.

Programmers often create simple versions of programs using scripts during the initial phases of a programming project. This is called application prototyping. Once they and the program’s users are happy with the main functionality of the program, the programmer will then create the full-featured program in a programming language such as C.

Linux Shell Script to Determine whether a file exist or not

Shell script is a series of command’s written in a plain text file. It is somewhat similar to the batch-file in MS-DOS but have much more power and functionality than MS-DOS batch file. The major advantage is that you don’t need to write the same set of instructions again, since you can execute it via your script file.  It is majorly used to automate the tasks of day to day life and moreover can be used to automate the system administration part.

Below is an example of a script file by which you can determine whether a file exists or not. This script is used to tell you only whether file exists or not, but not its location as to where it is located. For that you can use FIND OR LOCATE command. We have created a file named existence-of-file.sh you may create a file with some other name. But it is convenient to save the file with ( .sh ) extension so that at later point of time you can recognize that it is a script file. Also in below code whatever is in front of # is a comment.

(more…)