Posts

Showing posts from April 10, 2011

Shell Scripting - part 2

Command Line Arguments ------------------------------- Here we will discuss some more about command line arguments( values passed along with script like flags in commands) and how they can be accessed in script 1)Name of the program The name of the program is stored in variable '0' So the value can be accessed as ${0} e.g:- vi file1.sh --------------------------------------------------------------------- #!/bin/bash echo ${0} --------------------------------------------------------------------- The output of the program will be like . /file.sh 2)All arguments passed along with script All values passed to the script(command line arguments) will be stored in variable '@' So the value can be accessed as ${@} e.g:- vi file2.sh --------------------------------------------------------------------- #!/bin/bash echo ${0} -------------------------------------------------

Shell Scripting - part 1

The echo command ------------------ The easiest command in shell script used for display text e.g:- echo "Hello world" Variables in script -------------------- variables are used for storing values It may be pe-defined or the value will be assigned on the fly (On execution of script) The variable manipulation in scripting VAR=value ${VAR} for retrieving the value assigned to VAR a)Pre-defined e.g:- VAR=10 b)On the fly e.g:- VAR=`expr ${i} + 1` In example (a) the variabe VAR is assigned a value of 10 The value of VAR will be 10 through out the execution life of script(unless that variable is not alterd by another assignment) In example (b)the variable VAR assigned a value that depend on the value of 'i' (another variable) We will split the example to understand two command expr ${i} + 1 and VAR= i) evaluat the expression $

Basic Shell Scripting-step by step

Image
Basic Shell Scripting-step by step If you are not a beginner go to summery page What is a script? Group of commands in one file for a specific or a number of actions to achieve a result 1. Select your editor You must have a text editor to write shell programs Select an editor in which you are comfortable Most common editors are Vi and Emacs 2. Structure of a script a)First line “#!/path/to/shell” b) Comments Purpose of script, author, created date, modifications, bug fix notes etc c) Body of script Commands to be executed This line will decide which shell to be used for executing the commands in script If the line is not present, system will use default shell E.g.:- 1 ------------------------------------ #!/bin/bash echo “Hello world” E.g. Explained ------------------------------------ The above script will execute echo “Hello world” in bash shell 3. Execute a scrip