Operating System - HP-UX
1828243 Members
2534 Online
109975 Solutions
New Discussion

Re: Shell script's command line parameter delimiter

 
SOLVED
Go to solution
Victor Balenton
Occasional Advisor

Shell script's command line parameter delimiter

Just wanted to get some ideas on how to pick up the delimiters from a command line in Korn shell script.
I will have delimiter ":" instead of space (which is default) in the command line for e.g:
# testmail ::::
/usr/sbin/sendmail -t << EOM
From:$1
To:$2
Subject:$3
$(/usr/bin/uuencode $4 $5)
EOM

In the above script,I want to pick up the delimiter ":" from the command line so as to pick up the positional parameter ($1,$2,$3..) values.

Appreciate if you can give some ideas.

Thanks much,
7 REPLIES 7
Ian Kidd_1
Trusted Contributor

Re: Shell script's command line parameter delimiter

Something like this might work. I didn't get the opportunity to test it since I removed sendmail...

STRING=$1
FROM=`echo $STRING | awk -F: '{print $1}'`
TO=`echo $STRING | awk -F: '{print $2}'`
SUBJECT=`echo $STRING | awk -F: '{print $3}'`
FILE_ATTACH=`echo $STRING | awk -F: '{print $4}'`
NAME_ATTACH=`echo $STRING | awk -F: '{print $5}'`


this would preceed the rest of your script.
and instead of having "From:$1", you would have
From:${FROM}
etc.

It seemed to work when I used `echo $FROM`, etc.
If at first you don't succeed, go to the ITRC
Bill Hassell
Honored Contributor

Re: Shell script's command line parameter delimiter

Note that the shell will still pay attention to spaces as parameter separators so you'll either have to require " around the string or escape the special meaning of spaces (in the subject line) with \ as in:

testmail "myname@mydomain.com:somebody@another.com:This is a test:/etc/issue"

testmail myname@mydomain.com:somebody@another.com:This\ is \a \test:/etc/issue

Otherwise, there will be $2 $3 etc. Be sure to test for $# to make sure there's only 1 parameter.


Bill Hassell, sysadmin
Steven E. Protter
Exalted Contributor

Re: Shell script's command line parameter delimiter

I'm not sure this appropriate but I've developed a mail with attachment script. It take parameters 5 through whatever and uses them to put together the sujbect.

It might be what you want. Probably not.

I'm posting it anyway. Zero point me if its useless.

You'll have to use awk as shown above to do it with colons and spaces will still screw that up.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
curt larson_1
Honored Contributor
Solution

Re: Shell script's command line parameter delimiter

you can try something like this

oldIFS="$IFS"
IFS=":"
set -- $1
IFS="$oldIFS"
Elmar P. Kolkman
Honored Contributor

Re: Shell script's command line parameter delimiter

What you can do is:
(echo $1 | awk '{printf "From:%s\nTo:%s\nSubject:%s\n",$1,$2,$3}'
lf=$(echo $1 | cut -d: -f4)
af=$(echo $1 | cut -d: -f5)
/usr/bin/uuencode "$lf" "$af"
) | sendmail -t

I would not recommend using awk for splitting the command line for each argument, as awk uses a lot of resources. The cut command is a lot smaller and does the trick quite right.

Mind the quotes for the uuencode arguments, because you could have spaces in the local or attachment filenames, due to the colon argument seperation.
Every problem has at least one solution. Only some solutions are harder to find.
Elmar P. Kolkman
Honored Contributor

Re: Shell script's command line parameter delimiter

You could of course also use awk to split the arguments completely:
eval $(echo $1 | awk -F: '{printf "FROM='%s'\nTO='%s'\nSUBJECT='%s'\nLOCALFILE='%s'\nATTACHMENT='%s'\n",$1,$2,$3,$4,$5}')

Then the rest is simple and straight forward, since you have 5 environment variables containing the seperate parts.
Every problem has at least one solution. Only some solutions are harder to find.
Victor Balenton
Occasional Advisor

Re: Shell script's command line parameter delimiter

Thanks to all the ideas, appreciate it much, I picked up one of them which would help us better in our environment. I was planning to send this script's command parameters from an SAP application.

Best Regards,
Victor