LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to pass argument from command prompt (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-argument-from-command-prompt-854396/)

smritisingh03 01-05-2011 11:53 AM

how to pass argument from command prompt
 
Hi
when I am running the script below,it performs on whatever logfile u type ,i.e, ./scriptname logfilename

But how do I convert it into a function and then call it from another script.I mean how do I prompt the user to enter a logname and then capture the name in the function and when calling this function from another script how do I pass the parameter.

please help and thankyou so much in advance!!!

my script looks like:

awk ' {

if (index ( $0, ". . exporting table") >0 || index ($0, ". . exporting partition") >0) #searches for the pattern in string

{


if ($4 != "partition"){ #$4 is either table or a partition
i=$6; #$6 is the number of rows stored in variable i here

if (table_flag ==0 && table_name ==temp_table_name){ # checking the flag

printf table_name > "logcountOP";
printf " " > "logcountOP";
print j > "logcountOP";
}

table_name = $5; #$5 has the name of the table from which the rows have been imported
temp_table_name = table_name;
table_flag =1; #setting the flag to 1

}




if ($4 == "partition") {

i=i+$6; #summing up the rows in partitioned tables
printf("value of i in first: %d\n",i);
j=i;
table_flag=0; #setting flag to 0

}
if (table_flag !=0 && $6 !=""){

printf table_name >> "logcountOP";
printf " " >> "logcountOP";
print i >> "logcountOP";

}



}
} ' < $1

Snark1994 01-05-2011 05:15 PM

If the script you typed above is /usr/bin/myScript, then you should just be able to use:

Code:

#!/bin/bash

read logfilename
myScript $logfilename

This reads in one word to the variable logfilename, then runs your script with the same variable as its parameter. Was this what you were looking for?

All the best,

grail 01-05-2011 10:39 PM

Seeing as this is simply an awk script in a bash wrapper, just get rid of bash altogether and ask for log name in the BEGIN:
Code:

#!/usr/bin/awk -f

BEGIN{
    printf "Enter name of logfile: "
    getline logfile < "-"

    while(getline < logfile){
        <your stuff here>
    }
}


smritisingh03 01-08-2011 09:18 AM

Thank uoy so much!!!it works.


All times are GMT -5. The time now is 12:06 AM.