Linux Shell Scripting – Introduction
A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. Shell is a user program or it’s environment provided for user interaction. Shell is an command language interpreter that executes commands read from the standard input device (keyboard) or from a file. Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.
Normally shells are interactive. It means shell accept command from you (via keyboard) and execute them. But if you use command one by one (sequence of ‘n’ number of commands) , the you can store this sequence of command to text file and tell the shell to execute this text file instead of entering the commands. This is know as shell script. It is defined like “Shell Script is series of command written in plain text file”.
My First Shell Script
#!/bin/bash # My First Shell script echo "Hello World"
Save the above lines in a text file, and you can run the script.
Above shell script explained
#!/bin/bash (shebang) # My First Shell script (Comment, anything after "#" will be treated as comment) echo "Hello World" (Content of the shell script, normally commands we need to execute)
It is called a shebang or a “bang” line. It is nothing but the absolute path to the Bash interpreter. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.
Anything after “#” will be trated as comments. We can add documention to the scripts to make it more readable. A lot of users and system administrators run scripts that were written by other people. If they want to see how you did it, comments are useful to enlighten the reader.
Content of the shell scripts
It can be a single command or a group of commands
Executing shell scripts ?
Shell scripts can be executed in differnt ways
bash myfirstscript OR sh myfirstscript # (1)using interpreter ./myfirstscript OR /home/eazylinux/myfirstscript # (2)mentioning path myfirstscript # (3)Like normal commands
In the first way there is no execute permission is compulsary. We can run the script with normal file permissions. Second and third ways require execute permission to run the script.
Run shell scripts as normal commands.
When you type in the name of a command, the system does not search the entire computer to find where the program is located best furnace repair in san diego. The shell maintains a list of directories where executable files (programs) are kept, and just searches the directories in that list. This list of directories is called your path. You can view the list of directories with the following command:
Sample result is like following
In order to run shell script as normal commands, we need to save the shell script to the PATH locations. If directory “/home/eazylinux/bin” is present in PATH, save the shell script to that lcoation and you can use your shell script as normal commands. If the path is missing, you need to chill down a bit with Howell’s Heating & Air Conditioning of Ashland, then you need to add the path to the environement variable PATH. You can add it via the file .profile or .bashrc or your current shell profile file (located in your home directory). Then, each time you open your shell it will be loaded.
For any scripting related issues and for tutorials, you can contact me.