script converstion. csh->sh

script converstion. csh->sh

Post by Jonathan H. N. Ch » Sun, 15 Aug 1993 07:21:20



I recently decided to rewrite a script in bourne shell to allow me to
break lines up into manageable segments (is there a way to do this in
csh? see code below for example).

I have hardly ever written scripts in sh and rarely in csh either.
Perhaps someone could comment on my code, and give pointers on ways
to make it simpler/shorter/faster/etc.

(I guess leaving out comments/indentation might make
the scanning go faster... 8^))

The script is for a sun IPC that does not automatically do its own
DNS lookups so the script munges hostnames into IP numbers to avoid
the user having to do it manually each time.

-jonathan

---------------------------------------------------------------------------

#!/bin/csh
# Usage: with-lookup <host> <command> [<args>] IP [<args>]

# need lots of :q's when dealing with variables
#   to avoid unintentional expansion
# several uses of `|& cat' to avoid stderr printing to screen
#   (will rewrite in /bin/sh at some point maybe)

# check we have enough arguments
set ARGC=$#argv
if ( $ARGC < 2 ) then
  echo ""
  echo '        Usage: with-lookup <HOST> <COMMAND> [<ARGS>] IP [<MORE-ARGS>]'
  echo ""
  exit -1
endif

set NSa=134.225.32.8    # use sussdirt
set NSb=134.225.32.1    # use sunserv
set NSc=131.239.2.1     # use think.com as nameserver to lookup host

echo 'Trying DNS...'

foreach NS ( $NSa $NSb $NSc )
  # sanity check:
  # make sure we can connect to the name server in the first place...
  if ( "`ping $NS |& cat`" == "$NS is alive" ) then
    # now do the lookup
    set HOST=`nslookup $1. $NS |& \
              sed -n -e '/^\*\*\*/s/^.*[Tt]ime.*out.*$/ETIME/p' -e '/^\*\*\*/s/^.*can.t find.*$/EDOM/p' -e '/^\*\*\*/s/^.*No address.*$/EADDR/p' -e '/^$/,$s/^Address:[    ]*\([0-9.]*\)$/\1/p'`
    if ( $HOST == "ETIME" ) then
      echo "Time-out during query. Trying another nameserver..."
    else
      break     # may need to test for more than just ETIME
    endif
  else
    set HOST=ECONN
  endif
end

# make sure DNS lookup found an address
# if not, try an NIS lookup before we continue
if ( $HOST == "ETIME" || $HOST == "EDOM" || \
     $HOST == "EADDR" || $HOST == "ECONN" ) then
  echo "DNS lookup failed ($HOST). Trying NIS..."
  set HOST=`ypcat hosts | grep $1`
  if ( $#HOST >= 1 ) then
    set HOST=$HOST[1]
  endif
  if ( $HOST == "" ) then
    echo "***Error: unable to find address for "'"'"$1"'"'". Aborting."
    exit -1
  endif
endif

# substitute occurences of IP in argv and CMD
#   IP -> ip-number in argv
#   IP -> hostname in CMD
set CMD=($argv:q)
set CT=3
while ( $CT <= $ARGC )
  if ( $argv[$CT]:q == "IP" ) then
    set argv[$CT]=$HOST
    set CMD[$CT]=$1
  endif

end

# for reference, print the command we hope was intended
echo ""
echo `hostname`% $CMD[2-]:q
exec $argv[2-]:q
exit 0

---------------------------------------------------------------------------

#!/bin/sh
# Usage: with-lookup <host> <command> [<args>] IP [<args>]

# check we have enough arguments
if [ $# -lt 2 ]; then
  echo ''
  echo '        Usage: with-lookup <HOST> <COMMAND> [<ARGS>] IP [<MORE-ARGS>]'  echo ''
  exit -1
fi


NAME=$1

NSa=134.225.32.8        # use sussdirt
NSb=134.225.32.1        # use sunserv
NSc=131.239.2.1         # use think.com as nameserver to lookup host

echo 'Trying DNS...'

for NS in $NSb $NSa $NSc; do
  # sanity check:
  # make sure we can connect to the name server in the first place...
  if [ "`ping $NS 2>&1 | cat`" = "$NS is alive" ]; then
    # now do the lookup
    HOST=`nslookup $NAME. $NS 2>&1 |                             \
          sed -n -e '/^\*\*\*/s/^.*[Tt]ime.*out.*$/ETIME/p'     \
                 -e '/^\*\*\*/s/^.*can.t find.*$/EDOM/p'        \
                 -e '/^\*\*\*/s/^.*No address.*$/EADDR/p'       \
                 -e '/^$/,$s/^Address:[    ]*\([0-9.]*\)$/\1/p'`
    if [ "$HOST" = ETIME ]; then
      echo 'Time-out during query. Trying another nameserver...'
    else
      break     # may need to test for more than just ETIME
    fi
  else
    HOST=ECONN
  fi
done

# make sure DNS lookup found an address
# if not, try an NIS lookup before we continue
if [ "$HOST" = ETIME -o "$HOST" = EDOM -o \
     "$HOST" = EADDR -o "$HOST" = ECONN ]; then
  echo "DNS lookup failed ($HOST). Trying NIS..."
  set X `ypcat hosts | grep $NAME`
  if [ $# -gt 1 ]; then
    HOST=$2
  else
    echo '***Error: unable to find address for "'$NAME'". Aborting.'
    exit -1
  fi
fi

# substitute occurences of IP in ARGV and CMD
#   IP -> ip-number in ARGV
#   IP -> hostname in CMD
set X $ARGV
shift 2


# for reference, print the command we hope was intended
echo ''
echo "`hostname`% $CMD"

exec $ARGV
exit 0

---------------------------------------------------------------------------

#!/bin/csh
# example application for with-lookup

# Usage: Ftp [<HOST> [<PORT>]]

set ARGC=$#argv

if ( $ARGC >= 3 ) then
  echo ""
  echo 'Usage: Ftp [<HOST> [<PORT>]]'
  echo ""
  exit -1
else if ( $ARGC >= 1 ) then
  exec with-lookup $1 ftp IP $2

else if ( $ARGC == 0 ) then
  exec ftp
endif

---------------------------------------------------------------------------

--
Jonathan H N Chin, 5 kyu | Dept. of *netics, | "Respondeo, etsi mutabor"
                         | University of Reading |