Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Variables in a shell script

0 views
Skip to first unread message

Robert A. Hayden

unread,
Nov 4, 1993, 5:19:00 AM11/4/93
to

I am trying to write a very quick shell script, but I am having some
problems with passing command line variables optionally into the shell.

Here I use Pine as my mail reader, but I am trying to write up a shell
script that will allow me to PGP sign mailings outside of pine.

Thus, what I'd like to do is something like:
signmail [-s 'subject'] user filename

Where the subject is, of course, optional. The script will then "pgp -s
filename" and then will "mail [-s 'subject'] user < filename.asc".

The problem is, I'm not sure how to make the subject portion wholly
optional. I can assign 'user' and 'filename' to $1 and $2, but it is
rather cumbersome to have the subject as $3 and require it to be at the
end of the command.

So, the question is, how can I have it plug the subject in if it is
defined in the command, but ignore it if not?

(Yes, I'm sure this is simple, and maybe even a duplicative effort of
another more functional program, but I'm putzing around here and there with
something I need. Just not sure how to do it).

--
____ Robert A. Hayden <=> hay...@krypton.mankato.msus.edu
\ /__ -=-=-=-=- <=> -=-=-=-=-
\/ / Finger for Geek Code Info <=> Veteran of the Bermuda Triangle
\/ Finger for PGP 2.3a Public Key <=> Expeditionary Force -- 1993-1951
-=-=-=-=-=-=-=-
(GEEK CODE 1.0.1) GAT d- -p+(---) c++(++++) l++ u++ e+/* m++(*)@ s-/++
n-(---) h+(*) f+ g+ w++ t++ r++ y+(*)

Mike Schienle

unread,
Nov 5, 1993, 1:45:31 PM11/5/93
to
In article <1993Nov4.0...@vax1.mankato.msus.edu> hay...@krypton.mankato.msus.edu (Robert A. Hayden) writes:
>
>I am trying to write a very quick shell script, but I am having some
>problems with passing command line variables optionally into the shell.
>
>Thus, what I'd like to do is something like:
> signmail [-s 'subject'] user filename
>

Hi Robert -

Here's a quick example for you.

# count command line variables
case $# in
0) ;;
*) case $1 in
-s)
# more than one variable and one of them was -s
subject="$2"
shift
shift
;;
esac

Is that enough to get you started/into trouble?

Later,

Mike Schienle m...@sbjse0.sbrc.hac.com
Hughes SBRC, Santa Barbara, CA (805)562-7466

Harald Schroepfer

unread,
Nov 6, 1993, 8:12:34 AM11/6/93
to
80...@sbjse0.sbrc.hac.com (Mike Schienle) writes:

In article <1993Nov4.0...@vax1.mankato.msus.edu> hay...@krypton.mankato.msus.edu (Robert A. Hayden) writes:
>
> I am trying to write a very quick shell script, but I am having some
> problems with passing command line variables optionally into the shell.
>
> Thus, what I'd like to do is something like:
> signmail [-s 'subject'] user filename
>

#! /bin/sh
#
# cf getopt(1), getopts(1)
#

while getopts s: option
do
case $option in

s) subject=$OPTARG
if [ "$subject" = "" ]; then
echo "-s: subject required!" >&2
exit 1
fi
;;

esac

shift `expr $OPTIND - 1`
done

user=$1
filename=$2

# ... other tests

Mark Allman

unread,
Nov 10, 1993, 9:39:32 PM11/10/93
to
>I am trying to write a very quick shell script, but I am having some
>problems with passing command line variables optionally into the shell.
>
>Here I use Pine as my mail reader, but I am trying to write up a shell
>script that will allow me to PGP sign mailings outside of pine.
>
>Thus, what I'd like to do is something like:
> signmail [-s 'subject'] user filename
>
>Where the subject is, of course, optional. The script will then "pgp -s
>filename" and then will "mail [-s 'subject'] user < filename.asc".

i would use getopts for this. like so:

#!/bin/sh

while getopts s: OP
do
case $OP in
s) SUB=$OPTARG ;;
esac
done


shift `expr $OPTIND - 1`

mail -s "$SUB" $1 < $2


hope that helps. :-)

allman

--

Mark Allman - Ohio University Computer Science
mal...@ace.cs.ohiou.edu * mal...@duce.cs.ohiou.edu
All programmers are playwrights, and all computers lousy actors.

0 new messages