Skip to content

Instantly share code, notes, and snippets.

@rathankalluri
Created November 4, 2016 09:01
Show Gist options
  • Save rathankalluri/70c42837e64ead26eca86cca7267f44b to your computer and use it in GitHub Desktop.
Save rathankalluri/70c42837e64ead26eca86cca7267f44b to your computer and use it in GitHub Desktop.
QR Code creator in Shell Programming
#! /bin/bash
# This is my very first program in IT industry :D
#This programme is for creating qr codes for the data in the database and storing those codes back into the database.
#Functions used are in the beggining
#
#====================START OF dirfun()..(creates directory)================
#
dirfun()
{ cd /home/rathankalluri/Rathan/
if [ -d qrcodes ]
then
echo "Directory Exists"
else
mkdir qrcodes
chmod 777 qrcodes
echo "Dir created "
fi
}
#
#====================END OF dirfun()==================
#
#=================START OF name_qr_creator()..creates qr codes and its file names==========
#
name_qr_creator()
{
mysql <<EOF > cat >tempfil
use mysql;
select eno,ename from employee;
EOF
no_ln=$(wc -l tempfil|awk '{ print $1 }')
#"echo The number of lines are $no_ln" This is for testing purpose
count=1
while [ "$count" -le "$no_ln" ] && read line
do
if [ $count = 1 ]
then
#"echo count is $count" This is for testing purpose
count=2
else
#"echo The line is :: $line " This for testing purpose
rawdat=`sed -n "$count"p tempfil`
fnm=`echo $rawdat| sed -e 's/ /_/g'`
#"echo The fil name is $fnm" This is for testing purpose
touch "$fnm".png
chmod 777 $fnm.png
#"echo "The file is @ `pwd`"" For Testing
$(qrencode "$line" -o "$fnm".png)
stat=yes
paths=(`pwd`"/$fnm.png")
#"echo $paths" For Testing
lp=1
for word in $rawdat
do
if [ $lp = 1 ]
then
e_no=$word
lp=2
fi
done
#"echo $e_no" For Testing
mysql -D mysql <<EOF
insert into qrdata values('$stat','$paths','$e_no');
EOF
count=`expr $count + 1`
fi
done </home/rathankalluri/Documents/Details
rm -rf tempfil
rm -rf cat
}
#
#=================End of name creator function =======================
#
#=================== M A I N P R O G R A M RUNS FROM HERE=================
#
#connecting to the sql database.If we have any privileges, use the commented code also.
echo -e "Establishing Connections \n"
/etc/init.d/mysqld start
#echo "Enter the username :"
#read usr_name
#echo "Password :"
#read -s passd
#echo -n "Enter the database name : "
#read db_name
#mysql -h localhost -u $usr_name -p $passd; This code is used if we have any privileges set.
#code for retriving from the database
mysql <<M > cat >Details
use mysql ;
select * from employee ;
M
#"cat Details" #For Testing the output from Database
echo -e "Initializing the Creation process \n"
echo -e "Reading row by row details \n" #line to be removed later
echo "Trying to create a directory"
dirfun
cd /home/rathankalluri/Rathan/qrcodes
echo -e "Qr code generator started \n"
name_qr_creator #Calling QR code creator
echo "Done"
echo -e "removing temporary files \n"
rm -rf /home/rathankalluri/Documents/cat
rm -rf /home/rathankalluri/Documents/Details
echo -e "Killing Connections... please wait \n"
/etc/init.d/mysqld stop
echo -e "Program Termination Complete !!!!"
exit 0
#
#======================== END OF MAIN PROGRAM ===============================
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment