More information: http://linuxconfig.org/Bash_scripting_Tutorial

----------------------------------------------------------------

--Basic Variable--

#!/bin/bash

STRING="Hello World"

echo $STRING

 

--Read in Variable--

#!/bin/bash

echo -e "Input: \c "

read  word

echo "Output: $word"

echo -e "Enter two words? "

read word1 word2

echo "Output: \"$word1\" \"$word2\""

echo -e "How do you feel about bash scripting? "

# read command now stores a reply into the default build-in variable $REPLY

read

echo "You said $REPLY"

echo -e "What are your favorite colours ? "

# -a makes read command to read into an array

read -a colours

echo "My favorite colours are also ${colours[0]}, ${colours[1]} and ${colours[2]}:-)"

--Arrays--

#!/bin/bash

#Declare array with 4 elements

ARRAY=( 'Debian Linux' 'Redhat Linux' Ubuntu Linux )

# get number of elements in the array

ELEMENTS=${#ARRAY[@]}

# echo each element in array

# for loop

for (( i=0;i<$ELEMENTS;i++)); do

echo ${ARRAY[${i}]}

done

 

--Read in a File--

#!/bin/bash

#Declare array

declare -a ARRAY

# Link filedescriptor 10 with stdin

exec 10<&0

# stdin replaced with a file supplied as a first argument

exec < $1

let count=0

while read LINE; do

ARRAY[$count]=$LINE

((count++))

done

echo Number of elements: ${#ARRAY[@]}

# echo array's content

echo ${ARRAY[@]}

# restore stdin from filedescriptor 10

# and close filedescriptor 10

exec 0<&10 10<&-

 

--IF / Else--

#!/bin/bash

directory="./BashScripting"

# bash check if directory exists

if [ -d $directory ]; then

echo "Directory exists"

else

echo "Directory does not exists"

fi

 

--Arithmentic Comparison--

-lt

-gt

-le

-ge

-eq

-ne

<=

>=

==

 

               

               

               

               

               

               

 

#!/bin/bash

# declare integers

NUM1=2

NUM2=2

if [ $NUM1 -eq $NUM2 ]; then

echo "Both Values are equal"

else

echo "Values are NOT equal"

fi

 

--String Comparison--

=

!=

-n s1

-z s1

Equal

Not equal

Less then

Greater then

String s1 is not empty

String s1 is empty

 

#!/bin/bash

#Declare string S1

S1="Bash"

#Declare string S2

S2="Scripting"

if [ $S1 = $S2 ]; then

echo "Both Strings are equal"

else

echo "Strings are NOT equal"

fi

--FOR Loop--

#!/bin/bash

# bash for loop

for f in $( ls /var/ ); do

echo $f

done

 

-          Running for loop from bash shell command line:

$ for f in $( ls /var/ ); do echo $f; done

 

--While Loop--

#!/bin/bash

COUNT=6

# bash while loop

while [ $COUNT -gt 0 ]; do

echo Value of count is: $COUNT

let COUNT=COUNT-1

done

--Until Loop--

#!/bin/bash

COUNT=0

# bash until loop

until [ $COUNT -gt 5 ]; do

echo Value of count is: $COUNT

let COUNT=COUNT+1

done

--Functions --

!/bin/bash

# BASH FUNCTIONS CAN BE DECLARED IN ANY ORDER

function function_B {

echo Function B.

}

function function_A {

echo $1

}

function function_D {

echo Function D.

}

function function_C {

echo $1

}

# FUNCTION CALLS

# Pass parameter to function A

function_A "Function A."

function_B

# Pass parameter to function C

function_C "Function C."

function_D

--Select--

#!/bin/bash

PS3='Choose one word: '

# bash select

select word in "linux" "bash" "scripting" "tutorial"

do

echo "The word you have selected is: $word"

# Break, otherwise endless loop

break 

done

exit 0

--CASE Condition--

#!/bin/bash

echo "What is your preferred programming / scripting language"

echo "1) bash"

echo "2) perl"

echo "3) phyton"

echo "4) c++"

echo "5) I do not know !"

read case;

#simple case bash structure

# note in this case $case is variable and does not have to

# be named case this is just an example

case $case in

1) echo "You selected bash";;

2) echo "You selected perl";;

3) echo "You selected phyton";;

4) echo "You selected c++";;

5) exit

esac