<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linux, Programming, Telephony, Asterisk, Apache, Tips &#38; Tricks &#187; Shell Script</title>
	<atom:link href="http://shahidz.com/category/shell-script/feed/" rel="self" type="application/rss+xml" />
	<link>http://shahidz.com</link>
	<description>Passionate About Technology</description>
	<lastBuildDate>Sun, 10 Jan 2010 17:38:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Linux Date Command Option</title>
		<link>http://shahidz.com/linux-date-command-option/</link>
		<comments>http://shahidz.com/linux-date-command-option/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 05:59:13 +0000</pubDate>
		<dc:creator>Shahid</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Linux Commands]]></category>
		<category><![CDATA[Shell Script]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[option]]></category>

		<guid isPermaLink="false">http://shahidz.com/?p=77</guid>
		<description><![CDATA[Date command options.
Date command to get the current date.
$ date=$(date +%Y-%m-%d)
$ echo $date
Date command to get yesterday&#8217;s date.
$ date=$(date --date=yesterday +%Y-%m-%d)
$ echo $date
Date command to get tomorrow&#8217;s date.
$ date=$(date --date=tomorrow +%Y-%m-%d)
$ echo $date
]]></description>
			<content:encoded><![CDATA[<p><strong>Date command options.</strong></p>
<p>Date command to get the current date.</p>
<pre>$ date=$(date +%Y-%m-%d)
$ echo $date</pre>
<p>Date command to get yesterday&#8217;s date.</p>
<pre>$ date=$(date --date=yesterday +%Y-%m-%d)
$ echo $date</pre>
<p>Date command to get tomorrow&#8217;s date.</p>
<pre>$ date=$(date --date=tomorrow +%Y-%m-%d)
$ echo $date</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fshahidz.com%2Flinux-date-command-option%2F&amp;linkname=Linux%20Date%20Command%20Option"><img src="http://shahidz.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://shahidz.com/linux-date-command-option/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shell Script To Monit Swap Usage</title>
		<link>http://shahidz.com/shell-script-to-monit-swap-usage/</link>
		<comments>http://shahidz.com/shell-script-to-monit-swap-usage/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 17:44:59 +0000</pubDate>
		<dc:creator>Shahid</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Shell Script]]></category>
		<category><![CDATA[cron. swap]]></category>
		<category><![CDATA[monit]]></category>
		<category><![CDATA[swap-usage]]></category>
		<category><![CDATA[system admin]]></category>

		<guid isPermaLink="false">http://shahidz.com/shell-script-to-monit-swap-usage/</guid>
		<description><![CDATA[Some time our system started to use more swap area even 100% of swap area and the system crashes. So for a System Admin it would be very helpful when they get informed if the system started to use more swap usage so that system admin can manage the server before crashing. So I wrote [...]]]></description>
			<content:encoded><![CDATA[<p>Some time our system started to use more swap area even 100% of swap area and the system crashes. So for a System Admin it would be very helpful when they get informed if the system started to use more swap usage so that system admin can manage the server before crashing. So I wrote a shell script which will Alert by send mail if the system started to use more that 70% of swap area.</p>
<pre>$ vim swap-usage.sh</pre>
<pre>#!/bin/sh
free -m | grep Swap | while read output;
do
  swap=$(echo $output | awk '{print $2}' )
  used=$(echo $output | awk '{ print $3 }' )
  freed=$(echo $output | awk '{ print $4 }' )
  echo "Swap : $swap"
  echo "Used : $used"
  echo "Free : $freed"
  usep=`expr $used \* 100 / $swap`
  echo $usep
  if [ $usep -ge 70 ]; then
    echo "Swap Usage Alert Total Swap: \"$swap\" Used: \"$used ($usep%)\" Free:
\"$freed\" on $(hostname) as on $(date)" |
     mail -s "Alert: Swap Usage space $usep%" mail1@example.com,mail2@example.com
  fi
done</pre>
<p>This script will send mails to the specified mails, if the system started to use more than 70% of  swap area. For continuously check the swap usage we need to set a cronjob for executing this script. So that the system should monit the swap usage by executing this script.</p>
<p>Edit the corntab file.</p>
<pre>$ sudo crontab -e</pre>
<p>Set the cronjob to to execute the script in every 15 Mints.</p>
<pre>*/15 * * * * ~/scripts/swap-usage.sh</pre>
<p>Restart the Cron</p>
<pre>$ sudo /etc/init.d/cron restart</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fshahidz.com%2Fshell-script-to-monit-swap-usage%2F&amp;linkname=Shell%20Script%20To%20Monit%20Swap%20Usage"><img src="http://shahidz.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://shahidz.com/shell-script-to-monit-swap-usage/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Shell script to notify Hard disk space utilization</title>
		<link>http://shahidz.com/shell-script-to-notify-hard-disk-space-utilization/</link>
		<comments>http://shahidz.com/shell-script-to-notify-hard-disk-space-utilization/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 16:15:33 +0000</pubDate>
		<dc:creator>Shahid</dc:creator>
				<category><![CDATA[Shell Script]]></category>
		<category><![CDATA[hardisk space]]></category>
		<category><![CDATA[monit]]></category>
		<category><![CDATA[notification]]></category>

		<guid isPermaLink="false">http://shahidz.com/shell-script-to-notify-hard-disk-space-utilization/</guid>
		<description><![CDATA[The below script will check the hard disk space usage, using the command du and send an email alert if any of the partition is using more than 85% of space.
vim disk-usage.sh
#!/bin/sh
df -H &#124; grep -vE '^Filesystem&#124;tmpfs&#124;cdrom' &#124; awk '{ print $5 " " $1 }' &#124; while read output;
do
  echo $output
  usep=$(echo [...]]]></description>
			<content:encoded><![CDATA[<p>The below script will check the hard disk space usage, using the command<em> du </em>and send an email alert if any of the partition is using more than 85% of space.</p>
<p>vim disk-usage.sh</p>
<pre>#!/bin/sh
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge 85 ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
     mail -s "Alert: Almost out of disk space $usep%" example@example.com
  fi
done</pre>
<p>Set a cronjob to run this script.</p>
<pre>*/30 * * * * /bin/sh /path-to-file/disk-usage.sh</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fshahidz.com%2Fshell-script-to-notify-hard-disk-space-utilization%2F&amp;linkname=Shell%20script%20to%20notify%20Hard%20disk%20space%20utilization"><img src="http://shahidz.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://shahidz.com/shell-script-to-notify-hard-disk-space-utilization/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Configuring Iptables on system startup</title>
		<link>http://shahidz.com/configuring-iptables-on-system-startup/</link>
		<comments>http://shahidz.com/configuring-iptables-on-system-startup/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 08:59:41 +0000</pubDate>
		<dc:creator>Shahid</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Shell Script]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[Iptables]]></category>
		<category><![CDATA[start]]></category>
		<category><![CDATA[status]]></category>
		<category><![CDATA[stop]]></category>

		<guid isPermaLink="false">http://shahidz.com/configuring-iptables-on-system-startup/</guid>
		<description><![CDATA[In Ubuntu we don&#8217;t have a mechanism to start or stop iptables or we don&#8217;t have a mechanism to restore iptables after restarting the system. Now we will see how to create a script for start and stop iptables also to make the script to start on system startup.
1. Create a Firewall script
 vim /etc/set_iptables.bash
echo 1 [...]]]></description>
			<content:encoded><![CDATA[<p>In Ubuntu we don&#8217;t have a mechanism to start or stop <a href="http://shahidz.com/tag/iptables/" title="Iptables" target="_blank">iptables</a> or we don&#8217;t have a mechanism to restore <a href="http://shahidz.com/tag/iptables/" target="_blank">iptables</a> after restarting the system. Now we will see how to create a script for start and stop iptables also to make the script to start on system startup.</p>
<p>1. Create a Firewall script</p>
<pre> vim /etc/set_iptables.bash</pre>
<pre>echo 1 &gt; /proc/sys/net/ipv4/ip_forward

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A PREROUTING -p tcp -i eth0 -d 192.168.0.10 --dport 555 -j DNAT --to 192.168.0.12:22
iptables -A FORWARD -p tcp -i eth0 -d 192.168.0.12 --dport 22 -j ACCEPT</pre>
<p>2. Change the file permission mod</p>
<pre>sudo chmod o+x /etc/set_iptables.bash</pre>
<p>3. Create a start.stop script</p>
<pre>vim /etc/init.d/iptables</pre>
<pre>#!/bin/bash

RETVAL=0

# To start the firewall
start() {
  echo -n "Iptables rules creation: "
  /etc/set_iptables.bash
  RETVAL=0
}

# To stop the firewall
stop() {
  echo -n "Removing all iptables rules: "
  /sbin/iptables -F
  RETVAL=0
}

case $1 in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  status)
    /sbin/iptables -L
    /sbin/iptables -t nat -L
    RETVAL=0
    ;;
  *)
    echo "Usage: iptables {start|stop|restart|status}"
    RETVAL=1
esac
exit</pre>
<p>4. Change the file permission mod</p>
<pre>sudo chmod o+x /etc/init.d/iptables</pre>
<p>5. The final step is to make your script running on each boot of your computer:</p>
<pre>sudo update-rc.d iptables defaults</pre>
<p>Now you can use these commands to start/stop/restart/status your iptables.</p>
<pre>sudo /etc/init.d/iptables start
sudo /etc/init.d/iptables stop
sudo /etc/init.d/iptables restart
sudo /etc/init.d/iptables status</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fshahidz.com%2Fconfiguring-iptables-on-system-startup%2F&amp;linkname=Configuring%20Iptables%20on%20system%20startup"><img src="http://shahidz.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://shahidz.com/configuring-iptables-on-system-startup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shell Script to delete unwanted files and directories</title>
		<link>http://shahidz.com/shell-script-to-delete-unwanted-files-and-directories/</link>
		<comments>http://shahidz.com/shell-script-to-delete-unwanted-files-and-directories/#comments</comments>
		<pubDate>Sat, 08 Mar 2008 09:41:12 +0000</pubDate>
		<dc:creator>Shahid</dc:creator>
				<category><![CDATA[Shell Script]]></category>
		<category><![CDATA[cleaning directories]]></category>

		<guid isPermaLink="false">http://shahidz.com/?p=13</guid>
		<description><![CDATA[#!/bin/bash
cd /home/shahid/                # Change the dectory to the specified location
monit stop all                      # Stop all the process
rm -rf log/* [...]]]></description>
			<content:encoded><![CDATA[<pre class="entry">#!/bin/bash
cd /home/shahid/                # Change the dectory to the specified location
monit stop all                      # Stop all the process
rm -rf log/*                           # Deletes all the files inside the directory log

rm -rf dump/processed/*     # Deletes all the files inside the directory dump/processed/

rm -rf dump/s3/*                  # Deletes all the files inside the directory dump/s3/
#rm -rf dump/02-*               # Deleting the directory starting with 02-
#rm -rf dump/03-*               # Deleting the directory starting with 03-
#rm -rf dump/12-*               # Deleting the directory starting with 12-*

months=( 01 02 03 04 05 06 07 08 09 10 11 12 ) # Declare an array named months.

seperator=”-”                        # Date seperator 13-02-2008
star=”*”                                 # Specify * to

for ((  i = 0 ;  i &lt; 31;  i++  ))      # For loop to check directories starting with the name 01-, 02-, .., 12-.
do
var1=${months[$i]}$seperator$star   # Gets all the files &amp; directories starting with 01-, 02-,…,12-
#    echo $var1
for dir in $var1                    # Gets each file and directories seperatly
do
if [ -d $dir ]                     # Checks for directory if yes
then                               #then
rm -rf $dir                  # Only deletes the directories
fi                                    # End of If
done                                  # End of second for loop
done                                     # End of First for loop

monit start all                        # Starting all the process</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fshahidz.com%2Fshell-script-to-delete-unwanted-files-and-directories%2F&amp;linkname=Shell%20Script%20to%20delete%20unwanted%20files%20and%20directories"><img src="http://shahidz.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://shahidz.com/shell-script-to-delete-unwanted-files-and-directories/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache URL Rewriting</title>
		<link>http://shahidz.com/apache-url-rewriting/</link>
		<comments>http://shahidz.com/apache-url-rewriting/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 14:12:20 +0000</pubDate>
		<dc:creator>Shahid</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Micellanious]]></category>
		<category><![CDATA[Shell Script]]></category>

		<guid isPermaLink="false">http://shahidz.com/?p=11</guid>
		<description><![CDATA[Introduction 
One of the more powerful tricks of the .htaccess is the ability to rewrite URLs. This enables us to do some mighty manipulations on our links; useful stuff like transforming very long URL&#8217;s into short, cute URLs,  redirecting URL to some other URL, transforming dynamic ?generated=page&#38;URL&#8217;s into /friendly/flat/links, redirect missing pages, preventing hot-linking, performing automatic [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Introduction </strong></p>
<p>One of the more powerful tricks of the .htaccess is the ability to rewrite URLs. This enables us to do some mighty manipulations on our links; useful stuff like transforming very long URL&#8217;s into short, cute URLs,  redirecting URL to some other URL, transforming dynamic <span class="urltxt">?generated=page&amp;URL&#8217;s</span> into /friendly/flat/links, redirect missing pages, preventing hot-linking, performing automatic language translation, and much, much more.</p>
<p>When you use rewrite rule you have to enable rewrite module in the apache.conf file and you have to restart apache again.</p>
<p>In Ubuntu you can use a2enmod to enable rewrite mod</p>
<p>#sudo a2enmod rewrite</p>
<p>#sudo /etc/init.d/apache2 restart</p>
<p><strong>begin rewriting&#8230; </strong></p>
<p>First you have to add following lines to the Apache Virtualhost.</p>
<p>&lt;Directory / Your Document Root&gt;<br />
Options FollowSymLinks<br />
AllowOverride all<br />
&lt;/Directory&gt;</p>
<p><strong>Simple rewriting</strong></p>
<p>Simply put, Apache scans all incoming URL requests, checks for matches in our .htaccess file and rewrites those matching URLs to whatever we specify. something like this..</p>
<p class="codetop"><strong>Example 1 :</strong></p>
<p class="codetop"> 			all requests to whatever.htm will be sent to whatever.php:</p>
<p># .htaccess file</p>
<p>Options +FollowSymlinks<br />
RewriteEngine on<br />
RewriteRule ^(.*)\.htm$ $1.php [nc]</p>
<p><strong>Example 2 :</strong></p>
<p>all requests to example.com will be sent to xyz.com</p>
<p># .htaccess file</p>
<p>Options +FollowSymLinks<br />
RewriteEngine On<br />
RewriteBase /</p>
<p># require non-empty HOST header<br />
RewriteCond %{HTTP_HOST} !^$</p>
<p># require case-insensitive HOST to be www.example.net<br />
RewriteCond %{HTTP_HOST} !^www\.example\.net$ [NC]</p>
<p># 301 redirect everything to correct www.example.net<br />
RewriteRule ^(.*)$ http://www.xyz.com/$1 [R=301,L]</p>
<p># or you can use this rewriterule<br />
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fshahidz.com%2Fapache-url-rewriting%2F&amp;linkname=Apache%20URL%20Rewriting"><img src="http://shahidz.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://shahidz.com/apache-url-rewriting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
