Argument passing using for or while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Argument passing using for or while loop
# 1  
Old 04-08-2008
CPU & Memory Argument passing using for or while loop

Hi All,

My query is as below:

Am basically writing a parser script.
My input file has got some variables which are populated by the calling program.

callig program:

fun1("cat","dog","cow")

input.*
argument[1] first
argument[2] second

I want to write a script that should give me the result as below when executed:

first="$1"
second="$2"

Forget about the calling function but the script i guess can be done using a for loop or a while, both am not at all comfortable with.

Can some one please help me in this.
Sorry for making the query so complicated.

Thanks in advance
JS
# 2  
Old 04-08-2008
Am working on the same .. I came across the below query in awk.The fir loop is mentioned here just for the logic to understand.


for(i=0;i<=6;i++)
{
awk '/param/ {print $2"=\"$""\"" }' a.txt > we.txt
}

I want the we.txt to look like
first="$1"
second="$2"

Using my query i can print only
first="$"
second="$"

How can i print the value of "i "inside the awk statement using the for loop or while ????
This is what i tried to explain above Smilie

Thanks
JS
# 3  
Old 04-08-2008
okay, i didnt understand your first post, but i think you need to pass i as a parameter inside awk.. so here goes:

for(i=0;i<=6;i++)
{
awk -v value_of_i=$i '/param/ {print $2"=\"$"value_of_i"\"" }' a.txt > we.txt
}
# 4  
Old 04-09-2008
Hello ag79,

Thanks a lot for ur time.I ran the following query

for i in 1 2 3
do
awk -v value_of_i=$i '/param/ {print $2"=\"$"value_of_i"\"" }' a.txt > we.txt
done

But this gav me the result

first="$3"
second="$3"
third="$3"


but my out put should be

first="$1"
second="$2"
third="$3"

Is this possible?

Thanks
JS
# 5  
Old 04-09-2008
Hi All,

I found a solution :

a=1
awk '/param/{a++;print $2"=\"$" a"\""}' a.txt > we

Thanks
JS
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

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

3. 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

4. Shell Programming and Scripting

passing an option as an argument!

Hi Folks I have got to the point where I can specify the arguments but how to pass an option is still mystery to me. Example: temp.csh a b c d set temp1 = $argv set temp2 = $argv set temp3 = $argv echo $temp1 a echo $temp2 b echo $temp3 c d I WANT: temp.csh a b c d -S 1 set temp1... (2 Replies)
Discussion started by: dixits
2 Replies

5. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

6. Shell Programming and Scripting

Using GET, passing argument to bash

Hi guys! So, I use GET ( Simple user agent using LWP library. ) on a remote text file that is then passed to bash and executed. However, I need to pass that bash script a single argument, and so far nothing that I have tried has worked, although I have learned quite a bit about input/output... (5 Replies)
Discussion started by: Rhije
5 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

passing argument into awk

i'm trying to pass a numerical argument with function xyz to print specfic lines of filename, but my 'awk' syntax is incorrect. ie xyx 3 (prints the 3rd line, separated by ':' of filename) function xyz() { arg1=$1 cat filename | awk -F: -v x=$arg1 '{print $x}' } any ideas? (4 Replies)
Discussion started by: prkfriryce
4 Replies

10. Programming

Thread Argument Passing

#include <stdio.h> #include <pthread.h> #define NUM_THREADS 4 /* function to be executed by the new thread*/ void *PrintHello(void * threadid) { printf("\n %3d:Hello World!\n",threadid); pthread_exit(NULL); } int main(int argc, char * argv) { int *taskids; int... (2 Replies)
Discussion started by: narom
2 Replies
Login or Register to Ask a Question