Odd(?) shell script practise


 
Thread Tools Search this Thread
The Lounge What is on Your Mind? Odd(?) shell script practise
# 1  
Old 05-07-2013
Odd(?) shell script practise

Hello,
I often stumble over a common shell coding practise.
Example 1:
Code:
#!/bin/sh 
# 
# Licensed Materials - Property of IBM
# Rational ClearCase
# (C) Copyright IBM Corp. 1999, 2010.  All Rights Reserved
# US Government Users Restricted Rights -
# Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp
# 
# Start/Stop ClearCase daemons as well as
# mount or umount MVFS file systems.
#
...
INSMOD="/sbin/insmod -f"
MODPROBE="/sbin/modprobe"
RMMOD=/sbin/rmmod
LSMOD=/sbin/lsmod
PIDOF=/sbin/pidof
ECHO="/bin/echo -e "
NAWK=/bin/awk
GREP=/bin/grep
RM=/bin/rm
PS=/bin/ps
CP=/bin/cp
LN=/bin/ln
MKDIR=/bin/mkdir
EXPR=/usr/bin/expr
...

Example 2:
Code:
#!/bin/ksh
# OPC_WHAT_STRING="@(#)HP Operations Smart Plug-in for Databases 11.40.000 (10/22/08)"

########################################################################
#
# File:         spi_migrate.sh
# Description:  determines the DCE / HTTPS agent environment and starts
#               migration of all available SPIs
# Language:     Bourne Shell
# Package:      -none-
#
# (c) Copyright 2004 Hewlett-Packard Development Company, L.P. 
#
########################################################################
# set -x

ECHO=echo
CAT=cat
MYSYS=`uname -s`
if [ $MYSYS = "HP-UX" ] ; then
  GREP=/usr/bin/grep
  AWK=/usr/bin/awk
  SED=/usr/bin/sed
  DIFF=/usr/bin/diff
elif [ $MYSYS = "SunOS" ] ; then
  GREP=/usr/xpg4/bin/grep
  AWK=/usr/xpg4/bin/awk
  SED=/usr/xpg4/bin/sed
  DIFF=diff
elif [ $MYSYS = "AIX" ] ; then
  GREP=/usr/bin/grep
  AWK=/usr/bin/awk
  SED=/usr/bin/sed
  DIFF=/usr/bin/diff
elif [ $MYSYS = "OSF" ] ; then
  GREP=/usr/bin/grep
  AWK=/usr/bin/awk
  SED=/usr/bin/sed
  DIFF=/usr/bin/diff
elif [ $MYSYS = "Linux" ] ; then
  GREP=grep
  AWK=awk
  DIFF=diff
  ECHO='echo -e'
...

Example3:
Code:
#! /bin/sh
#
# Log file pattern detector plugin for Nagios
# Written by Ethan Galstad (nagios@nagios.org)
# Last Modified: 07-31-1999
#
...
# Paths to commands used in this script.  These
# may have to be modified to match your system setup.
# TV: removed PATH restriction. Need to think more about what this means overall
#PATH=""

ECHO="/bin/echo"
GREP="/bin/egrep"
DIFF="/usr/bin/diff"
TAIL="/usr/bin/tail"
CAT="/bin/cat"
RM="/bin/rm"
CHMOD="/bin/chmod"
TOUCH="/bin/touch"

Do you see what is in common?
Always define all commands with its platform-dependent path and store it in UPPERCASE variables.
Sometimes I even see a for loop that finds out where a command lives and stores the result - in an UPPERCASE variable.
Always UPPERCASE!

And later in the scripts there is of course
Code:
$ECHO ... | $GREP ... | $AWK ... | $WC ...

instead of
Code:
echo ... | grep ... | awk ... | wc ...

My Questions:

1. have you met that, too?
2. Is there a book or a University that teaches this?
# 2  
Old 05-07-2013
1) Yes, I've seen it a lot.
2) I'm not aware of such University either.

It's a matter of preference which in turn is driven by the known convention of capitalizing all environment variables and shell internal variables.

$CAT UUoC.txt | $GREP stuff | $CUT stuff is definitely more visible and less prone to confusion than $cat UUoC.txt | $grep stuff | $cut stuff.

It also helps when you want to throw a few extra flags into the command. Consider this for example:
Code:
SSH="$(which ssh) -o ConnectTimeout=4 -o UserKnownHostFile=stuff -o Port=12345 -o PermitLocalCommand=yes -o NoHostAuthenticationForLocalhost <...>"

I usually prefer GREP=$(which grep) or even GREP=$(which grep 2>/dev/null || echo :) if I want to be extra paranoid, but I only do this when writing shell scripts.

For compiled languages most people I know use #define kVariableName which seems to be pretty standard in C-based languages (I only use Obj-C though).
# 3  
Old 05-07-2013
It is fairly common practice when writing software that is to be used in multiple operating environments to use a scheme like one of these as a configuration option when installing the software. Obviously the copyrighted code in example 2 from HP configures itself each time the script runs based on the results of a call to uname. How the IBM script in example 1 and the nagios script in example 3 are setup may be hidden in the ... areas of the code you didn't show or during the installation of these scripts.

I don't know of any university course that teaches this style, but you can bet that HP, IBM, and Oracle have coding guidelines (and probably internal classes) that specify coding styles for these kinds of application scripts.
# 4  
Old 05-08-2013
I like...

Although not quite the same........

Stragely enough I try to use capitalisation whenever I can, even when coding is case insensitive:-

https://www.unix.com/windows-dos-issu...atch-file.html

But this idea I like...

<Thumbs up>
# 5  
Old 05-08-2013
Some of the ... code in the IBM script:
Code:
LISTVIEWUSERS="${FUSER} -m `ls -d $VIEWPATH/* 2>/dev/null` `${MOUNT} -t mvfs |${NAWK} '{print $3}'`"
...
PIDLIST=`${ECHO} ${OTHERPIDS} ${VIEWUSERPIDS} ${SETVIEWPIDS} | ${TR} ' ^I' "${NEWLINE}${NEWLINE}" | ${SORT} -u`

The even forgot about the ls. The ^I is a literal TAB character.
I would write that as
Code:
LISTVIEWUSERS="fuser -m `ls -d $VIEWPATH/* 2>/dev/null` `mount -t mvfs | awk '{print $3}'`"
...
PIDLIST=`echo ${OTHERPIDS} ${VIEWUSERPIDS} ${SETVIEWPIDS} | tr ' \t' "${NEWLINE}${NEWLINE}" | sort -u`

and at the beginning of the script only put a general
Code:
export PATH
PATH=/usr/xpg4/bin:/bin:/usr/bin:/usr/sbin:/sbin

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Best practise for keeping cronjobs across 2 servers in sync

Hi all, I have 2 server A and B. B is acting as standby for A. The cronjobs running in A must not be run in B until failover. The activation of cronjobs in B can be manual. In server A, I am doing the following 1) create a cron/job script that does "crontab -l >... (5 Replies)
Discussion started by: javanoob
5 Replies

2. Shell Programming and Scripting

Odd behaviour with Expect password update script

Hi there. I've been forced by circumstance to write an expect script to handle password updates on a number of servers. There's a mix of Solaris 8, 9, 10, RedHat and Ubuntu. There's no chance the client will allow us to hook them up to a directory, so we have to make do. This script is mostly... (0 Replies)
Discussion started by: whetu
0 Replies

3. Shell Programming and Scripting

Odd results when my script runs from cron..

Hi folks, So I wrote a script to run "top", "awk" out values fro the "top" and send the results to a data file. I then set it to run in cron every 15 minutes. Now I'm noticing that the script, and it's sub-commands are not always cleanly finishing and, in my investigations, I am also... (11 Replies)
Discussion started by: Marc G
11 Replies

4. Shell Programming and Scripting

Calling a Perl script in a Bash script -Odd Situation

I am creating a startup script for an application. This application's startup script is in bash. It will also need to call a perl script (which I will not be able to modify) for the application environment prior to calling the application. The problem is that this perl script creates a new shell... (5 Replies)
Discussion started by: leepet01
5 Replies

5. Shell Programming and Scripting

something odd with my awk script

The code I am using #!/bin/sh for FILE in *.cfg; do awk '{ print; if ($1 == "host_name") store_name = $2; if ($1 == "register") { printf("\t\t parents\t\t\t %s-ilo\n", store_name); } }' "$FILE" > ../new-files/hosts/$FILE sed -i -e "s/notification_options.*/notification_options... (0 Replies)
Discussion started by: jag7720
0 Replies

6. Shell Programming and Scripting

"Odd" behavior exiting shell script

Is it normal behavior for a shell script that terminates to terminate its parent shell when executed with the "." option? For example, if I have the example script (we'll name it ex.sh): #!/bin/sh if then echo "Bye." exit 2 fi And I execute it like this: >./ex.sh It... (6 Replies)
Discussion started by: DreamWarrior
6 Replies

7. Shell Programming and Scripting

Is it bad practise to exit in function?

Question is title, I don't understand why all examples I am reading return constants error values instead of just exiting under certain conditions... then back in main script test return value and exit if true, to me seems like a lot of extra typing and test conditions which can make for bulky and... (5 Replies)
Discussion started by: gcampton
5 Replies

8. UNIX for Dummies Questions & Answers

Odd .sh behavior in script

Hello, I have been working on a what I thought was a fairly simple script for installing a software kit on Linux and Unix I am not new to scripting but am far from being fluent in sh scripting. any assistance would be appreciated. I have an odd bug occuring when executing the script. When... (2 Replies)
Discussion started by: robertmcol
2 Replies

9. UNIX for Dummies Questions & Answers

Need Unix Terminal for practise on Rental basis ...plz help!

Hey Guys,, Have just got started with Unix , I need UNIX Terminal to practise commands. Does any website host such services ? Happy Holidays... (9 Replies)
Discussion started by: rrover1977
9 Replies

10. UNIX for Dummies Questions & Answers

even odd script

I need a unix script that check for even or odd. EXAMPLE:::: please enter the number to check: 12 the output: This is an even number it has to have prompts. (2 Replies)
Discussion started by: snyper2k2
2 Replies
Login or Register to Ask a Question