What is a Shell Script or shell scripting
Normally shells are interactive. It means the shell will accept command from you (via keyboard) and execute them. However, if you store a sequence of commands to a text file and tell the shell to execute the text file instead of entering the commands, that is known as a shell program or shell script.
Shell script defined as Shell script is a series of command(s) stored in a plain text file. A shell script is similar to a batch file in MS-DOS, but is much more powerfull.
Each shell script consists of
- Shell commands such as if..else, do..while
- Linux text processing utilities such as grep, awk, cut
- Linux binary commands such as w, who, free etc
Did you know?
- It is the shell that lets you run different commands without having to type the full pathname to them even when they do not exist in the current directory.
- It is the shell that expands wildcard characters, such as * or ?, thus saving you laborious typing.
- It is the shell that gives you the ability to run previously run commands without having to type the full command again by pressing the up arrow, or pulling up a complete list with the history command.
- It is the shell that does input, output and error redirection.
Why shell scripting?
- Shell scripts can take input from a user or file and output them to the screen.
- Whenever you find yourself doing the same task over and over again you should use scripting i.e. repetitive task automation:
- Time saving.
- Creating your own power tools/utilities.
- Customizing administrative task.
- Since scripts are well tested, the chances of errors are reduced while configuring services or system administration task such as adding new users:
- Practical examples where shell scripting actively used:
- Monitoring your Linux system
- Data backup and creating snapshots
- Dumping Oracle or MySQL database for backup
- Creating email based alert system
- Find out what processes are eating up your system resources
- Find out available and free memory
- Find out all logged in users and what they are doing
- Find out if all necessary network services are running or not. For example if web server failed then send an alert to system administrator via a pager or an email
- Find out all failed login attempt, if login attempt are continue repeatedly from same network IP automatically block all those IPs accessing our network/service via firewall
- User administration as per your own security policies
- Find out information about local or remote servers
- Configure server such as BIND (DNS server) to add zone entries
Shell scripting is fun. It is useful to create nice (perhaps ugly) things in shell scripting. Here are few script example I use everyday:
- Find out today's weather (useful when you are busy in a chat room)
- Find out what that site is running (just like netcraft)
- Download RSS feeds and display them as you login or in your email
- Find out the name of the MP3 file you are listening to
- Monitor your domain expiry date every day