BASH Scripting ~ aprendizagem por exemplos
Introduction ~
—————————————–
Program (1) ~ array.sh
—————————————–
#!/bin/bash
echo “==============”
declare -a myarr[0]=”Arun”
declare -a myarr1
myarr1=(arun bagul bangalore mumbai raju santhosh)
myarr[1]=”Bagul”
echo “my name is ${myarr[0]} ${myarr[1]}”
echo “————————–”
echo “${myarr1[*]}”
echo ${myarr1[2]}
echo ${myarr1[@]}
echo “————————–”
echo “Total no of elements in array – ${#myarr1[*]}”
echo “Total no of elements in array – ${#myarr1[@]}”
echo “Size of word ‘${myarr1[2]}’ is – ${#myarr1[2]}”
echo ${#myarr1[1]}
echo ${#myarr1[0]}
echo “————————–”
#how to delete element in array
unset myarr[1]
echo “myarr is – ${myarr[*]}”
#how to assign element in array
myarr[1]=”- System Engineer!”
echo “myarr is – ${myarr[*]}”
echo ${myarr}
————————————————————————
Program (2) ~ command_line_arguments.sh
————————————————————————
#!/bin/bash
echo “Script/command name => $0”
echo “arg1 => $1”
echo “arg2 => $2”
echo “arg3 => $3”
echo “Total No of argument = $#”
echo “Script PID is => $$”
echo “Status of previous command – $?”
name=$myname
echo “Name – $name”
read n
————————————————-
Program (3) ~ default_value.sh
————————————————-
#!/bin/bash
#start=’123′
#start=${1:-$start}
start=${1:-‘123’}
echo “Value of ‘start’ variable is ==> $start”
—————————————————
Program (4) ~ echo_example.sh
—————————————————
#!/bin/bash
name=”Arun”
echo -e “My Name is $name_arun and \n”
echo -e “My Name is ${name}_arun and \n”
echo -e ‘My Name is $name and \n’
—————————————–
Program (5) ~ elif.sh
—————————————–
#! /bin/bash
if [ $1 -eq $2 ];then
echo “good”
elif [ $2 -eq $3 ];then
echo “Fine”
elif [ $1 -eq $3 ];then
echo “OK”
else
echo “NO”
fi
————————————————————
Program (6) ~ for_loop_example-1.sh
————————————————————
#!/bin/bash
i=1
while [ $i -le 512 ]
do
temp=$i
echo “What is => $i | $temp”
i=$(expr $i + 32)
for (( j=$temp; $j<=$i; j++ ))
do
echo -n ” $j”
done
done
———————————————————–
Program (7) ~ for_loop_example-2.sh
———————————————————–
#!/bin/bash
#for val in $(ls -1 /tmp)
sum=0
#for val in {1..5}
#for val in {$1..$2}
for((val=$1;$val<=$2;val++))
do
#echo “$val”
sum=$(expr $sum + $val )
#sum=`expr $sum + $val`
done
echo “$0 # Sum of $1 to $2 => $sum”
————————————————————
Program (8) ~ for_loop_example-3.sh
————————————————————
#!/bin/bash
for i in {1..9}
do
echo $i
done
—————————————–
Program (9) ~ function.sh
—————————————–
#!/bin/bash
function my_function()
{
name=”Arun Bagul”
echo “‘my_function’ body ~ $name”
return 1;
}
##########
myfunc()
{
echo “Another way of defining the function”
}
##########################
echo “Starting function program”
echo “——————————”
#calling function here
my_function
##
myfunc
echo -e “\n end of program”
—————————————————————————————
Program (10) ~ how_to_pass_argument_to_function.sh
—————————————————————————————
#!/bin/bash
function my_function()
{
echo “Total number of argument ~ $#”
echo “Arg1 => $1”
echo “Arg2 => $2”
echo “Arg3 => $3”
return 0;
}
##########
echo “Starting function program”
echo “——————————”
#calling function here
my_function arun bagul 1234
————————————————————————-
Program (11) ~ how_to_take_hidden_input.sh
————————————————————————-
#!/bin/bash
echo -n “Enter User Name :”
read username
echo -n “Enter Password :”
read -s mypwd
echo -e “\nI am $username and my password is – $mypwd”
——————————————————————————
Program (12) ~ how_to_take_input_from_user.sh
—————————————————————————–
#!/bin/bash
echo -ne “Enter the Name:- ”
read name
echo -n -e “Enter the Number:- ”
read num
echo “——————————”
add=$(expr $num + 10)
echo “Name is ~ $name”
echo “Number is ~ $add”
—————————————–
Program (13) ~ ifthen.sh
—————————————–
#!/bin/bash
if [ “arun” == “arun” ];then
echo “true!”
else
echo “false”
fi
echo “———————————-”
if [ 2 == 2 ];then
echo “true!”
else
echo “false”
fi
echo “———————————-”
if [ “arun” = “arun” ];then
echo “true!”
else
echo “false”
fi
echo “———————————-”
if [ 2 -eq 2 ];then
echo “true!”
else
echo “false”
fi
——————————————————
Program (14) ~ non-interactive.sh
——————————————————
#!/usr/bin/expect -f
spawn ssh arun@192.168.0.1
expect “password:”
sleep 1
send “pwd\r”
interact
—————————————————————-
Program (15) ~ read_file_line_by_line.sh
—————————————————————-
#!/bin/bash
file_name=”/etc/hosts”
while read myvar
do
echo “Line => $myvar”
done < $file_name
echo “#################################################”
for myvar1 in $(cat $file_name)
do
echo “Line => $myvar1”
done
——————————————————
Program (16) ~ reverse-number.sh
——————————————————
#!/bin/bash
declare -a date_array
num=$1
i=$(expr $(echo $num | wc -c) – 1 )
while [ $num -gt 10 ]
do
temp=$( expr $num % 10 )
num=$( expr $num / 10);
echo “Digit($i) => $temp”
date_array[$i]=”${temp}”
i=$(expr $i – 1)
done
echo “Digit($i) => $num”
date_array[$i]=”${num}”
echo ${date_array[*]}
——————————————————–
Program (17) ~ string-operation.sh
——————————————————–
#! /bin/bash
echo “Arun Bagul:-”
string=”/root/arun/bagul/image.gif”
echo “string=> $string”
echo “String=> ${string##/*/}”
echo “String=> ${string#/*/}”
echo “String=> ${string%.*}”
echo “String=> ${string%%.*}”
#str=”/home/y/conf/arunbagul/daily_market_0.conf”
str=”${str##/*conf/}”
echo “String=> ${str%/*}”
#done
mystr=”keyword_summary_exact_arunsb”
echo $mystr
echo ${mystr%_*}
echo “$*”
—————————————–
Program (18) ~ switch.sh
—————————————–
#!/bin/bash
echo ” Switch program | arg1 => $1″
echo ” ——————————-”
case $1 in
123)
echo “Case is 123”
;;
arun)
echo “Case is ‘arun'”
;;
pri*)
echo “Case is ‘pri*'”
;;
*)
echo ” * Usage: $0 ”
echo ” Default case (nothing is matched)”
exit 0;
;;
esac
—————————————————————–
Program (19) ~ while_loop_example-1.sh
——————————————————————
#!/bin/bash
mywait=wait
while [ “${mywait}” = “wait” ]
do
echo “arun”
done
——————————————————————-
Program (20) ~ while_loop_example-2.sh
——————————————————————–
#! /bin/bash
## on command line -> i=0 && while [ $i -le 10 ] ; do echo $i; i=$(expr $i + 1); done
i=0
while [ $i -le 10 ]
do
echo $i
i=$(expr $i + 1)
done
——————————————————————–
* Please download PDF file http://www.slideshare.net/arunbagul/bash-learning-by-examples/
Regards,
Arun