SHELL-1.1 Shell Script Introduction and Basics


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/fuzicast

A shell script is basically a script that runs a bunch of unix commands along with conditional statements, loops statements. When programming shell script, space matters.
#!/bin/bash

# SETTING A VARIABLE
greeting=”Hello World”

# SETTING A GLOBAL/ENVIRONMENT VARIABLE
export blogname=”fuzicast”

# ASSIGN OUTPUT OF SUBSHELL TO VARIABLE
filenames=$(ls -ltr) # note that subshell inherits all environment variables from parent shell
no_of_files=`ls -ltr | wc -l`

# HOW TO CREATE MULTIPLE LINE COMMENT
<<COMMENT
The previous line begins a comment block, this is also known as HERE DOCUMENT
Here's another comment
The following line ends the comment block
COMMENT

# PERFORM ARITHMETIC CALCULATION
var1=45
var2=30
echo $[ $var1 + $var2 ]
(( sum=$var1 + $var2 ))
echo $sum

# IF STATEMENT
if [ $[ $var1 + $var2 ] -gt 70 ];then
echo "sum is greater than 70"
elif [ $[ $var1 + $var2 ] -lt 70 ];then
echo "sum is less than 70"
else
echo "unknown"
fi

# WHILE STATEMENT
var1=30
while [ $var1 -gt 0 ];do
echo "while \$var1 is greater than 0, keep running … "
var1=$[ $var1 – 1 ]
done

# UNTIL STATEMENT
var1=0
until [ $var1 -gt 30 ];do
echo "until \$var1 is greater than 30, keep running …"
var1=$[ $var1 + 1 ]
done

# FOR STATEMENT
for arg in $@;do
echo $arg
done

# CASE STATEMENT
var1=1
case $var1 in
1) echo "hello world"
echo "fuzicast video tutorials";;
2) echo "wrong option";;
3) echo "guess again";;
*) echo "Default option";;
esac

Advertisements

One thought on “SHELL-1.1 Shell Script Introduction and Basics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s