inteliture.com
Google

Saturday, July 14, 2007

Shell scripting for linux

Introduction to shell scripting.

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.

Reasons why to write one.

  • Shell script can take input from user, file and output them on screen.
  • Useful to create our own commands.
  • Save lots of time.
  • To automate some task of day today life.
  • System Administration part can be also automated.
Some examples.

Example No1

$ vi first
#
# My first shell script
#
clear
echo "Knowledge is Power"

Example No2

$ vi ginfo
#
#
# Script to print user information who currently login , current date & time
#
clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0

Important notes.

To write a shell script, u can use any editor like vi or mcedit. For the above examples we used the vi.

After writing ur shell script(s), set execute permission by typing $ chmod permission your-script-name ex. $ chmod 755 first

Then to run it type $ ./your-script-name ex. ./first

NOTE: The syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell. The syntax for . (dot) command is $ . command-name ex. $ . first

NOTE: Read the "VI editor" tutorial and "Linux commands" tutorial in this section if not yet.

No comments: