Archive for October, 2008
How to clean multiple sub-directories in Makefile
Oct 23rd
The following example assumes that all sub-directories are listed in $(SUBDIRS) variable. All the entries in $(SUBDIRS) can be iterated using a “for” loop as shown below and “make clean” can be run in each directory.
SUBDIRS = dir1 dir2 dir3 clean: for i in $(SUBDIRS); do $(MAKE) -C $$i clean; done
Note that if you doing are copy-n-paste of the above snippet, the “for” statement should be indented with a TAB. Otherwise it will result in make error.
In the above example “clean” rule can be replaced with any other rule.
The -C option tells GNU make to change to a specific directory before reading the More >
Arithmetic operation using double parentheses construct in Bash
Oct 18th
The following script illustrates how double parentheses can be used to do arithmetic operations.
#!/bin/sh (( a = 23 )) echo "a (assign value) = $a" (( ++a )) echo "a (after ++a) = $a" (( --a )) echo "a (after --a) = $a" (( a++ )) echo "a (after a++) = $a" (( a-- )) echo "a (after a--) = $a" echo (( b = a>30?12:20 )) echo "If a > 30, then b = 12, else b = 20." echo "b = $b " echo
The following is output of the above script.
[neo@techpulp ~]# sh dpara.sh a (assign value) = More >
How to determine if a string contains a validate IP address in C
Oct 18th
Any valid IP address consists of four numbers separated by three period (.) characters and each number ranges from 0-255 inclusive. An example IP address is “125.231.167.234″. The string function sscanf can be used to extract the four numbers and then validate each number for the range 0-255. Then use strcmp to further validate with reconstructed ip string.
The following function returns 0 if the IP address is invalid and 1 if valid.
int is_valid_ip(const char *ip_str)
{
unsigned int n1,n2,n3,n4;
if(sscanf(ip_str,"%u.%u.%u.%u", &n1, &n2, &n3, &n4) != 4) return 0;
if((n1 != 0) && (n1 <= 255) && (n2 <= 255) && (n3 More > How to generate random/temporary file name using mcookie command
Oct 17th
The mcookie shell command is used to generate magic cookie for xauth. It throws a md5sum like output and the basic usage is as shown below.
[neo@techpulp ~]# mcookie 5f8dbb14e7eb4db75428017585115feb [neo@techpulp ~]#
Now let us use the output of mcookie command and form a random file name for temporary usage. In the following script, we use first characters of the random string printed by mcookie to form a random file name. The randfile() function takes prefix and suffix of the temporary file name and returns a temporary file name after checking for its existence.
#!/bin/bash
function randfile
{
while [ 1 ];
do
MCOUT=`mcookie`
TNAME="$1""${MCOUT:0:5}""$2"
if [ ! More > How to write infinite loop in Bash using for or while statement
Oct 17th
The while-do-done statement can be used to program an infinite loop in bash. The following sample shows how it can be done.
#!/bin/bash while [ 1 ]; do echo I am in infinite loop. Press Ctrl+C to stop me done
The break statement can be used to exit the while loop. The following sample code breaks away from while loop after iterating 10 times.
#!/bin/bash
i=1
while [ 1 ];
do
i=`expr $i + 1`
echo loop $i
if [ "$i" == "10" ]; then
echo Exiting while loop after iterating $i times
break
fi
done
echo end of shell script
The following will be the output More >
How to generate temporary file name in bash using RANDOM environment variable
Oct 17th
Whenever the $RANDOM environment variable is accessed, it gives a random number. This can be used to generate a random file name. In the following example randfile function takes prefix and suffix of the temporary file name and returns a temporary file name after checking for its existence.
#!/bin/bash
function randfile
{
while [ 1 ];
do
TNAME="$1""$RANDOM""$2"
if [ ! -f $TNAME ]; then
echo $TNAME
exit 0
fi
done
}
NAME=`randfile /tmp/test- .tmp`
echo $NAME
How to scan all reserved ports on a host using nmap
Oct 9th
nmap is a very useful network exploration tool and security scanner. It can be used to scan all open ports on a host. The following example shows how it can be used. However port scanning is just one of the many features of nmap.
[neo@techpulp ~]# nmap 127.0.0.1 Starting Nmap 4.20 ( http://insecure.org ) at 2008-10-09 12:28 IST Interesting ports on localhost.localdomain (127.0.0.1): Not shown: 1692 closed ports PORT STATE SERVICE 22/tcp open ssh 111/tcp open rpcbind 631/tcp open ipp 904/tcp open unknown 8000/tcp open http-alt Nmap finished: 1 IP address (1 host up) scanned in 0.292 seconds [neo@techpulp ~]#
More >
View page source with a link or a button using Java Script
Oct 9th
window.location can be set to a special location using view-source: followed by window.location.href to make browser to display source code of the current page. This can be attached to a standard link or a click of a button as shown below.
The following shows how to fire view-source window using a standard link: Place this code between <head> and </head> tags of the page.
<script language=JavaScript>
function viewSource() {
window.location = "view-source:" + window.location.href;
}
</script>
Place this code where you would like your link to appear.
<a href="javascript:viewSource()">View Source</a>
Alternately if button needs to fire the view-source window of the browser, add the following piece of code More >
How to prompt for e-mail on click of a button using Java Script
Oct 9th
In general a standard link can be created to prompt for an e-mail as shown below and it doesn’t involve Java Script.
<a href="mailto:neo@techpulp.com">Mail to Neo</a>
However same functionality can be attached to a button using onClick event of the button as show below. But this requires Java Script support.
<form>
<input type="button" value="Email Neo"
onClick="location.href='mailto:neo@techpulp.com'">
</form>


Recent Comments