Fun With BASH Scripts – Randquote v1.1
Some of the most popular posts on my blog have been my BASH scripting tutorials. I am not sure if anyone is actually typing them in and using them, but they do come to read them. One of the burdens of having successful posts, especially dealing with programing, is you feel compelled to come back and follow up on them.
So while reading about “20 Funny Commands of Linux or Linux is Fun in Terminal” on Techmint ( http://www.tecmint.com/20-funny-commands-of-linux-or-linux-is-fun-in-terminal/ ). I stumbled upon ‘cowsay’ amd had the wild idea that it would make a great upgrade to my Random Quote script!
First, let’s start off with the simple question. What is Cowsay? Cowsay is a pearl script that will take text and output it inside a cow’s cartoon speech bubble drawn with ASCII characters. Like This.
Now I know at this point there is a NIX guru out there jumping up and down, wanting to tell me that there is no need to modify my code at all, and this exercise is a waste of time, I could simply pipe the output of RandQuote to cowsay and be done with it. While on one hand this person would be correct, however there are 2 reasons to press on. The first being that due to the output being centered by the BASH Script, the output looks funny (Extra Spaces), second I want to be able to demonstrate how to write a BASH script that uses command line arguments. So get a fresh cup of coffee, pull up your chair, and stay awhile.
The first thing we need to do is actually download and install the cowsay script, if you are running Linux in a GUI, simply go to the following URL ( http://www.melvilletheatre.com/articles/el7/cowsay-3.03-14.el7.centos.noarch.rpm ) to download the file.
If you are operating your system from the console or from a terminal you will need to use the wget command.
If it is not installed use sudo yum install wget first, then copy the url above, then cd to your Downloads directory. Ie ~/Downloads. If you do not have a downloads directory you can either use /tmp (Though this is not considered best practice), or you can create the directory with the mkdir command. Anyways. Getting back on track, once you are sure your directory is set to the place you want to download cowsay to.
Copy the url above and enter sudo wget <PASTE>, you should end up with
$sudo wget http://www.melvilletheatre.com/articles/el7/cowsay-3.03-14.el7.centos.noarch.rpm .
Once cowsay has been downloaded, we next need to install it using rpm. As we are still in the directory where cowsay was downloaded, simply perform an ls, and select and copy the filename, cowsay-3.03-14.el7.centos.noarch.rpm . Now issue the command
sudo rpm rpm –ivh cowsay-3.03-14.el7.centos.noarch.rpm . That’s it! You now have cowsay installed. Now comes the hard part. Now we have to modify randquote to incorporate cowsay, hence making it v1.1.
Lets test cowsay to make sure it is installed and working properly, at your shell prompt $ enter cowsay “The Quick Brown Fox Jumped Over The Lazy Dog.” If everything installed correctly, you should be presented with the screen below.
Now let’s go ahead and actually add the necessary lines to randqoute.sh, if you actually have the script installed and running on your system, let’s put it someplace where we can work on it, and not break the actual running copy. The documentation called for the script to run from /opt/randquote, and we will place a copy in a folder called dev under your home folder. If dev does not exist lets create it. At a shell prompt type $ mkdir ~/dev <ENTER>
If you perform a ls of ~/ you should see dev listed.
Lets perform the actual copy. So again at your shell prompt, type.$ sudo cp /opt/randquote/randquote.sh ~/dev/randquote_v1.1.sh <ENTER>
This will copy the file with the new filename randquote1.1.sh so we can easily differentiate between the two versions. We will also most likely need to change owners so we can edit the file without having to use SUDO all the time.
$ chown <USERNAME> : <GROUP> ~/dev/randquote_v1.1.sh <ENTER>
Let us now go into NANO, and edit the script.
$ nano ~/dev/randquote_v1.1.sh
Add the following under the copyright message, <CR> Denotes Carriage Return.
#
# RandQuote v1.1 (C)22NOV2015 by Brent Hendricks
# Added the ability to display quotes through cowsay.
# cowsay is writen by Tomy Monroe (tomy@nog.net)
<CR>
COWSAY=0
<CR>
## Get Options
<CR>
while getopts “c” OPTION
do
case $OPTION in
c)
COWSAY=1
;;
esac
done
<CR>
Now cursor down to ## Read Quote from Quote file
After sed -n “${RECORD},+5p” $QUOTATIONS > $TEMP_QUOTATION
Highlight and cut sed -e :a -e “s/^.\{1,${TERMWIDE}\}$/ &/;ta” -e ‘s/\( *\)\1/\1/’ $TEMP_$
Then add the following.
<CR>
if [ $COWSAY -eq 0 ]
then
Paste the line you cut from above. ‘sed -e :a -e “s/^.\{1,${TERMWIDE}\}$/ &/;ta” -e ‘s/\( *\)\1/\1/’ $TEMP_$’
else
cat “${TEMP_QUOTATION}” | cowsay
fi
The rest of the file remains untouched. The entire file listing should look like this.
1 #! /bin/bash
2
3 # BASH script to display RANDOM QUOTE and center text depending on terminal width.
4 # BASH script and quotations taken from CNET Amiga 2 AREXX PFILE
5 # AMIGA Version Copyright 1992 Jim Selleck and Beverly James Products
6 #
7 # BASH Script (C)24MAY2015 by Brent Hendricks
8 # Script and accompaning BLOG article (C) 2015 Brent Hendricks
9 # Script and quotes are public domain, the accompaning BLOG may only be
used with permission
10 # Contact brent.hendricks@catracing.org if you wish to republish the article.
11 # Please visit Brent’s World @ www.catracing.org\hendrb
12 #
13 # RandQuote v1.1 (C)22NOV2015 by Brent Hendricks
14 # Added the ability to display quotes through cowsay.
15 # cowsay is writen by Tomy Monroe (tomy@nog.net)
16
17 COWSAY=0
18
19 ## Get Options
20
21 while getopts “c” OPTION
22 do
23 case $OPTION in
24 c)
25 COWSAY=1
26 ;;
27 esac
28 done
29
30 ## Get Terminal Width
31 TERMWIDE=”$(tput cols)”
32 ((TERMWIDE = TERMWIDE -3))
33
34 ## Set file path
35 QUOTATIONS=/opt/randquote/quotations
36 TEMP_QUOTATION=/tmp/temp_quotation
37
38 ## Get number of quotations
39 QUOTES=”$(cat $QUOTATIONS | wc -l)”
40 ((QUOTES = QUOTES / 5))
41
42 ## Pick a record
43
44 RECORD=$(($RANDOM % $QUOTES * 5))
45
46 ## Read Quote from Quote file
47 sed -n “${RECORD},+5p” $QUOTATIONS > $TEMP_QUOTATION
48
49 if [ $COWSAY -eq 0 ]
50 then
51 sed -e :a -e “s/^.\{1,${TERMWIDE}\}$/ &/;ta” -e ‘s/\( *\)\1/\1/’
$TEMP_QUOTATION
52 else
53 cat “${TEMP_QUOTATION}” | cowsay
54 fi
55
56 ## Remove TEMP_QUOTATION scratch file
57 # rm -f “${TEMP_QUOTATION}”
58 exit 0
We should now be able to test version 1.1. make sure you are in the directory where v1.1 resides and at a shell prompt enter.
$ ./randquote_v1.1.sh –c
If all worked out right in the end, you should see.
Let’s also do a quick test to determine if you can also generate the random quote without the output being piped through cowsay.
At the shell prompt execute randquote without the -c option, like this.
$ ./randquote_v1.1.sh
You should then only see the normal randquote output.
Once everything is tested me can replace randquote with v1.1
At the shell prompt, let us first rename the original randquote so we retain a backup copy. To do this we use the move (mv) command.
$ sudo mv /opt/randquote/randquote /opt/randquote/randquote_v1.0 <ENTER>
Now we move randquote_v1.1 from our dev directory, renaming it randquote. (Unless you want to keep the _v1.1 filename and change your login scripts.
$ sudo mv ~/dev/randquote_v1.1 /opt/randquote/randquote
We are now finished, if you have entered randquote for the first time, you will also need to download the quotations file attachment. https://www.catracing.org/hendrb/wp-content/uploads/2015/06/quotations.txt
I would like to thank the following people whose blog’s helped immensley with writing version 1.1
First I would like to thank Tomy Monroe who without cowsay, version 1.1 would not be possible.
Second I would like to thank Avishek Kumar for writing the blog 20 Funny Commands of Linux or Linux is Fun in Terminal, http://www.tecmint.com/20-funny-commands-of-linux-or-linux-is-fun-in-terminal/ . Who gave me the original idea.
Last but not least, for showing me the light regarding the getopts statement, this wonderful blog tutorial by rsalveti, Bash: Parsing arguments with ‘getopts’ https://rsalveti.wordpress.com/2007/04/03/bash-parsing-arguments-with-getopts/
Comments
Fun With BASH Scripts – Randquote v1.1 — No Comments
HTML tags allowed in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>