November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

korn shell (ksh) – for loop

Reading from a file

for line in $(cat /etc/passwd) do echo $line done

Iterate for a range

for number in {1..10} do echo “Current number is $number” done

Loop for values given

for planet in earth mars saturn jupiter do echo “The planet name is $planet” done

Loop for variable containing multiple values

planets=”earth mars […]

If statement and comparison operators

If statement without any brackets: These are used to check the outcome of a command. It return the exist status of command executed after if.

if grep -i oolala lyrics.txt then echo “lyrics.txt file contains oolala” else echo “lyrics.txt file doesn’t contain oolala” fi

if statement with []: These are used to check the […]

BASH – Default variables for a bash shell

$? => Return code of previous command $0 => Name of the script itself $1-9 => Argument number 1 to 9 given to script ${10} => Argument number 10 given to script $# => Total number of arguments given to script $@ => All arguments given to script but as an individual string values “$*” […]

if condition to check file or directory exists

-e: Returns true value, if file exists

-f: Return true value, if file exists and regular file

-r: Return true value, if file exists and is readable

-w: Return true value, if file exists and is writable

-x: Return true value, if file exists and is executable

-d: Return true value, if exists and is […]

check process is running or not shell

!/bin/bash CHECK=$0 SERVICE=$1 DATE=`date` OUTPUT=$(ps aux | grep -v grep | grep -v $CHECK |grep $1) echo $OUTPUT if [ “${#OUTPUT}” -gt 0 ] ; then echo “$DATE: $SERVICE service running, everything is fine” else echo “$DATE: $SERVICE is not running” fi

#!/bin/bash ps axho comm| grep $1 > /dev/null result=$? echo “exit code: ${result}” […]

Redhat init Script for apache

#!/bin/sh # # Startup script for the Apache Web Server # # chkconfig: – 85 15 # description: Apache is a World Wide Web server. It is used to serve \ # HTML files and CGI. # processname: httpd # pidfile: /var/run/httpd.pid # config: /etc/httpd/conf/access.conf # config: /etc/httpd/conf/httpd.conf # config: /etc/httpd/conf/srm.conf ulimit -HSn 32768 # […]

zmapachectl

#!/bin/bash # # ***** BEGIN LICENSE BLOCK ***** # Zimbra Collaboration Suite Server # Copyright (C) 2005, 2007, 2008 Zimbra, Inc. # # The contents of this file are subject to the Yahoo! Public License # Version 1.0 (“License”); you may not use this file except in # compliance with the License. You may obtain […]

restart of service

echo is_uid=`/usr/bin/id | /usr/bin/awk -F\= {‘print $2’} | /usr/bin/awk -F\( {‘print $1’}` if [ $is_uid -ne 0 ] ; then echo “You are not logged on as root” exit ; fi # DNS, name server, bind ; case “$1” in ‘dns’|’bind’|’named’) if [ -f “/usr/local/etc/named.pid” ] ; then named_pid=`cat /usr/local/etc/named.pid` kill -HUP $named_pid echo […]

Apache restart

#!/bin/bash # # apache Start the apache HTTP server. # # The variables below are NOT to be changed. They are there to make the # script more readable. NAME=apache DAEMON=/usr/sbin/$NAME PIDFILE=/var/run/$NAME.pid CONF=/etc/$NAME/httpd.conf APACHECTL=/usr/sbin/${NAME}ctl # note: SSD is required only at startup of the daemon. SSD=`which start-stop-daemon` ENV=”env -i LANG=C PATH=/bin:/usr/bin:/usr/local/bin” SHIB_HOME=/opt/shibboleth-1.3 LD_LIBRARY_PATH=${SHIB_HOME}/libexec:${SHIB_HOME}/lib export LD_LIBRARY_PATH […]

Shell to find directory and execute

#!/bin/bash KNOWN_PATH=”/usr/sbin /sbin /opt/apache2/bin /usr/local/sbin” HTTPD_DIR= for path in $KNOWN_PATH; do echo “trying $path/httpd…” if `ls $path/httpd > /dev/null 2>&1`; then HTTPD_DIR=$path break fi done if [ -z $HTTPD_DIR ]; then echo “httpd path not found, please enter the path to directory where httpd is” echo -n “httpd path : ” read HTTPD_DIR fi HTTPD=”$HTTPD_DIR/httpd” […]