Need help with procedure and parameters in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with procedure and parameters in a script
# 1  
Old 07-05-2011
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:

Code:
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.
# 2  
Old 07-05-2011
Code:
man getopts

Running the following
Code:
#! /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
Code:
~$ ./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


Last edited by Skrynesaver; 07-05-2011 at 07:23 AM.. Reason: formatting chaos
This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 07-05-2011
Thanks.

I'd like to add the ability to do the

Code:
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 ----------

Quote:
Originally Posted by Skrynesaver
Code:
man getopts

Running the following
Code:
#! /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
Code:
~$ ./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

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

Code:
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.
# 4  
Old 07-05-2011
If not set File, it's nothing => test give error, need argument
Code:
File=""
#...
if [ -f "$File" ]

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

This User Gave Thanks to kshji For This Post:
# 5  
Old 07-06-2011
Quote:
Originally Posted by kshji
If not set File, it's nothing => test give error, need argument
Code:
File=""
#...
if [ -f "$File" ]

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

Thanks.

This helped solve one of my problems.

My next problem is if I wanted to do something like

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

or

Code:
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.

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why i can't execute the procedure in shell script?

i have the following code inside a shell script .prog in oracle server when i call the program DBMS_OUTPUT.PUT_LINE(x_return_status|| ln_rep_req_id); will return 0 , it is very strange , i try to submit the concurrent request in oracle , and it can successfully executed, what am i missing ? i... (1 Reply)
Discussion started by: feilhk
1 Replies

2. Shell Programming and Scripting

Sub script calling procedure

Hi, I have a doubt regarding how sub scripts will be executed and interested to know internal workflow, For example - My main script is A,it calls a script B then B will call some C....and so on. Then B script run parallel to A or it will wait B to execute then A will continue. ... (1 Reply)
Discussion started by: nag_sathi
1 Replies

3. Shell Programming and Scripting

Executing procedure using script

Hi, I want to have a automted script to exceute 20 procedures one by one. Suppose below is one procedure once it get executed script will write "PL/SQL procedure successfully completed." to a log for ex- exec dbms_stats.gather_table_stats(); Now later procedure starts executing... (1 Reply)
Discussion started by: sv0081493
1 Replies

4. Shell Programming and Scripting

How to get OUT parameter of a stored procedure in shell script?

I am invoking a SQL script from shell script. This SQL script will invoke a stored procedure(which has the OUT parameter). I want to have the OUT parameter in the shell script as a variable. Is this possible? (6 Replies)
Discussion started by: vel4ever
6 Replies

5. Shell Programming and Scripting

Executing Procedure from shell script..

Hello, I created a sql file to create a Procedure, and it was successfully created. I created a sql file to execute the procedure, and it did without any errors, but i dont see the data been updated. The Execute procedure.sql script is: BEGIN set serveroutput on size 1000000 execute... (5 Replies)
Discussion started by: msrahman
5 Replies

6. Shell Programming and Scripting

getting proper o/p from a procedure in a script

foll. is my code snippet. #!/bin/ksh retVal=`sqlplus -s user/passwd@oracle_sid <<EOF SET SERVEROUTPUT ON SIZE 100000 DECLARE STATUS_VALUE CHAR; BEGIN SELECT temp1 INTO STATUS_VALUE FROM sai; DBMS_OUTPUT.PUT_LINE(STATUS_VALUE); END; / SET... (1 Reply)
Discussion started by: sainathdeg
1 Replies

7. Shell Programming and Scripting

calling procedure from script

Hi All, My script will call a storedprocedure #!/bin/bash # # Script for ........... . ../conf/setting.env # Environment file to set the DATABASE #TODAY=`date '+%Y%m%d_%H'` #echo TODAY = $TODAY sqlplus -s $DATABASE <<EOF spool $TRACKING_LOGDIR/CalcFreqPgsVues_H.log exec... (3 Replies)
Discussion started by: scorpio
3 Replies

8. Shell Programming and Scripting

Passing parameters form unix to Oracle procedure

Hi, I have screen which was desined in PL/SQL Catridges in apps. In that screen some enterable fields these values r the passing parameters to create value sets, functions, menus etc in apps by using front end screens. Now in that screen i have a button. when i click that button it have to... (0 Replies)
Discussion started by: rajasekharamy
0 Replies

9. Shell Programming and Scripting

DB2 stored procedure (with input parameters) from script

I have a db2 stored procedure on my database which require 3 parameters, I have the following db2 command in a sql script CONNECT TO SAMPLE; CALL BACKUP(INPUT_1, INPUT_2, INPUT3); Usually, I would just invoke this sql script from my shell script like below db2 -tf... (1 Reply)
Discussion started by: mpang_
1 Replies

10. UNIX for Advanced & Expert Users

Starting a script from a Database procedure

Hey guys, I have this problem : I can run on an sqlprompt !ftp_file2.ksh test.xml 172.16.204.81 Anonymous Anonymous which will ftp the file from Unix to an NT machine, but if I do exec shell('sh ftp_file2.ksh test.xml 172.16.204.81 Anonymous Anonymous') it does NOTHING. I have no... (4 Replies)
Discussion started by: vidireporting
4 Replies
Login or Register to Ask a Question