Shell Scripting

Introduction of Shell Scripting



A shell script is a script written for the shell, or command line interpreter, of an operating system. Shell programming a list of commands stored in a file and executed sequentially.


Writing the script


sha-bang (#!)


Any shell script is started with the sha-bang ( #!)  at the head of a script tells your system that this file is a set of commands to be fed to the command interpreter indicated. The #! indicates that the file is an executable script.

After the sha-bang there is a path name. This is the path to the program that interprets the commands in the script. It can be a shell, a programming language or a utility. This command interpreter then executes the commands in the script, starting at the top, the line following the sha-bang line and ignoring comments.

Some examples are
#!/bin/sh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl
#!/bin/sed -f
#!/bin/awk –f

The path after the "sha-bang" must be correct. If it is wrong you will get an error message like "Command not found."


Shell Script


Running (executing the script)

Before running the script check the read and execute permission on the file name. If these file permission is not set then give the permission using chmod command.

chmod +rx filename
Or
chmod u+rx filename
Or
chmod 555 filename

Now you can run the script using command

./filename

The another method to run the script is using command

sh filename


Comment in shell script


You can comment using # the script.

Example:

Comment a line

# This line is a comment.

Comment after some command in line

ls –l     # comment here

Where # is not work as comment

1. In first line of script #!, here # is not used as comment.
2. Within a command # is not a comment.

Example:

echo ${PATH#*:}
here # is used as parameter substitution, not as comment.

Within quoting and escape character (“  ,    , /)


Semicolon

Semicolon is used to write more than one command in a single line. After first command we can use semicolon and can continue the next command on the same line.

Example:


echo "File $filename not found."; touch $filename



dot (.) in Linux

No comments: