Pipes and args passed to a shell script

Pipes and args passed to a shell script

Post by Lowri » Mon, 10 Mar 1997 04:00:00



I am trying to write a shell script that will take some arguments and a
stream (I hope thats the correct terminology) that will result in a file
being placed in a particular directory. Here is some backround info and
what I've learned so far:

- I am using the bourne shell.

- The script is called from an Informix 4GL (4.xx series) program
running on the Standard Engine (5.xx series).

- To print a report, there is a form type menu the user fills in. This
is normally the same command as would be used from the command line,
such as lp -dprintername -valid_switches -oprinter_options. The user
gives this printer a name for use in the informix program, and specifies
the unix configured printer by this name when requesting a print job.

This works fine for local as well as remote unix printers using lp.

In order to print to a printer attached to a networked PC, we use a
product from James River called Ice.lp. This is a program that lives on
the PC with a counterpart that runs as a daemon on the unix box. To
print to the PC-printer, we use a script in place of the lp command
described above. The PC based program uses a pre-defined socket and
checks periodically for the existance of any file in a particular
directory on the unix box. If a file exists there, it sends it to the
printer attached to the PC.

The part of the script that puts the file to be printed in the correct
directory looks like this:

cp $1 >> /path_to_ice.lp_dir/filename

When called from the informix program, the report to be printed gets
copied as a file and placed in the correct directory as expected. If I
look at $1, however, it is null. If I add a single argument, the arg
becomes $1 and I change the line above to cp $2, but no file gets
copied. If I look at $2 it is null. A programmer explained to me that
informix doesn't pass the script a file, it opens a pipe to a stream
and thats what gets fed to the script.

My question is how to write the script to accept arguments as well as
the stream. How do I identify this stream in the script so it gets put
in the file and not one of the arguments. The flow of the script looks
like this:

echo "some printer commands" > file1
cp $1 >> file1
echo "some reset the printer commands" >> file1
mv file1 /path_to_icelp_dir/filename

What I really don't get is how this works at all (yet it does) given
that $1 has no value (at least, when I try to look at it). Then, if I
give the script an argument, the argument becomes $1 and $2 is null, yet
if I specify $2 to be cp'ed nothing gets cp'ed.

Thanks in advance for any help in this area.

Tom Lowrie

 
 
 

Pipes and args passed to a shell script

Post by Bill Marc » Mon, 10 Mar 1997 04:00:00



>I am trying to write a shell script that will take some arguments and a
>stream (I hope thats the correct terminology) that will result in a file
>being placed in a particular directory. Here is some backround info and
>what I've learned so far:

>- I am using the bourne shell.

>- The script is called from an Informix 4GL (4.xx series) program
>running on the Standard Engine (5.xx series).

> To print a report, there is a form type menu the user fills in. This
>is normally the same command as would be used from the command line,
>such as lp -dprintername -valid_switches -oprinter_options. The user
>gives this printer a name for use in the informix program, and specifies
>the unix configured printer by this name when requesting a print job.

>This works fine for local as well as remote unix printers using lp.

>In order to print to a printer attached to a networked PC, we use a
>product from James River called Ice.lp. This is a program that lives on
>the PC with a counterpart that runs as a daemon on the unix box. To
>print to the PC-printer, we use a script in place of the lp command
>described above. The PC based program uses a pre-defined socket and
>checks periodically for the existance of any file in a particular
>directory on the unix box. If a file exists there, it sends it to the
>printer attached to the PC.

>The part of the script that puts the file to be printed in the correct
>directory looks like this:

>cp $1 >> /path_to_ice.lp_dir/filename

In most versions of Unix, the 'cp' command requires two or more arguments
and doesn't use standard input or output.  Apparently your 'cp' acts like
'cat' if it has no arguments.  In the script below, try changing:
cp $1 >> file1
to:
cat >> file1

- Show quoted text -

>When called from the informix program, the report to be printed gets
>copied as a file and placed in the correct directory as expected. If I
>look at $1, however, it is null. If I add a single argument, the arg
>becomes $1 and I change the line above to cp $2, but no file gets
>copied. If I look at $2 it is null. A programmer explained to me that
>informix doesn't pass the script a file, it opens a pipe to a stream
>and thats what gets fed to the script.

>My question is how to write the script to accept arguments as well as
>the stream. How do I identify this stream in the script so it gets put
>in the file and not one of the arguments. The flow of the script looks
>like this:

>echo "some printer commands" > file1
>cp $1 >> file1
>echo "some reset the printer commands" >> file1
>mv file1 /path_to_icelp_dir/filename

>What I really don't get is how this works at all (yet it does) given
>that $1 has no value (at least, when I try to look at it). Then, if I
>give the script an argument, the argument becomes $1 and $2 is null, yet
>if I specify $2 to be cp'ed nothing gets cp'ed.

>Thanks in advance for any help in this area.

>Tom Lowrie


--
Bill Marcum    bmarcum at iglou dot com
"...and eleven long haired friends of Jesus in a chartreuse microbus..."

 
 
 

Pipes and args passed to a shell script

Post by Daniel Marti » Tue, 11 Mar 1997 04:00:00


<3322FE6C.3...@mint.net>, Lowrie had asked questions on this subject.
Since I have extensive experience with same, I offer all the below to
all who might benefit therefrom - Daniel

We use SCO UNIX 3.2v4.2 and ICE.TCP ver 4.3. We're also using Informix
SQL v 4.1 and Informix SE 5.1.

Below are the scripts we use to:

1) Create a new UNIX lp "printer" to be used by ICELP
2) Define UNIX Printer interface scripts for HP, Oki & IBM printers

Use these any way you find helpful or informative.

Note that we have chosen two kludges that _all_ printers to be used by
ICE.TCP and ICELP _must_ obey:

1) "ice_" must be the first four characters of any UNIX lp
    name (e.g., ice_hp1) to be used by ICELP.
2) The UNIX working directory for ICELP is: /usr/tmp

If you wish otherwise, considerable modification is needed.
Ditto if you're using non-SCO UNIX.

The three interfaces provided prepend all necessary printer
control-codes into the file to be printed.  Therefore be sure that you
have defined a "Generic/Text" printer for Windows and then use that
name as the "Printer Device" when you set up PC Print Services with
SETTCP.

: "FROM: /usr/spool/lp/pr_make/pr_make.ice"
# by Dan Martin (DRM/CTG) Thu Nov 16 14:39:11 CST 1995
# Automatically creates a new UNIX-to-printer-on-PC for ICELP
# Works OK on SCO UNIX 3.2v4.2
#
# NOTES: To create a new ICELP printer (i.e., a printer connected to the
#        parallel port on the [ab]Users PC), copy the script "pr_make.ice"
#        into a file named for how you want the UNIX lp spooler to recognize
#        it (e.g., ice_xyz7).  See also /u/spims2/icetcp/README.ICE
#
#        The name of script-copy MUST begin with "ice_" due to the needs of
#        /bin_drm/mkicedir.sh (see its internal documentation).
#
#        IN THE COPY, appropriately edit the variables orgrem1 and orgmod1.
#        For orgmod1, use any of these appropriate /usr/spool/lp/model/ice*
#        interface scripts: icelp.dum  iceoki.ibm  iceoki.fxe  iceHP.jet
#
# Comments & Suggestions: drmar...@sound.net  -OR- Compuserve: 75046,1331
#
orgnam1=""                        # lp printer name (script-name if blank)
orgrem1="PLANT: ICELP for UUU PC" # Mod PLANT & UUU as required.
orgdev1="/dev/null"               # ALWAYS /dev/null for ICE TCP/IP Printer
# ICELP scripts in /usr/spool/lp/model/ are: iceoki.fxe iceoki.ibm iceoki.ib2
orgmod1="iceoki.fxe"
bedefault="NO"                    # shall this printer be the system default?
# if true, lp printer-name will be made same as this script:
[ -z "$orgnam1" ] && orgnam1="`basename $0`"
export orgnam1 orgrem1 orgdev1 orgmod1
if [ ! -s "/usr/spool/lp/model/${orgmod1}" ]
then
  echo "\nPrinter Driver specified (\"/usr/spool/lp/model/${orgmod1}\") NOT FOUND!\n"
  exit 1
fi
waiter() {
echo "\nPress <Retn> key to continue...\c"
read waitin
echo

}

echo "
About to make the printer called \"$orgnam1\"
.                 ... which is a \"$orgrem1\"
.                 ...using model \"$orgmod1\"
...to be attached to device-port \"$orgdev1\""
waiter
if [ -f /usr/spool/lp/admins/lp/interfaces/$orgnam1 ]
then
  echo "Old interface for ${orgnam1} is now being saved as:
  /usr/spool/lp/admins/lp/interfaces/${orgnam1}.old"
  mv  /usr/spool/lp/admins/lp/interfaces/${orgnam1} \
      /usr/spool/lp/admins/lp/interfaces/${orgnam1}.old
  cp  /usr/spool/lp/admins/lp/interfaces/${orgnam1}.old \
      /usr/spool/lp/admins/lp/interfaces/${orgnam1}
  /usr/lib/lpadmin -x $orgnam1
fi
/usr/lib/lpadmin -p "$orgnam1" -m "$orgmod1" -o nobanner -v "$orgdev1" \
                 -D "$orgrem1 model $orgmod1 on $orgdev1" 2>/dev/null
/usr/lib/accept "$orgnam1"
/usr/bin/enable "$orgnam1"
if [ "$bedefault" != "NO" ]
then
  echo "\nNow making printer \"$orgnam1\" the System Default Printer."
  /usr/lib/lpadmin -d "$orgnam1"   # makes THIS printer the system default
fi
ice="$orgnam1"
if [ ! -d "/usr/tmp/$ice" ]
then
  echo "ICE DIR NEEDED FOR: $ice - NOW MAKING IT."
  mkdir /usr/tmp/$ice
  chmod 777 /usr/tmp/$ice
  mkdir /usr/tmp/$ice/icelp_jobs
  chmod 777 /usr/tmp/$ice/icelp_jobs
else
  echo "Ice dir \"/usr/tmp/$ice\" Found OK"
fi
sleep 2
echo "\nDONE! Please verify (and WRITE DOWN) the statistics below:\n"
sleep 1
lpstat -a "$orgnam1"
lpstat -p "$orgnam1" -D "$orgnam1"
echo
# EOF

# Printer Interface "iceHP.jet" - copy into /usr/spool/lp/model.
: "                  INTERFACE FROM: /usr/spool/lp/model/iceHP.jet"
# by Dan Martin (drmar...@sound.net) Mon Feb 05 11:15:22 CST 1996
# Tested with: HP LaserJet 4+    HP DeskJet 660C
#                 (PCL V)           (PCL III)
##################################################################
## HP PCL III Interface for use with ICE.TCP ICELP on local PC. ##
## SETS UP TRICKS for HP printers using at least PCL III.       ##
## Supports: 6 & 8 lpi, 10, 12, 17 cpi, Italics (it),           ##
## Letter (nlq), Draft (hsd), and Utility (utl) modes.          ##
###################################################################
# Comments & Suggestions: drmar...@sound.net  -OR- Compuserve: 75046,1331
#
# NOTES: Information taken from HP Fax-Back Documents 2531 and 2561.
#        According to PCL 5 Printer Language Technical Reference Manual
#        (Sept. '90), PCL V is a superset of PCL III, so this script
#        should work with both HP LaserJet 4+  and  HP DeskJet 660C.
#        Both printer types should be set at factory defaults.
#
#        Command Codes should be entered in this sequence:
#           Orientation, Symbol Set, Spacing, Pitch,
#           Character height, Style, and Typeface.
#
# EXTENSIVELY enhanced (by DRM) from the minimal ICELP interface
#             script, icelp.scr (c) 1993, James River Group, Inc.
#
# See also: /usr/spool/lp/pr_make/pr_make.ice (also by DRM)
#
# This script will queue print jobs in a directory below $tdir
# named $printer/icelp_jobs/  For example, if the printer name is ice_rgt,
# the directory under which ice_rgt's print jobs will be queued is:
# $tdir/ice_rgt/icelp_jobs/  (which will be made if necessary).
# The ICELP running on the [ab]User's PC will fire up a FTP session
# every so often and go looking for files therein to print.
#
propts="/tmp/propts"
if [ -w "$propts" ]
then
  optrak="YES"   # voyeur mode
else
  optrak="NO"
fi
#
# optrak="NO"  # voyeur override
printer=`basename $0`
request=$1
name=$2
title=$3
copies=$4
options=$5
file=$6
if [ "$optrak" = "YES" ]
then
  etad="`date`"
  cat << BOX >$propts

  lp-generated options as of: $etad

  0  Interface: "$printer"
  1     Job ID: "$request"
  2    User ID: "$name"
  3      Title: "$title"
  4   # copies: "$copies"
  5 lp options: "$options"
  6       File: "$file"

BOX
fi
# Set supported options for printer.  Each must be preceeded by "-o ".
# Any [logical] combo can be stacked up as per: -o E -o 20 -o it -o nlq
# Tricks as per HP Fax-Back Documents 2531 and 2561.
trk=""
# Warm-reset HP printer to default mode:
defmode=" E"
# Portrait, Letter size, 6 lpi, Fixed spacing, Line Termination,
# 0 Top Margin, Bi-Directional, Disable PerfSkip
ourway=" &l0O &l2A &l6D (s0P &k2G &l0E &l0L"
# Form Feed:
FF=" &l0H"
# NOTE: This script always puts $defmode and $ourway before any other $trk
for ooh in $options
do
  case "$ooh" in
    e|E) # Extend Vertical Capacity to 8 Lines-Per-Inch
         # Best use is with "c|C|15|17)" below
         trk="${trk} &l8D"      # Works !
         ;;
     10) # 10 Characters-Per-Inch
         trk="${trk} (s10H"     # Works !
         ;;
     12) # 12 C-P-I Elite
         trk="${trk} &k4S"     # Works !
         ;;
 c|C|15|17|20) # 16.6 C-P-I Compressed
         trk="${trk} &k2S"      # Works !
         ;;
     it) # Italics
         trk="${trk} (s1S"      # Works !
         ;;
 lq|nlq) # Near-Letter-Quality
         trk="${trk} (s2Q"      # Not sure (nlq & util look same)
         ;;
d|D|hsd|utl) # High-Speed-Draft
         trk="${trk} (s1Q"      # Not sure (nlq & util look same)
         ;;
  esac
done
#
# Determine if the ICELP job-directory exists, if not, create it:
tdir=/usr/tmp
if [ ! -d "$tdir" ]
then
  mkdir $tdir
  chmod 777 $tdir
fi
jdir="$tdir/$printer/icelp_jobs"
if [ ! -d "$jdir" ]
then
  mkdir $tdir/$printer
  chmod 777 $tdir/$printer
  mkdir $jdir
  chmod 777 $jdir
fi
# Send the file(s) to icelp directory output $copies times, nameing
# each one $$_$copies, then $$_$copies -1, etc.
[ -z "$copies" ]    && copies=1
[ "$copies" = "0" ] && copies=1
while  [ "$copies" -gt 0 ]
do
  cop="$jdir/$$_$copies"      # The print-file to be assembled.
  echo "${defmode}${ourway}\c"  >$cop
  echo "$trk\c"                >>$cop
  cat $file                    >>$cop
  echo "${FF}${defmode}\c"     >>$cop # Eject-page and Warm-reset
  chmod 666 $cop
  copies=`expr $copies - 1`
# ICELP "daemon" running on [ab]User's PC deletes all $cop when through.
done
if [ "$optrak" = "YES" ]
then
  ls -l $jdir >>$propts
  cat << BOX  >>$propts

  Note: Files above are under "$jdir/"

  Control-Codes prepended to the file were (defmode+ourway+options):
  defmode = "$defmode"
  ourway  = "$ourway"
  options = "$trk"

BOX
fi
exit 0
# EOF iceHP.jet

# Printer Interface "iceoki.fxe" - copy into /usr/spool/lp/model.
: "                  INTERFACE FROM: /usr/spool/lp/model/iceoki.fxe"
#                    Also linked to: /usr/spool/lp/model/iceoki.321
# by Dan Martin (drmar...@sound.net) Thu Nov 16 14:37:12 CST 1995
###################################################################
## OKI-321 Interface for use with ICE.TCP ICELP on local PC.     ##
## SETS UP PRINTER TRICKS for OKIDATA 320/321 in Epson FXe mode. ##
## Supports: 6 & 8 lpi, 10, 12, 17, & 20 cpi, Italics (it),      ##
## Letter (nlq), Draft (hsd), Wide (w), and Utility (utl) modes. ##
###################################################################
# Comments & Suggestions: drmar...@sound.net  -OR- Compuserve: 75046,1331
#
# WARNING: REQUIRED Oki "Menu" Settings:
#          Font              Pitch            10 CPI
#          General Control   Emulation Mode   Epson FXe
#          Vertical Control  Line
...

read more »

 
 
 

1. How to pass a variable from a shell script to another shell script...

Hi,
         I have written a shell script 1............and internally its
calling another shell script 2........input to  redhat specific
chkconfig command... which executes and builds links in bootstartup
scipt directories (rc.d).

Actually i needs to pass a variable $DIRECTORY  of my command available
( can hard code but i am
setting it in the 1st script) to the 2nd script how to pass that... As
the setting is happening before the calling of
shell script2, i just tried like putting  echo "$DIRECTORY" into the 2nd
shell script, which is not working is there
a way to get the functionality.

shellscript1:
#!/bin/sh
---------
---------
---------
DIRECTORY="/sbin/ntp"
---------
---------
/sbin/chkconfig "shellscript2"
---------

shellscript2:
#!/bin/sh
-------
echo "$DIRECTORY"     <------put to see whether getting the value here
or not..

$DIRECTORY   -c ntp.conf          <------if it is so ,  i could use like
this.................

thanks,
srinivas.

--
office ph: 091-40-3607619

2. Wanted - Motif vendors.

3. Passing args to scripts run with 'at'?

4. Root can't print in Slackware 7.1

5. Passing args to a Korn script

6. modularization of i386 setup_arch and mem_init in 2.4.18

7. Passing args to another script

8. Hostname resolution

9. passing bourne shell args to C program

10. How do you pass a value from a shell script into a sqlplus script?

11. Passing a variable between shell script and perl script

12. Passing a variable from a Perl script to a shell script

13. Hiding shell script args