Category: Shell Scripts

Sample and ready to use shell scripts

Bash – Convert a comma separated list to any Array

Bash – Convert a comma separated list to any Array

#!/bin/bash IFS="," sampleData="59,100,23,98,50" arr=($sampleData) for i in "${!arr[@]}";do echo Item $i:"${arr[$i]}" done unset IFS echo "Completed"

Nagios Bash script to monitor Domain Expiration

Nagios Bash script to monitor Domain Expiration

This little script here, lets you use nagios to monitor domain name expiration and warn you ahead of time. You can also use it to just run some cron jobs, our extract the portion...

Bash script to check for valid IP

Bash script to check for valid IP

Script to check for valid ip #!/bin/bash function validateIP() { local ip=$1 local stat=1 if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then OIFS=$IFS IFS=’.’ ip=($ip) IFS=$OIFS [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \...

Resize Image with Bash Script

Resize Image with Bash Script

You can use bash script to resize images. Prerequisite : yum install ImageMagick Files with .JPG extension , file will be overwritten with new image #!/bin/bash for i in *.JPG; do convert $i -resize...

Shell Script to Create and Grant Privilege to a new user

Shell Script to Create and Grant Privilege to a new user

#!/bin/bash #Script to Create a user and assign privilege to a MySQL Database read  -p  “Please Enter Database Name:” dbname mysql -Bse “USE $dbname” 2> /dev/null if [ $? -eq 0 ]; then        ...

Shell script to ssh to a remote host without specifying the port

Shell script to ssh to a remote host without specifying the port

#!/bin/bash PORT=”non standard port number” if [ -z “$1” ]; then echo “Usage: $0 hostname” else ssh -p $PORT $1 fi

Shell script to display recipient email from message id – postfix

Shell script to display recipient email from message id – postfix

#!/bin/bash if [ -z “$1” ];then echo “Usage:$0 ” else abuse=`echo $1| cut -f2 -d.` log_seq=0 if [ “$log_seq” -le 1 ];then log_file=/var/log/maillog else log_file=/var/log/maillog/$log_seq fi cat $log_file | grep $abuse | grep -w...

Bash Script to check if a host is alive

Bash Script to check if a host is alive

Script to check if a host is alive: #!/bin/bash ping -c 2 www.amitnepal.com > /dev/null if [ $? -eq 0 ]; then echo “Alive”; else echo “Dead”; fi

Shell Script to Backup and Rotate Mysql Database

Shell Script to Backup and Rotate Mysql Database

#!/bin/bash #Author : Amit K Nepal #Last Modified : Nov 01 2011 BACKUP_DIR=/db_backups DB_NAME=dbname TSTAMP=`date +%Y%m%d` FILENAME=$BACKUP_DIR/$DB_NAME.gz Notify=1 Rotate=8 logfile=$BACKUP_DIR/backup.log echo “………….Backup Script Running on $TSTAMP…………” >> $logfile let i=$Rotate-1 if [ -f “$FILENAME.$Rotate”...