The shell sets some environment variables according to the command line arguments specified

The shell sets some environment variables according to the command line arguments specified

























$0 The name the script was invoked with. This may be a basename
without directory component, or a path name. This variable
is not changed with subsequent shift
commands.
$1, $2, $3, … The first, second, third, … command line argument,
respectively. The argument may contain whitespace if the argument
was quoted, i.e. "two words".
$# Number of command line arguments, not counting the
invocation name $0
$@ "$@" is replaced with all command line
arguments, enclosed in quotes, i.e. "one", "two three",
"four". Whitespace within an argument is
preserved.
$* $* is replaced with all command line
arguments. Whitespace is not preserved, i.e.
"one", "two three", "four" would be changed to
"one", "two", "three", "four".

This variable is not used very often, "$@"
is the normal case, because it leaves the arguments
unchanged.

How do I modify or change the prompt in bash?

How do I modify or change the prompt in bash?

Modifying the prompt is easy. Just assign a new value to PS1

\a : an ASCII bell character (07)
\d : the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
\e : an ASCII escape character (033)
\h : the hostname up to the first ‘.’
\H : the hostname
\j : the number of jobs currently managed by the shell
\l : the basename of the shell’s terminal device name
\n : newline
\r : carriage return
\s : the name of the shell, the basename of $0 (the portion following the final slash)
\t : the current time in 24-hour HH:MM:SS format
\T : the current time in 12-hour HH:MM:SS format
\@ : the current time in 12-hour am/pm format
\A : the current time in 24-hour HH:MM format
\u : the username of the current user
\v : the version of bash (e.g., 2.00)
\V : the release of bash, version + patch level (e.g., 2.00.0)
\w : the current working directory, with $HOME abbreviated with a tilde
\W : the basename of the current working directory, with $HOME abbreviated with a tilde
\! : the history number of this command
\# : the command number of this command
\$ : if the effective UID is 0, a #, otherwise a $
nnn : the character corresponding to the octal number nnn
\\ : a backslash
\[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] : end a sequence of non-printing characters

Some examples:

-bash-3.2# PS1="\d \h $ "
Fri Aug 17 dracko1 $

Fri Aug 17 esehost1 $
Fri Aug 17 esehost1 $ PS1="[\d \t \u@\h:\w ] $ "
[Fri Aug 17 10:00:45 root@dracko1:/ ] $

Shell script test syntax

Shell script test syntax

All of the shell script tests that follow should be performed between the bracket characters [ and ], like this:

if [ true ]
then
# do something here
fi

Make sure you leave spaces around the bracket characters.

File-related tests
==================

To perform tests on files use the following comparison operators:

-d file Test if file is a directory
-e file Test if file exists
-f file Test if file is an ordinary file
-r file Test if file is readable
-w file Test if file is writable
-x file Test if file is executable

As an example, assuming you have a file named foo, here’s how you would test to see if that file is readable:

if [ -r foo ]
then
# do something here
fi

String comparison tests
=======================

Here are the operators for performing string comparison tests:

s1 Test if s1 is not the empty string
s1 = s2 Test if s1 equals s2
s1 != s2 Test if s1 is not equal to s2
-n s1 Test if s1 has non-zero size
-z s1 Test if s1 has zero size

Here’s an example of how to see if two strings are equal:

if [ $foo = $bar ]
then
# do something
fi

This script echoes TRUE:

s1=

if [ -n $s1 ]
then
echo "TRUE"
else
echo "FALSE"
fi

This script echoes FALSE:

s1=bar

if [ -z "$s1" ]
then
echo "TRUE"
else
echo "FALSE"
fi

Those tests also showed the else syntax of the if statement.

Math/number equality tests
==========================

n1 -eq n2 Test if n1 equals n2
n1 -ne n2 Test if n1 is not equal to n2
n1 -lt n2 Test if n1 is less than n2
n1 -le n2 Test if n1 is less than or equal to n2
n1 -gt n2 Test if n1 is greater than n2
n1 -ge n2 Test if n1 is greater than or equal to n2

Here’s an example of how to test whether two numbers are equal:

if [ $n1 -eq $n2 ]
then
# do something
fi

Boolean and/or/not operators
============================

-a and
-o or
! not

Here’s an example of how to test perform a test using the and operator:

if [ $num -gt 0 -a $num -lt 10 ]
then
# do something here
fi

More powerful grouping operators
================================

If you need to perform multiple tests at one time you can use grouping operators, as shown in the example below.

a=5
b=20

if test \( $a -gt 0 -a $a -lt 10 \) -o \( $b -gt 0 -a $b -lt 20 \)
then
echo "TRUE"
else
echo "FALSE"
fi

That script echoes "TRUE".

Bourne shell arithmetic
=======================

In the Bourne shell math/arithmetic is performed using the expr command, like this:

sum=`expr $foo + $bar`
half=`expr $foo / 2`
times=`expr $foo \* 2`

Note that you can’t have any spaces before or after the equal sign in those (or any) shell script assignment statements.

A few other common shell tricks
===============================

Here are a few other tricks/techniques you will often see in Unix shell scripts:

cmd1 && cmd2 Run cmd1; if it returns 0 (success), run cmd2
cmd1 || cmd2 Run cmd1; if it returns non-zero, run cmd2
cmd1 & cmd2 Run cmd1 and also cmd2
(ls -1) Run the command "ls -1" in a subshell

Solaris patch backout script

Here is the back out script. It is NOT Sun Supported. Customer needs to
use it at his own risk.

#! /bin/sh
# This is just an example on how to remove all the patches
# applied in a Recommended Patch Cluster.

LOGFILE=/var/sadm/install_data/cluster_remove.log

echo
echo
echo
echo " ————————————————-"
echo " RUN THIS SCRIPT AS ROOT IN SINGLEUSER MODE"
echo " This script is compatible with Solaris 2.6 and higher."
echo
echo " The remove_cluster script will remove patches automatically"
echo " using any patch_order file which must exist in the"
echo " same directory where the script resides."
echo " The patch_order file can be copied from the last installed
echo " patch cluster or it can be created manually. It needs to be
echo " in a XXXXXX-YY format (one entry per line)."
echo
echo " The patches will be removed in a reverse order"
echo " (patch listed on the bottom of the patch_order file"
echo " will be removed first)."
echo " You might see some errors, as most likely not all of the patches"
echo " in the cluster were applied to your machine."
echo
echo " This process will be logged to: ${LOGFILE}"
echo
echo "Hit C to abort or RETURN to continue"
read response
#####
# Reverse patch_order file
#####

patchlist=`awk ‘{ line[NR] = $0 } END { for (i=NR; i>0; i=i-1)\
print line[i] }’ ./patch_order`
#####
# Remove them
#####
echo "" >> ${LOGFILE}
echo "" >> ${LOGFILE}
echo "*** Removal begins `date` ***" >> ${LOGFILE}
echo "Removing patches in reverse order from the patch_order file" | tee
-a ${LOGFILE}
echo "" >> ${LOGFILE}
###########
for patch in ${patchlist} ; do
echo "Removing ${patch}…" | tee -a ${LOGFILE}
( /usr/sbin/patchrm ${patch} ) >> ${LOGFILE} 2>&1
result=$?
if [ ${result} -ne 0 ]; then
echo " Removal of ${patch} failed. Return code
${result}." | tee -a ${LOGFILE}
fi
done
echo "***** remove_cluster script finished on `date` ******" | tee -a
${LOGFILE}
exit 0

Gather information on paths to LUNS

#!/bin/sh
#: john core 04-27-07
## gather lad info
#
# save file as get_lad_info.sh
# chmod 755 get_lad_info.sh
# ./get_lad_info.sh
####################

DATE=`date +%m%d%H%M`
TMPFILE=/tmp/${DATE}.txt
touch ${TMPFILE}

echo ""

echo "running healthck -a"
healthck -a >> ${TMPFILE}
echo ""
echo ""

echo \# lad
lad >> ${TMPFILE}

echo ""
echo ""

for ctrl in `lad | awk ‘{print $1}’`
do
echo controller: $ctrl
echo \# drivutil -d $ctrl
drivutil -d $ctrl >> ${TMPFILE}
echo ""
echo \# drivutil -i $ctrl
drivutil -i $ctrl >> ${TMPFILE}
echo ""
echo \# drivutil -I $ctrl
drivutil -I $ctrl >> ${TMPFILE}
echo ""
echo \# drivutil -l $ctrl
drivutil -l $ctrl >> ${TMPFILE}
echo ""
echo \# drivutil -p lun $ctrl
drivutil -p lun $ctrl >> ${TMPFILE}
echo ""
echo \# raidutil -c $ctrl -i
raidutil -c $ctrl -i >> ${TMPFILE}
echo ""
echo \# raidutil -c $ctrl -B
raidutil -c $ctrl -B >> ${TMPFILE}
echo ""
echo \# rdacutil -i $ctrl
rdacutil -i $ctrl >> ${TMPFILE}
echo ""
echo ""
echo ""
done

echo "/etc/osa/rmparams"
echo —————————————————————————
cat /etc/osa/rmparams >> ${TMPFILE}
echo —————————————————————————
echo ""
echo ""
echo "/kernel/drv/sd.conf"
echo —————————————————————————
cat /kernel/drv/sd.conf >> ${TMPFILE}
echo —————————————————————————
echo ""
echo ""
echo "/var/osa/rmlog.log (uuencoded)"
echo —————————————————————————
uudecode -p /var/osa/rmlog.log >> ${TMPFILE}
echo —————————————————————————
echo ""
echo ""
compress ${TMPFILE}

echo "finished!"

echo "file is ${TMPFILE}.Z – please upload to sunsolve with your case number in the name"

exit

Find errant processes

save file
chmod 755

will create a file labeled month day hour minute.txt

#!/bin/sh
#:john core
################################
#change TMPFILE to wherever you want
#################################
# crontab should look like this (run every 15 minutes)
#
# 15,30,45,59 * * * * /this_script
################################################################
DATE=`date +%m%d%H%M`
TMPFILE=/var/spool/crash/${DATE}.txt
top > $TMPFILE
ps -ef >> $TMPFILE
exit

ZFS getinfo script

#!/bin/ksh
#### start of zfs_getinfo script
#### Copyright 2004 Sun Microsystems, Inc.
#### Use of this script is AT YOUR OWN RISK.
#### There are NO WARRANTIES provided with this script.

STAMP=`/usr/bin/date +%Y%m%d%H%M%S`

TMPDIR="/tmp/ZFS.$STAMP"
TARFILE="ZFS.${STAMP}.tar"

/usr/bin/mkdir -p $TMPDIR/kstats

ZPOOLLIST=`/usr/bin/zpool ls`
for zp in $ZPOOLLIST; do
/usr/bin/cp /etc/zpool/$zp $TMPDIR
ZFSLIST=`/usr/bin/zfs ls $zp`
for zf in $ZFSLIST; do
/usr/bin/zfs getprop $zfs >> $TMPDIR/zfs_ls_${zp} 2>&1
done
done

/usr/bin/pkginfo -l SUNWzfs > $TMPDIR/pkginfo-l.SUNWzfs 2>&1

/usr/bin/zpool vdevs -v > $TMPDIR/zpool_vdevs_-v 2>&1
/usr/bin/zpool devices -v > $TMPDIR/zpool_devices_-v 2>&1

/usr/sbin/modinfo |/usr/bin/egrep -i "zfs|zpool|zvol" > $TMPDIR/modinfo 2>&1

/usr/bin/kstat -m zfs > $TMPDIR/kstats/zfs
/usr/bin/kstat -m unix -n zfs_znode_cache > $TMPDIR/kstats/unix.zfs_znode_cache
/usr/bin/kstat -m unix -n zil_dobj_cache > $TMPDIR/kstats/unix.zil_dobj_cache
/usr/bin/kstat -m unix -n zil_itx_cache > $TMPDIR/kstats/unix.zil_itx_cache
/usr/bin/kstat -m unix -n zfs_taskq > $TMPDIR/kstats/unix.zfs_taskq
/usr/bin/kstat -m unix -n spa_zio_issue > $TMPDIR/kstats/unix.spa_zio_issue
/usr/bin/kstat -m unix -n spa_zio_intr > $TMPDIR/kstats/unix.spa_zio_intr

/usr/bin/echo "zpool_version/s" | /usr/bin/mdb -k > $TMPDIR/mdb_zpool_version 2>&1
/usr/bin/echo "zfs_intent_log_disable/x" |/usr/bin/mdb -k > $TMPDIR/mdb_zil_disable 2>&1

echo "Now bundling for transfer to Sun"

cd /tmp
/usr/bin/tar cvf /tmp/${TARFILE} $TMPDIR
/usr/bin/gzip -9 /tmp/${TARFILE}

echo "Please forward the file /tmp/${TARFILE}.gz to Sun"

rm -rf $TMPDIR

#### end of zfs_getinfo script

killLP

creat in /usr/bin/killLP

chmod 755

#!/bin/sh
#:johncore
## usage killLP printer
###########################
if [ "$1" = "" ]
then
echo "usage: killLP printer_name"
exit
fi
cancel `lpstat -o | grep $1| awk ‘{print $1}’`
exit

check range of IP addresses for connectivity

#!/bin/bash
#####################################################################
##### Script to check range of IP addresses for connectivity #####
##### — Souvik Prasad (20.11.2003.) #####
#####################################################################

#### — configure— ####
network="203.197.118." # the class C network address with a trailing dot
lrange=65 # the lower limit of the range of IPs to scan
urange=127 # the upper limit of the range of IPs to scan
#### –end of configure– ####

while [ $lrange -le $urange ]
do
ip=`echo "$network$lrange"`
case `ping -qnc 3 -w10 $ip 2>&1` in
*’100% loss’*)
echo -e "$ip\txx"
;;
*)
echo -e "$ip\tok"
;;
esac
lrange=`expr $lrange + 1`
done

Update the nameserver cache information file

#!/bin/sh
#
# Update the nameserver cache information file once per month.
# This is run automatically by a cron entry.
#
# Original by Al Longyear
# Updated for BIND 8 by Nicolai Langfeldt
# Updated for BIND 9 on RedHat Systems by Souvik Prasad
# Miscelanious error-conditions reported by David A. Ranch
# Ping test suggested by Martin Foster
# named up-test suggested by Erik Bryer.
#
(
echo "To: root@localhost"
echo "From: root@localhost"

# Is named up? Check the status of named.
case `rndc status 2>&1` in
*refused*)
echo "named is DOWN. named.ca was NOT updated"
echo
exit 0
;;
esac

PATH=/sbin:/usr/sbin:/bin:/usr/bin:
export PATH
# NOTE: /var/named must be writable only by trusted users or this script
# will cause root compromise/denial of service opportunities.
cd /var/named 2>/dev/null || {
echo "Subject: Cannot cd to /var/named, error $?"
echo
echo "The subject says it all"
exit 1
}

# Are we online? Ping a server at your ISP
case `ping -qnc 1 -w60 www.vsnl.com 2>&1` in
*’100% loss’*)
echo "Subject: named.ca NOT updated. The network is DOWN."
echo
echo "The subject says it all"
exit 1
;;
esac

dig @e.root-servers.net . ns > named.ca.new 2> errors

case `cat named.ca.new` in
*NOERROR*)
# It worked
:;;
*)
echo "Subject: The named.ca file update has FAILED."
echo
echo "The named.ca update has failed"
echo "This is the dig output reported:"
echo
cat named.ca.new errors
exit 1
;;
esac

echo "Subject: The named.ca file has been updated"
echo
echo "The named.ca file has been updated to contain the following
information:"
echo
cat named.ca.new

chown root.root named.ca.new
chmod 444 named.ca.new
rm -f named.ca.old errors
mv named.ca named.ca.old
mv named.ca.new named.ca
rndc reload 2>&1
echo
echo "The nameserver has been restarted to ensure that the update is complete."
echo "The previous named.ca file is now called
/var/named/named.ca.old."
) 2>&1 > /tmp/update-named.ca
cat /tmp/update-named.ca | /usr/sbin/sendmail -t
rm -f /tmp/update-named.ca
exit 0