BY , B. Gayathri.

.INTRODUCTION TO LINUX SHELL SCRIPTING: Shell accept command from you and execute them. But there will be a situation where you will need to use the same set of commands one by one. In such situation you can store these commands in a text file and then execute this text file instead of entering the commands one by one. Such file can be referred as shell scripts.

Write to a file. Automated some system administration task. To automate some day to day tasks.THE MAIN REASONS WHY TO WRITE SHELL SCRIPT? Shell scripts can take input from user. Save lots of time. file and output them on screen. .

GENERAL SYNTAX IN LINUX SHELL SCRIPTING. the file names. Case sensitivity: AS you know Linux is case sensitive. variables. and arrays used in shell scripts are also case sensitive. Examples: String1=10 String2=20 echo ´The value of string1:µ echo ´the value of string2:µ .

Adding comments. while editing the in future. make things easy for the programmer. Example: String1=10 # Assigning value for string1 String 2=20 # Assigning value for string2 # prints the values echo ´The value of String1:µ echo ´the value of String2:µ . This part of the code will be ignored by yhe program interpreter.COMMENTS: Comments are used to escape from the code.

System variables: create by Linux developers.SHELL VARIABLES: In Linux. these variables are defined in UPPER case. User variables: create by user. . variables are defined in lower case. there are two types of Linux.

VARIABLES IN LINUX SHELL SCRIPTING: System variables: To see the system variables try the commands: # set To prints the system variables use echo: Examples: echo $HOME] User defined variables: To define a user defined variables use the following syntax: #variables name = value How to access value of UDV? #use a $ symbol to access Example: value = 100 echo $value .

Back Quote : To execute command. Example: Single=¶single quotes· Double=´double quotesµ echo $single echo $double .STRINGS IN LINUX SHELL SCRIPTING: Define String: we use single or double quotations to define a string. ¶Single Quotes·: Enclose in single quotes remains unchanged. ´Double Quotesµ: Anything enclose in double quotes removed meaning of that characters.

STRING FORMATTING: Character Description: -n= do not output the trailing new line. \a alert \b backspace \c suppress trailing new line \n new line \r carriage return \t horizontal tab Example: echo ²e ´Hello \t welcome to Linux shell scriptingµ . -e = enable interpretation of the following backslash escaped characters in the string.

Syntaxµ expr op1 math-operator op2 Example: $expr 3 + 2 $expr 3 .2 $expr 3 * 2 $expr 3 \ 2 echo ¶$expr 3 + 2· .ARITHMETIC'S IN LINUX SHELL SCRIPTING: We use the keyword ´exprµ to perform arithmetic operations.

The read statement: used to get input from keyboard and store to variables. welcome to the world of Linuxµ . Syntax: variable1 ««« variableN. In shell scripts we use the read statement to take input from the user.USER INTERACTION IN SHELL SCRIPTS: In some cases the script needs to interact with the user and accepts inputs. Example: echo µ Enter your name:µ read name echo ´Hello $name.

<. .INPUT ² OUTPUT REDIRECTION IN SHELL SCRIPTS: Using shell scripts. they are >. There are mainly 3 types of redirection. we can redirect The output of a command to a file or redirect an output file as an input to other commands.>>.

Example: cat>friends (create new file) kani abi Press ctrl + d to save Command : sort < friends | tr ´[a-z]µ ´[A-Z]µ >FRIENDS .PIPES AND FILTERS: Pipes: A pipes is used to connect the output of one program or command to the input of another program or command without a temporary file we use the symbol ´|µ for pipes.

Using tee command. Filters: A filters is a program that accepts input transforms it or does something useful with it and outputs the transformed data. Example: ls ²l | grep ²a ´dµ | tee dir.listµ and the wc ²I prints the total number of lines in that file. . The grep command takes only the dir.list | wc ²l Ls command lists all the files and dir. we writes the result to the file ´dir.

. Syntax: If condition Then Execute some command «« «« fi .IF STATEMENT: This statement are conditional statements used to test whether the condition is true or false end then continue executing the script.

IF ELSE CONDITION: Syntax: If condition Then condition is true Execute some commands Else If condition is not true Execute some commands up fi .

Syntax: for {variable name} in {list} do {code to execute} done WHILE LOOP: A while loop is executed as long as a given condition is true. We can use for loop. Syntax: while[condition] do {code} « done . If we want our scripts to repeat the same set of code say around 10 or 100 times.FOR LOOP IN SHELL SCRIPTS: A for loop is used to repeat the same set of code.

. Syntax: case $variable name in patter1){code to execute}. *) {code to execute} esac .CASE STATEMENT: The case statement is used to match several values against one variable. The case statement is very useful when youwant to provide options for the users. patter2){code to execute}... pattern){code to execute}.

Thank you« .

Sign up to vote on this title
UsefulNot useful