0

I'm new in bash and awk, I have code like this:

i=0
while [ $i -lt 10 ] ; 
do

    tes1() {
        set_from=$i;
        if [ -z "${set_from}" ]; then set_from=1; fi
             awk '
             {
                time=$1;
                from=$3;
                to=$4;
                status=$5;
                set_from = set_from_set;
                set_to = 10;

                if (from == set_from && to == set_to){
                print time, from, to, status;
            }
        }'  set_from_set="$set_from" tes.txt;

    }



done

I don't know to get $i from the first line, maybe anybody here knows how to get that $i for function tes1()? Thankyou...

Ihsan
  • 5
  • 3
  • Bash doesn't have "methods" and it's not at all clear why you have a function declaration inside another function declaration. – tripleee Feb 15 '19 at 10:48
  • oh I'm sorry, I'm wrong about that method, I mean function. yes I have a function declaration inside another function declaration because I need that. – Ihsan Feb 15 '19 at 10:54
  • 1
    I'm curious. I have programmed in Bash in 25+ years and I cannot recall a situation where that was necessary. Can you expand on what it does for you? – tripleee Feb 15 '19 at 10:55
  • oh I'm sorry again, after I checked again it turned out that I didn't need that first function, can you help me how can I get that **$i**? – Ihsan Feb 15 '19 at 11:08
  • If you have trouble understanding the answer you got, please comment on it. – tripleee Feb 15 '19 at 11:09
  • Whatever it is you're trying to do, having 3 variables named `from`, `set_from`, and `set_from_set` isn't helping the legibility of your code! – Ed Morton Feb 15 '19 at 14:14
  • 1
    Thanks for the correction, I'll change it @EdMorton – Ihsan Feb 18 '19 at 08:33

2 Answers2

1

The simplest way is to pas it as argument to function:

tes0 "$1"

In this way you are passing first argument provided to script into function as also first argument.

  • 3
    Maybe spell out that the function will receive the argument in `"$1"`(and use quotes like that everywhere; see https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable/27701642) – tripleee Feb 15 '19 at 10:49
0

I'm guessing you are actually looking for something like

for ((i=1; i<=10; i++)); do
     awk -v set_from_set="$i" '{
            time=$1;
            from=$3;
            to=$4;
            status=$5;
            set_from = set_from_set;
            set_to = 10;

            if (from == set_from && to == set_to){
            print time, from, to, status;
        }
    }' tes.txt
done

Declaring a function ten times but not ever executing that function seems misdirected and wasteful.

Probably the Awk script could be refactored to do the loop on each input line, but without knowing what your data looks like, it's uncertain whether that would be an improvement. If the data file is big, probably try to process it only once.

If your actual question is "how do I pass a parameter to a function" that would be

fun () { printf "Hello, I got called with %s\n" "$1" }

fun 1
fun 2

or more generally

funtoo () {
    printf "All my arguments:\n"
    printf "%s\n" "$@"
}

funtoo one "two 2" 'three 3' four\ 4
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I'm still confused, because I only want to get $i from outside the function for set_from – Ihsan Feb 15 '19 at 11:37
  • You can use a global variable, of course; but that's poor practice. Can you explain why you don't want to explicitly pass the value to Awk? – tripleee Feb 15 '19 at 11:39
  • Thankyou, I can run it with global variable. Because I need to repeat my data with awk and other function – Ihsan Feb 18 '19 at 08:29
  • It's not really obvious how you cannot repeat your data with Awk and some other function if the variable is not global. In fact, I would argue that the contrary is true. – tripleee Feb 18 '19 at 09:52