Calling method from AWK command
I have varibales which represent order numer
I want to parse it and call to method handleOrder
I tried it but not working
#!/bin/ksh
ORDER_LIST=100,200,300
`echo $line |awk -F"," '{for(i=0;i<=NF;i++) HandleOrder printf $i}'`
HandleOrder
(
echo "My Order is $1"
}
Expected Result:
My Order is 100
My Order is 200
My Order is 300
Actual Result
开发者_StackOverflowSyntax Error
You need to define the HandleOrder
function inside the awk script so that it can be called in the awk script. Also, you want to start your loop at 1 so that you do not print $0
, the variable that contains the whole line. Something like:
#!/bin/ksh
ORDER_LIST=100,200,300
echo $ORDER_LIST | awk -F"," '
function HandleOrder(order) { print "My Order is ", order }
{for(i=1;i<=NF;i++) { HandleOrder($i) }}
'
If you wish to have HandleOrder
as a shell script instead of an awk function, then use something like:
#!/bin/ksh
ORDER_LIST=100,200,300
echo $ORDER_LIST | awk -F"," '
{for(i=1;i<=NF;i++) { system("HandleOrder.ksh " $i) }}
'
and create a shell script named HandleOrder.ksh
containing:
#!/bin/ksh
echo "My Order is $1"
Make sure that HandleOrder.ksh
is set executable and is in the $PATH
.
精彩评论