Wednesday, May 23, 2018

Trap in shell script,invoking subroutine using a key combination,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
# 23/05/2018
#
echo "Usage of trap in Shell Script,for example a key combination can invoke a subroutine"
trap catchCtrlC INT
# Initialize the trap
# Subroutines
catchCtrlC()
{
echo "Entering catchCtrlC routine."
}
# Code starts here
echo "Press Ctrl-C to trigger the trap, 'Q' to exit."
loop=0
while [ $loop -eq 0 ]
do
read -t 1 -n 1 str
rc=$?
if [ $rc -gt 128 ] ; then
echo "Timeout exceeded."
fi
if [[ "$str" = "q" || "$str" = "Q" ]] ; then
echo "Exiting the script."
# wait 1 sec for input or for 1 char
loop=1
fi
done
exit 0

Tuesday, May 22, 2018

Make your Shell Script interactive using read command,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
# 23/05/2018
#
echo "Usage Of Read Command in Linux"
rc=0 # return code
while [ $rc -eq 0 ]
do
read -p "Enter value or q to quit: " var
echo "var: $var"
if [ "$var" = "q" ] ; then
rc=1
fi
done
rc=0 # return code
while [ $rc -eq 0 ]
do
read -p "Password: " -s var
echo ""
# carriage return
echo "var: $var"
if [ "$var" = "q" ] ; then
rc=1
fi
done
echo "Press some keys and q to quit."
rc=0
# return code
while [ $rc -eq 0 ]
do
read -n 1 -s var # wait for 1 char, does not output it
echo $var # output it here
if [ "$var" = "q" ] ; then
rc=1
fi
done
exit $rc

Monday, May 21, 2018

Automatic backing up a file if any changes occur in a regular interval,Linux Teacher Sourav,Kolkata 09748184075



 sh ./autobackup.sh a1.txt usb/ 3

 autobackup.sh=script name

a1.txt=file to be monitored

3=interval of checking in seconds

 autobackup.sh

#!/bin/sh
#
# automatically creating a backup of a file if the any changes to the file occurs
# the script that automatically backs up everytime with an incremented number is needed
# Checks that /back exists
# Copies to specific USB directory# Checks if filename.bak exists on startup, copy if it doesn't
# 22/05/2018
if [ $# -ne 3 ] ; then
echo "Usage: autobackup filename USB-backup-dir delay"
exit 255
fi
# Create back directory if it does not exist
if [ ! -d back ] ; then
mkdir back
fi
FN=$1 # filename to monitor
USBdir=$2 # USB directory to copy to
DELAY=$3 # how often to check
if [ ! -f $FN ] ; then # if no filename abort
echo "File: $FN does not exist."
exit 5
fi
if [ ! -f $FN.bak ] ; then
cp $FN $FN.bak
fi
filechanged=0
while [ 1 ]
do
cmp $FN $FN.bak
rc=$?
if [ $rc -ne 0 ] ; then
cp $FN back
cp $FN $USBdir
cd back
sh ./createnumberedbackup.sh $FN
cd ..
cp $FN $FN.bak
filechanged=1
fi
sleep $DELAY
done
****************************************************
createnumberedbackup.sh


#!/bin/sh
#let me show you this script which is able to back up of files in the same directory and number
#the backup,in devops it is very common the save the project with revision number,this
#script is not that complex though


echo "Create Numbered Backup from files of the current directory given as argument"
if [ $# -eq 0 ] ; then
echo "Usage: sh ./createnumberedbackup.sh filename(s) "
echo " Will make a numbered backup of the files(s) given."
echo " Files must be in the current directory."
exit 255
fi
rc=0 # return code, default is no error
for fn in $*  # for each filename given as arguments

do
if [ ! -f $fn ] ; then # if not found
echo "File $fn not found."
rc=1 # one or more files were not found
else
cnt=1 # file counter
loop1=0 # loop flag
while [ $loop1 -eq 0 ]
do
tmp=bak-$cnt.$fn
if [ ! -f $tmp ] ; then
cp $fn $tmp
echo "File "$tmp" created."
loop1=1
# end the inner loop
else
let cnt++ # try the next one
fi
done
fi
done
exit $rc # exit with return code


#every time you run this script with arguments if it finds backup file created by itself

#it will create the next backup with incrementing numbers so the backup can

#more organized


Using TPUT command to change cursor position in a Shell Script,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
# 21/05/2018
# script4
# Subroutines Example
export LINES=$LINES
export COLUMNS=$COLUMNS
#echo $LINES
cls()
{
tput clear
return 0
}
home()
{
tput cup 0 0
return 0
}

bold()
{
tput smso
# no newline or else will scroll
}
unbold()
{
tput rmso
}
underline()
{
tput smul
}
normalline()
{
tput rmul
}
end()
{
#x="${COLUMNS}"-1
let x=$COLUMNS-1

let y=$LINES
#echo x is $x
#echo y is $y
tput cup $y $x
echo -n ""
}
# Code starts here
rc=0
# return code
if [ $# -ne 1 ] ; then
echo "Usage: script4 parameter"
echo "Where parameter can be: "
echo " home - put an X at the home position"
echo " cls - clear the terminal screen"
echo " end - put an X at the last screen position"
echo " bold - bold the following output"
echo " underline - underline the following output"
exit 255
fi
parm=$1
# main parameter 1
if [ "$parm" = "home" ] ; then
echo "Calling subroutine home."
home
echo -n "X"
elif [ "$parm" = "cls" ] ; then
cls
elif [ "$parm" = "end" ] ; then
echo "Calling subroutine end."
end
elif [ "$parm" = "bold" ] ; then
echo "Calling subroutine bold."
bold
echo "After calling subroutine bold."
unbold
echo "After calling subroutine unbold."
elif [ "$parm" = "underline" ] ; then
echo "Calling subroutine underline."
underline
echo "After subroutine underline."
normalline
echo "After subroutine normalline."
else
echo "Unknown parameter: $parm"
rc=1
fi
exit $rc

Saturday, May 19, 2018

Create Numbered backup files from the current working directory given as arguments,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
echo "Create Numbered Backup from files of the current directory given as argument"
if [ $# -eq 0 ] ; then
echo "Usage: sh ./createnumberedbackup.sh filename(s) "
echo " Will make a numbered backup of the files(s) given."
echo " Files must be in the current directory."
exit 255
fi
rc=0 # return code, default is no error
for fn in $*  # for each filename given as arguments

do
if [ ! -f $fn ] ; then # if not found
echo "File $fn not found."
rc=1 # one or more files were not found
else
cnt=1 # file counter
loop1=0 # loop flag
while [ $loop1 -eq 0 ]
do
tmp=bak-$cnt.$fn
if [ ! -f $tmp ] ; then
cp $fn $tmp
echo "File "$tmp" created."
loop1=1
# end the inner loop
else
let cnt++ # try the next one
fi
done
fi
done
exit $rc # exit with return code


Usage:


sh ./createnumberedbackup.sh a1.txt a2.txt