What is shell script ?
Shell
Shell provides the environment for user interaction. It is a CLI (command language interpreter) that executed command read from the standard input devices or files.
Computer understand only binary language ie. 0’s and 1’s. In early days of computer, instruction was provided using binary language, which was tough for all, to read, write and understand. That’s the reason why the especial program called Shell is introduced in OS. Shell accepts instruction or commands and if it’s a valid command, it passes to kernel.
Several shells are available with Linux
· BASH ( Boune-Again Shell) - Most common Shell in Linux and its freeware shell.
· CSH (S Shell) - The C shell’ syntax and usage are similar to C programming language.
· TCSH – enhanced and completely compatible version of Berkeley UNIX C shell. (CSH)
· KSH (Korn Shell)
Shell script
Thousand of commands are available for the command line user and need to work with it daily or repeatedly. How can one remember them all?. The solution of it is getting work done using the power of shell to automate things, we write scripts for it.
Scripts are simply the series of commands that are stored in a text file. The shell can read this file and act on the commands as if they were typed at the keyboard. Moreover the features available on various form of shell made is really powerful and realistic. It is similar to batch file in MS-DOS but more powerful than MS-DOS batch file.
Why shell script?
Firstly the shell script is really powerful tool as it directly interacts with kernel and some reasons like:
- User can create the command of their own.
- To automate some task of day-to-day life
- Takes input from user, file and output them to user through display.
- System administration part can be also be automated.
- Save lots of time i.e not required to type the lines of command each time, everyday.
-
Writing simple script.
To successfully write a shell script, you have to do three things:
- Write a script
Use any editor like vi or gedit to write shell script and write:
#! /bin/bash
# My first script
echo ' helo this is my first script ' ;
Save your file with some descriptive name. How about my_script?
The first line of the script is important. This is a special clue given to the shell indicating what program is used to interpret the script. In this case, it is #! /bin/bash (SHE-BANG). Other scripting languages such as perl, awk, tcl, Tk, and python can also use this mechanism.
The second line is a comment. Everything that appears after a "#" symbol is ignored by bash. As your scripts become bigger and more complicated, comments become vital. They are used by programmers to explain what is going on so that others can figure it out. The last line is theecho command. This command simply prints what it is given on the display.
- Give the shell permission to execute it
- Syntax
chmod permission script_name
Eg. chmod 755 my_script
[read, write, execute( 7)– permission for owner, for group and for other permission is read and execute only (5) .]
- Execute script as
syntax:
./script_name
sh script_name
eg.
$ ./my_script
$sh my_script
Output: First script will be appear in terminal.
For further details ways of writing shell scripts and to learn about writing the complete and complex script you can see the sites.
- http://www.freeos.com
- newa's blog
- Login or register to post comments
Who's new
- brorpamod
- akbro
- bishal
- sheelakk
- alfa
Who's online
Online users
- crackjack
