This is a collection of code patterns for some of the common UNIX programming tasks which tend to repeat from time to time. When not singled out and classified these patterns get easily lost in the bulk of an application-specific code, and constant rewriting (and debugging) of them from scratch is a senseless waste of time.
Normally there are several ways to code the same logic, and some ways are better than the other in terms or size, effectiveness, readability and ease of modification. These criteria are rather fuzzy (except for the "size"), and are affected by personal preferences. Yet, there seems to exist a certain common sense consensus of what is "good" – like mnemonic names and labels, use of indentation, commenting, etc.
My brother Victor and I have been collecting such "good" code patterns for a long time, and this is an attempt to select the most usable of them from our scattered notes, e-mails, and scripts into one document hyperlinked for an ease of use, and accessible from anywhere and by anybody who might have interest in it. However, this selection is not compiled for the Web only – rather it's our working reference that we try to maintain as accurate and current as possible. As such it inevitably will always lack an overall integrity and completeness, yet it is still a rather useful tool that often saves time.
Most of the tasks originated from a real-life experience of a UNIX programmer. Most of the solutions were coded and debugged in the process of practical programming of various applications. Every pattern was checked to the best of my competence, and as a rule was tested by a substantial time of repeated usage. This is not to suggest that it is in any way guaranteed to be error-free – you will be using any code you pick from this selection entirely at your own risk, and will be completely responsible yourself for any problems that might arise from this usage. So when trying a code, please exercise a common sense precautions, and test it most thoroughly yourself before applying it to anything serious.
This legal mantra been chanted, I want to mention that all error reports and any comments about this code pattern selection are most welcome, and I'll try to do my best to fix any known problem as quick as I'll be able to do this.
[[ -z $1 ]] – Parameter 1 is empty
[[ $1. = . ]] – Parameter 1 is empty
[[ "$1" = "" ]] – Parameter 1 is empty (use when $1 could contain special symbols)
[[ -n $1 ]] – Parameter 1 is non-empty
[[ $1. != . ]] – Parameter 1 is non-empty
[[ "$1" != "" ]] – Parameter 1 is non-empty (use when $1 could contain special symbols)
[[ $1. = [Aa]. ]] – Parameter 1 is equal to A or a (case insensitive)
[[ $1. = ^[Aa]*$. ]] – Parameter 1 starts with A or a (case insensitive)
[[ $1. != [Aa]. ]] – Parameter 1 is not equal to A or a (case insensitive)
[[ $1. != ^[Aa]*$. ]] – Parameter 1 doesn't start with A or a (case insensitive)
[[ -a ~/.profile ]] – File ~/.profile exists
[[ -d ~/script ]] – Directory ~/script exists
[[ ! -a ~/.profile ]] – File ~/.profile doesn't exist
[[ ! -d ~/script ]] – Directory ~/script doesn't exist
[[ "$Var" = "" ]] – Variable Var is empty (use when $Var could contain special symbols)
User=$(id | cut -f2 -d'(' | cut -f1 -d')')
if [[ $User = root || $User = mqm ]]
then main_script_code
else echo === Sorry, script_name script requires root or mqm
echo === authority for execution, but you are $User.
id
fi
# -------- Get Version of Installed MQSeries -----------------------------------
function gmqv { if [[ "$(pkginfo | grep mqm)" = "" ]]
then echo MQSeries is NOT installed on host $(hostname)
else pkginfo | grep mq
echo
pkginfo -l mqm
P=$(pkginfo | grep mqm- | awk '{print $2}')
if [[ "$P" = "" ]]
then echo MQSeries patch is NOT installed on host $(hostname)
else pkginfo -l $P
fi
unset P
fi; }
# gmqv
application mqm MQSeries for Sun Solaris 2
application mqm-upd02 MQSeries for Sun Solaris 2 - U474789
PKGINST: mqm
NAME: MQSeries for Sun Solaris 2
CATEGORY: application
ARCH: sparc
VERSION: 5.2.0
VENDOR: IBM
PSTAMP: p000-L001106 (packaged: November 07, 2000 - 05:51:46 AM)
INSTDATE: Jan 29 2001 14:53
STATUS: completely installed
FILES: 2248 installed pathnames
142 executables
47 setuid/setgid executables
58599 blocks used (approx)
PKGINST: mqm-upd02
NAME: MQSeries for Sun Solaris 2 - U474789
CATEGORY: application
ARCH: sparc
VERSION: 5.2.0
VENDOR: IBM
PSTAMP: cubam15162854
INSTDATE: Jan 29 2001 15:00
STATUS: completely installed
FILES: 71 installed pathnames
57 executables
28 setuid/setgid executables
22369 blocks used (approx)
dis qmgr crdate – Display current Queue Manager name and creation date
dis chl(*) – List all channels with their types
dis chl(*) type(SDR) – List all sender channels
dis chl(*) type(SVR) – List all server channels
dis chl(*) type(RCVR) – List all receiver channels
dis chl(*) type(SVRCONN) – List all server-connection channels
dis chl(*) type(SDR) conname – List all sender channel connection names
dis chl(*) type(SDR) xmitq – List all sender channel transmission queues
dis chl(*) type(SVR) xmitq – List all server channel transmission queues
ping chl(channel) – Ping specified non-running sender or qualified server channel
dis chl(channel) – Display all attributes of the specified channel
dis chs(*) – Display current status of every channel that has status data
dis chs(*) saved – Display saved status of every channel that had status data
dis chs(*) all – Display all current status data of every channel that has it
dis chs(*) saved all – Display all saved status data of every channel that had it
dis q (*) – List all queues
dis ql(*) – List all local queues
dis qr(*) – List all remote queues
dis qa(*) – List all alias queues
dis qm(*) – List all model queues
dis ql(*) defpsist – Display default message persistence of every local queue
dis ql(*) curdepth – Display current depth of every local queue (number of messages)
dis q(queue) – Display all attributes of the specified queue
clear ql(queue) – Delete all messages from specified local queue (clear queue)
dis pro(*) – List all processes
dis pro(process) – Display all attributes of the specified process
dis chl(SYSTEM.DEF*) all – Display all default attributes for every type of channel
dis q(SYSTEM.DEF*) all – Display all default attributes for every type of queue
end – End MQSC session
dis q (*) where(defpsist EQ NO) – List all queues non-persistent by default
dis ql(*) where(defpsist EQ NO) – List all local queues non-persistent by default
dis qr(*) where(defpsist EQ NO) – List all remote queues non-persistent by default
dis qa(*) where(defpsist EQ NO) – List all alias queues non-persistent by default
dis qm(*) where(defpsist EQ NO) – List all model queues non-persistent by default
dis ql(*) where(curdepth GT 99) – Display current depth of all local queues with more than 99 messages
dis ql(*) curdepth where(usage EQ XMITQ) – Display current depth of all transmission queues
dis chs(*) where(status NE RUNNING) – Display all channels with current status other than RUNNING
dis chs(*) where(status NE STOPPED) – Display all channels with current status other than STOPPED
dis chs(*) where(status EQ STOPPED) – Display all channels that are currenty STOPPED
dis chs(*) where(status EQ RETRYING) – Display all channels that are currenty RETRYING
dis chs(*) where(chltype EQ SDR) – Display current status of all sender channels
dis chs(*) where(chltype EQ RCVR) – Display current status of all receiver channels
dis chs(*) where(chltype NE SVRCONN) – Display current status of all channels except SVRCONNs
dis chl(*) where(xmitq EQ xmitq) – Display names of all channels having specified xmitq
dis chs(*) where(xmitq EQ xmitq) – Display status of all channels having specified xmitq