i Shell tutorial – part 2 – All things in moderation

Shell tutorial – part 2

Shell Script ?

• Shell script can take input from user, file and output them on screen
• To automate some task of day-to-day life
• Save lots of times
• Userfule to create own/custom commands

Creating Code Snippets

Creating abbreviations in .vimrc

Abbreviations allow for a shortcut string to be used in place of longer string.Just open ~./vimrc and type a shortcut code you want.
Syntax

abbr <shortcut><string>


Example

abbr _sh #!/bin/bash


Using this abbreviations, we just need type _sh while in edit mode. On pressing the ENTER key after shortcut code, the full text for shebang is printed.

It’s easy let enjoy it !

Bringing color to the terminal

Example
We create variables for some colors below :

RED="\033[31m"
GREEN="\033[32m"
BLUE="\033[34m"
RESET="\033[0m"


We create file hello.sh, which makes use of these colors

Result:

Loop Control

while loop
#!/bin/sh

a=12

until [ $a -lt 10 ] do echo$a
a=expr $a + 1' done  The break Statement The following break statement is used to come out of a loop #!/bin/sh a=0 while [$a -lt 10 ]
do
echo $a if [$a -eq 5 ]
then
break
fi
a='expr $a + 1' done  Result is : 0 1 2 3 4 5  break n  Here n specifies the nth enclosing loop to the exit from. dev/null to send unwanted output program Exmaple: $ls > dev/null



Output of above command is not shown on screen its send to this special file.

Regular Expressions with SED

SED stands for stream editor.
The sed General Syntax

/pattern/action


patten is a regular expression and action is one of these command following.

p: prints the line
d: delete the line
s/pattern1/parttern2/ : Substitutes the first occurrence of pattern1 with pattern2


For details you can reference link below :
tutorial point shell scripting : regular expression
This is some stuff hope it help full for you to start with bash shell, in the next post I’ll write some scirpts we mosty use for administrators.