Need help with procedure and parameters in a script

Hi,

I'm trying to create a procedure within my script and what I want is something like:

myproc () {
  PARM1=value after -t
  PARM2=value after -y
}

myproc -t "PARM1 Value" -y "PARM2 Value"

Of course that won't work since there's no such command as "value after -t". But this is what I'm trying to achive.

I'm on AIX by the way.

Thanks.

man getopts

Running the following

#! /bin/bash  
while getopts "abc:n" opt; do    
    case $opt in
         a )   a="true";;
         b )   b="true";;
         c )   c=$OPTARG;;
         n )   n="true";;
    esac 
done  
shift $(($OPTIND - 1)) # remove all options from the argument array
echo "a is $a 
b is $b 
c is $c 
n is $n 
remaining args are $*";

returns

~$ ./test.sh -abc setting -n  filename otherfile directory 
a is true 
b is true 
c is setting 
n is true 
remaining args are filename otherfile directory
1 Like

Thanks.

I'd like to add the ability to do the

myproc -t "PARM1" << EOT
Text here
EOT

How can I access the value passed in that way?

Regards.

---------- Post updated at 04:49 AM ---------- Previous update was at 04:30 AM ----------

How many arguments are allowed in this? I tried using it but kept getting an error on the last argument.

email() {
  while getopts "t:c:b:s:f" opt
  do    
      case $opt in
           t )   To=$OPTARG;;
           c )   Cc=$OPTARG;;
           b )   Bcc=$OPTARG;;
           s )   Subject=$OPTARG;;
           f )   File=$OPTARG;;
      esac 
  done  
  shift $(($OPTIND - 1)) # remove all options from the argument array
  echo "t is $To 
  c is $Cc 
  b is $Bcc 
  s is $Subject 
  f is $File
  remaining args are $*";

(
  echo "From: NoReply@Adshocker.com"
  echo "To: $To"
  echo "Cc: $Cc"
  echo "Bcc: $Bcc"
  echo "Subject: $Subject"
  echo "MIME-Version: 1.0"
  echo 'Content-Type: multipart/mixed; boundary="-1234567890"'
  echo
  echo '---1234567890'
  echo "Content-Type: text/plain"
  echo "Content-Disposition: inline"
  echo
  echo
  echo
  echo 

  if [ -n $File ]
  then
    echo
    echo '---1234567890'
    echo 'Content-Type: application; name="'${File##*/}'"'
    echo "Content-Transfer-Encoding: uuencode"
    echo 'Content-Disposition: attachment; filename="'${File##*/}'"'
    echo
    uuencode $File $File
    echo
    echo '---1234567890--'
  fi
) | sendmail -t
}

The error I get is "ksh: test: 0403-004 Specify a parameter with this command.".

Thanks.

If not set File, it's nothing => test give error, need argument

File=""
#...
if [ -f "$File" ]
# maybe better testing: File has value and file exist
if  [ "$File" !=  ""  -a -f  "$File"  ]
1 Like

Thanks.

This helped solve one of my problems.

My next problem is if I wanted to do something like

echo "This is my body message" | email -t "admin@adshocker.com" -s "Sample"

or

email -t "admin@adshocker.com" -s "Sample" << EOT
This is my body message
EOT

How should I do it?

Appreciate your help.

Thanks.

---------- Post updated 07-06-11 at 12:54 AM ---------- Previous update was 07-05-11 at 09:28 PM ----------

Never mind.

I figured out how to do it.

For anyone interested, here's my complete code.

(
  echo "From: NoReply@Adshocker.com"

  while getopts t:c:b:s:f: opt
  do    
      case $opt in
           t )   echo "To: $OPTARG";;
           c )   echo "Cc: $OPTARG";;
           b )   echo "Bcc: $OPTARG";;
           s )   echo "Subject: $OPTARG";;
           f )   File=$OPTARG;;
      esac 
  done  
  shift $(($OPTIND - 1)) # remove all options from the argument array

  echo "MIME-Version: 1.0"
  echo 'Content-Type: multipart/mixed; boundary="-1234567890"'
  echo
  echo '---1234567890'
  echo "Content-Type: text/plain"
  echo "Content-Disposition: inline"
  echo

  while read body
  do
    echo $body
  done

  echo
  echo 

  for i in $File
  do
    if [ -n "$i" ] && [ -f "$i" ]
    then
      echo
      echo '---1234567890'
      echo 'Content-Type: application; name="'${i##*/}'"'
      echo "Content-Transfer-Encoding: uuencode"
      echo 'Content-Disposition: attachment; filename="'${i##*/}'"'
      echo
      uuencode $i $i
    fi
  done

  echo
  echo '---1234567890--'
) | sendmail -t

Thanks guys.