Linux Shell Scripting
Creating an automatic installer for HTTPD
This tutorial explains how to create a simple shell script with default HTTPD compile options which can install HTTPD just in one command PROVIDED you have sudo and the NOPASSWD: ALL in your username's entry in the sudoers file.
So, here's a script which can do the above task. Please note that you need not rewrite all the options given to configure while compiling first time, you can just take a copy of config.nice script which is created by configure present in the build directory. For instance, if your apache directory is /usr/local/apache2 ; then you'll find config.nice in /usr/local/apache2/build.
Actually I must say this is an extension of the config.nice file as I myself created this using config.nice ;)
Note: I have used config,nice from httpd-2.2.9's compilation, may not work for httpd-1.x or httpd-2.0.x
#!/bin/bash # Apache Install Script # Modified version of config.nice # ------- PARAMETERS REQUIRED ------- # 1st Parameter - Extracted Source code directory if [ ! $1 ]; then echo -e "You must pass the first argument as the SOURCE CODE directory\n" exit 1 fi SRCDIR=$1 if [ -d $SRCDIR ] && [ $(pwd) != $SRCDIR ]; then echo -e "Current Working Directory is $(pwd)\n\n" echo -e "Changing Directory to $SRCDIR\n\n" cd $SRCDIR &> /dev/null CD_RETVAL=$? if [ $CD_RETVAL -ne 0 ]; then echo -e "Failed to change directory to $SRCDIR\n\n" exit $CD_RETVAL fi fi # Replacing ./configure from config.nice to $SRCDIR/configure as the configure script is in $SRCDIR # Parameters for configure taken from config.nice # configure by default will install in /usr/local/apache2 "$SRCDIR/configure" \ "-C" \ "--build=i686-pc-linux-gnu" \ "--host=i686-pc-linux-gnu" \ "--target=i686-pc-linux-gnu" \ "--enable-mods-shared=authn-file authn-dbm authn-dbd authn-default authn-anon authn-alias authz-host authz-groupfile authz-user authz-dbm authz-owner authz-default auth-basic auth-digest file-cache cache disk-cache mem-cache dbd ext-filter include filter substitute charset-lite deflate log-config log-forensic logio env mime-magic cern-meta expires headers ident usertrack unique-id setenvif version proxy proxy-connect proxy-ftp proxy-http proxy-ajp proxy-balancer ssl mime dav status autoindex asis info cgid cgi suexec dav-fs dav-lock vhost-alias negotiation dir imagemap actions speling userdir rewrite alias" \ "--enable-modules=so" \ "--with-mpm=worker" \ "--with-included-apr" \ "$@" AUTOCONF_RETVAL=$? if [ $AUTOCONF_RETVAL -ne 0 ]; then echo -e "\n\nError\n\n" exit $AUTOCONF_RETVAL fi make -f Makefile sudo make -f Makefile install
This script will install httpd in /usr/local/apache2
Enjoy! :)
Bugs, if found, shall be posted in comments and the same for any suggested improvements or modifications ;)
Extracting Data from a file using cut and grep
Ever thought of having a database as a single TEXT file ? You could do that, but how do you extract data from it ?
This tutorial deals with extracting data from a SINGLE file very much similar to a RDBMS SELECT with WHERE query.
Let's create a file with the following lines -
A;A1;A2;A3;A4;A5 B;B1;B2;B3;B4;B5 C;C1;C2;C3;C4;C5 D;D1;D2;D3;D4;D5 E;E1;E2;E3;E4;E5 F;F1;F2;F3;F4;F5
As you see above, data has been separated by semicolons (;).
Suppose you want to extract the 4th field from the file for the record which starts with C.
To do the above, use this -
$ grep ^C file | cut -f4 -d\;
Please note that this will give you the value which says C3. This is because the first entry itself is a field.
In the above command, grep ^C means show all lines starting with the letter C.
And the next part cut -f4 -d\; means ONLY show the fourth field and since we have separated our fields by a semicolon, the -d\; option is used. Here you need to put the backslash before the semicolon because semicolon is a SHELL character. You could also use colon (:) in the file and the cut command to avoid this problem.
Please comment if you have questions/suggestions.
Some keyboard shortcuts for the Linux Shell
Here are some Linux command line (BASH) tricks which can save you a lot of time -
To repeat the last command -
Use Up Arrow or type !! (double exclamation mark)
To repeat the last command started with a word -
!word
To repeat a command by referring to its history no. -
!number
To repeat a command enterted some commands ago use -
!-number
Know more.. ? Post comments! I'll update the post. :)
Pingstat!
What is Pingstat ?
Pingstat is small Linux Bash Script which can log the results of PINGING a particular IP in a LOGFILE if specified OR output to STDOUT in a user-friendly format -
[time] PING [IP-ADDR] SUCCESS/FAILURE
LOGFILE by default is $HOME/pingstat.log
The script has a inbuilt default IP variable called $DEFAULTIP which is pinged if no IP is given as the first command line argument..
The script takes another argument which can have values of 1, YES, Yes, Y, TRUE, True and when any of the values are found it will remove $LOGFILE if it exists.
It has two internal variables called $PINGCOUNT and $TIMEOUT (Their names say what they mean).
It has one more variable called $PINGCMD i.e. the name (or path) of the UNIX `ping` command ... in most cases its gonna be `ping`
So here's the script-
Feedback and Suggestions would be appreciated
#!/bin/bash
#PING AND LOG STATUS
#First argument which can be passed is the IP-Address
#If IP Address is not specified on the command line arguments,
#Then $DEFAULTIP will be used as declared below
#Second argument which can be specified is to remove the old log file
#Second argument can have values of - 1, TRUE, Yes, YES, True
#If second argument is not given then result is appended to ..
#existing logfile
#SOME DEFAULT SETTINGS .... CHANGE AS YOU WISH
#The default IP to PING if no IP was given as the first argument
DEFAULTIP='121.246.205.1'
#The PING command: Usually `ping`
PINGCMD='ping'
#Number of packaets to send....
#Keep this small < 5 if you are pinging frequestly using Cron job to avoid
#conflicts with the next ping
PINGCOUNT=2
#Timeout in seconds if no response is recieved.....
#Keep this small < 5 if you are pinging frequently using Cron job to avoid
#conflicts with the next ping
TIMEOUT=5
#The LOG File PATH to save the result -
#Format of the LOG File will be Like this -
#[time] PING $IP SUCCESS/FALIURER
#If LOGFILE is not specified, then output will be STDOUT
LOGFILE=$HOME/pingstat.log
#END CONFIGURATION SECTION
rmf()
{
rm -f $LOGFILE
}
if [ $1 ]; then
IP=$1
else
if [ ! $DEFAULTIP ]; then
echo -e "\nNO IP ADDRESS DEFINED\n";
exit 1;
else
IP=$DEFAULTIP
fi
fi
REMOLDLOG=$2
if [ -f $LOGFILE ]; then
case "$REMOLDLOG" in
1)
rmf
;;
TRUE)
rmf
;;
YES)
rmf
;;
Yes)
rmf
;;
Y)
rmf
;;
True)
rmf
;;
esac
fi
if [ ! $TIMEOUT ]; then
TIMEOUT=5
fi
if [ ! $PINGCOUNT ]; then
PINGCOUNT=2
fi
if [ ! $PINGCMD ]; then
PINGCMD='ping'
fi
if [ ! $LOGFILE ]; then
LOGFILE="/dev/stdout"
fi
$PINGCMD -c$PINGCOUNT -W$TIMEOUT $IP &> /dev/null
if [ $? -eq 0 ]; then
echo -e "[$(date)] - PING $IP SUCCESS \n" >> $LOGFILE
else
echo -e "[$(date)] - PING $IP FAILURE \n" >> $LOGFILE
fi
Create a Shell Script which can break a sentence into words
Have you ever wondered to create program which can split a sentence into words ? Now you can! Using simple Bash Shell Techniques! The following script will split your sentence into words (incl. punctuation marks).
The script is based on a simple concept that the read command accepts character clusters (words) into an array.
When you run it it will ask for a sentence.... suppose you give this -
FOO GOO ROO MOO .
Then it will output "
Your sentence has been split into 5 words -- as follows --
Word 1: FOO
Word 2: GOO
Word 3: ROO
Word 4: MOO
Word 5: .
"
Actually there are only 4 words but since . is like a word for the script, it will show that also..... Suggestions about how to ignore this problem would be appriciated.
PS: I have created this script just for fun!
#!/bin/bash
echo -e "\n";
read -p "Enter a Sentence: " -a sentence;
count=0;
echo -e "\n";
for num in ${sentence[@]}; do
let count=$count+1;
done
echo -e "Your sentence has been split into $count words -- as follows -- \n";
echo -e "\n";
i=0;
for word in ${sentence[@]}; do
let c=$i+1;
echo -e "Word $(echo $c): $word\n";
let i=$i+1;
done
echo -e "\n\n";
HAVE FUN! :P
Recent comments
1 hour 1 min ago
1 day 20 hours ago
2 days 10 hours ago
2 weeks 3 days ago
2 weeks 5 days ago
3 weeks 16 hours ago
3 weeks 5 days ago
3 weeks 6 days ago
4 weeks 1 day ago
4 weeks 1 day ago