Recursive argument passing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursive argument passing
# 1  
Old 09-28-2010
Recursive argument passing

I'm writing a script that can be called on itself, and must be able to pass arguments down to itself properly.
The regular usage of the script is as follows:

Code:
myfun cmd1 'options1' cmd2 'options2' input

If you're interested, the nuts and bolts of this function simply compare the outputs of the 2 cmd#'s using their respective options and the same input on both. For example:

Code:
myfun cat '' cat '' input

Will essentially compare input to itself, whereas:

Code:
myfun cat '' cat '-n' input

Will compare input to input with line numbers.


However, if I try calling:

Code:
myfun myfun "cat '' cat '-n'" myfun "cat '' cat '-n'" input

The expected output doesn't come out. I get errors from the cat calls in the inner myfun calls reading something like:

Code:
cat: '': No such file or directory

What I'm getting from this is that when I use "cat '' cat '-n'" as an argument, it's really using "cat \'\' cat \'-n\'" or something similar, causing the quotes to remain in place in the inner calls.

I can post some code if that would help anyone in diagnosing this problem for me.


Thanks!
# 2  
Old 09-28-2010
Quote:
Originally Posted by Sunlis
I can post some code if that would help anyone in diagnosing this problem for me.
That would help a lot Smilie
# 3  
Old 09-28-2010
Alright, the code is a bit lengthy and redundant, so I think I'll keep it simple. I'll replace parts that aren't relevant with comments, just so you know what's going on.

Code:
checkExec() {
     # checks if argument is a valid, executable command or file
     # (sets $cmd variable to result, checked below)
}

# if [ help flag ] show help message

# if [ ${#} is not 5 ] show error message

# else

     # check first command, ${1}, using checkExec
     # if invalid, halt
     # else

          p1=${cmd}
          p1o=${2}

          # check second command, ${3}, using checkExec
          # if invalid, halt
          # else

               p2=${cmd}
               p2o=${4}

               shift 4

               # loop through input files
                    # make some (unique) tmp files to store data in
                    # save tmp paths in ${out1} and ${out2}

                    ${p1} ${p1o} ${1} > ${out1} 2>&1
                    ${p2} ${p2o} ${1} > ${out2} 2>&1

                    # compare out1 and out2 using diff
                    # echo something depending on what diff returns
                    # remove tmp files (out1, out2)
                    shift
               # end loop
          # fi
     # fi
# fi


Like I said before, this works fine for simple calls like:
Code:
myfun cat '' grep 'a' input

But fails when I try to use some recursion
Code:
myfun myfun "cat '' grep 'a'" myfun "cat '' grep 'b'" input


If interested, attached is the full file, ripe with comments, though the relevant material is posted here.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing a second argument

I am trying to pass a second argument like so: if ] then export ARG2=$2 else message "Second argument not specified: USAGE - $PROGRAM_NAME ARG1 ARG2" checkerror -e 2 -m "Please specify if it is a history or weekly (H or W) extract in the 2nd argument" fi however, it always goes... (4 Replies)
Discussion started by: MIA651
4 Replies

2. Shell Programming and Scripting

Passing argument not retrieving.

Hi I have a script which am trying to pass an argument which am trying to call using $1 but its not taking the value inside the if loop as it showing the error as if: Empty if.. Any help on this will be helpful. #!/usr/bin/csh echo $1 if ('$1' == "pp") then echo "Printing $1" endif (4 Replies)
Discussion started by: rogerben
4 Replies

3. Shell Programming and Scripting

Argument passing

How to pass the alphabet character as a argument in case and in if block? ex: c=$1 if a-z ]] then echo "alphabet" case $1 in a-z) echo "the value is a alphabet" edit by bakunin: please use CODE-tags. We REALLY mean it. (9 Replies)
Discussion started by: Roozo
9 Replies

4. Shell Programming and Scripting

Help with passing argument

Hi, I have a script that is scheduled with cron and runs every night. The cron part looks like this: 00 20 * * 0,1,2,3,4,5,6 /usr/local/bin/BACKUP TBTARM HOT DELETE My issue is with the 3rd parameter. Somewhere in the script, i want to tell the script to delete some files if the 3rd... (7 Replies)
Discussion started by: dollypee
7 Replies

5. Shell Programming and Scripting

passing argument in script?

hi, I want to implement some function to perform following task if ; then $TEXT = "Data_0" else $TEXT = $1 fi if ; then $Lines = 45 else $Lines = $2 fi Kindly suggest, thanks (11 Replies)
Discussion started by: nrjrasaxena
11 Replies

6. Shell Programming and Scripting

passing argument from one function to another

Hi all, In the given script code . I want to pass the maximum value that variable "i" will have in function DivideJobs () to variable $max of function SubmitCondorJob(). Any help? Thanks #!/bin/bash ... (55 Replies)
Discussion started by: nrjrasaxena
55 Replies

7. Shell Programming and Scripting

Passing argument to nawk

Hi all I have got a file digits.data containing the following data 1 3 4 2 4 9 7 3 1 7 3 10 I am writing a script that will pass an argument from C-shell to nawk command. But it seems the values in the nawk comman does not get set. the program does not print no values out. Here is the... (2 Replies)
Discussion started by: ganiel24
2 Replies

8. Shell Programming and Scripting

passing Argument

Hi All, i have script like below.. echo "1) first option" echo "" echo "2) second option" echo "" echo "*) please enter the correct option" read select case $select in 1) echo "first option selected" ;; 2) echo "second option selected" ;; *) echo "please enter the correct... (4 Replies)
Discussion started by: Shahul
4 Replies

9. Shell Programming and Scripting

Problem with Argument Passing

Greetings, I am wrapping the monitoring commands like vmstat, sar, iostat and call via arguments I want ./unix_stats.sh -v vmstat -p <SEC> -d <Duration> to give vmstat values, and similarly iostat etc.,. Also if I give ./unix_stats.sh -v vmstat -i iostat -p <SEC> -d <Duration> should give... (4 Replies)
Discussion started by: A_Rod
4 Replies

10. Shell Programming and Scripting

Passing argument from one script to other

Dear All, I have one script which accepts database name and user_id from the user, i have another script that will unload the data from all the tables based on the user_id accepted by the user. How can i pass the user_id from the 1st script to the other. My OS is sun solaris. Thanks in... (3 Replies)
Discussion started by: lloydnwo
3 Replies
Login or Register to Ask a Question