|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Passing awk variable argument to a script which is being called inside awk
consider the script below Code:
sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml
awk -F'[=|"|<|>|,]' '{for(i=1;i<=NF;i++){
if($i=="Alert id") {
if(id!="")
if(dt!=""){
cmd="sh someScript.sh "printf "%s",alDFid;"
cmd | getline prio
print "priority=" prio
close(cmd)
printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid; }
id=($i=="Alert id")?$(i+2):id; }
nm=($i==" name")?$(i+2):nm;
fx=($i==" fixed")?$(i+2):fx;
dt=($i~/^ [0-9]+-/)?$i" "$(i+1):dt;
alDFid=($i==" alertDefinitionId")?$(i+2):alDFid;
}
}END{
if(dt!=""){
printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid;
}
}' /tmp/alerts.xmlhere the script that i am calling is someScript.sh and to that i want to pass the contents of alDFid , i itred giving it in quotes , $ but none of it worked. even tried printf "%s",alDFid even this is not working. Does anyone know how to pass argument to script from Awk with awk variable?? ALSO why does Code:
awk -F'priority=' '{print $2}' | cut -d'"' -f2inside cmd="" wont work.. it wont consider awk inside this command. it keeps throwing error as Code:
| grep priority | awk -Fpriority= {print
awk: cmd. line:6: ^ unterminated string
./alertCheck_vivek.sh_date: line 26: cmd: command not found
./alertCheck_vivek.sh_date: line 26: getline: command not found
./alertCheck_vivek.sh_date: line 27: print: command not found
./alertCheck_vivek.sh_date: line 28: syntax error near unexpected token `cmd'
./alertCheck_vivek.sh_date: line 28: `close(cmd)' |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Your idea is correct, but as your code is absolutely unreadable, I only can guess that the variable alDFid is undefined when used to build the external command.
And, of course, you have an unterminated string, i.e. one unmatched double quote (I guess in the printf statement) Last edited by RudiC; 02-11-2013 at 06:10 AM.. Reason: typo / addendum |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
the error which was coming was due to below line Code:
awk: cmd. line:6: cmd="sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alertdefinition list --id=$myAlertDefID --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true | grep priority | awk -Fpriority= {print
awk: cmd. line:6: ^ unterminated string
./alertCheck_vivek.sh_date: line 26: cmd: command not found
./alertCheck_vivek.sh_date: line 26: getline: command not found
./alertCheck_vivek.sh_date: line 27: print: command not found
./alertCheck_vivek.sh_date: line 28: syntax error near unexpected token `cmd'
./alertCheck_vivek.sh_date: line 28: `close(cmd)'lets forget about that... the alDFid is defined below is the output Code:
13214 priority= ID=10101 NAME=APP-MS-lib_license_common-150040-licenseSchemaTampered-S_R FIXED=true DATE= 2013-01-22 07:54:51.3 AlDefID=13214 where the line above priority is alDFid the snippet which i used was Code:
awk -F'[=|"|<|>|,]' '{for(i=1;i<=NF;i++){
if($i=="Alert id") {
if(id!="")
if(dt!=""){
printf "%s\n",alDFid;
cmd="sh someScript.sh alDFid"
cmd | getline prio
print "priority=" prio
close(cmd)
printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid; }
id=($i=="Alert id")?$(i+2):id; }
nm=($i==" name")?$(i+2):nm;
fx=($i==" fixed")?$(i+2):fx;
dt=($i~/^ [0-9]+-/)?$i" "$(i+1):dt;
alDFid=($i==" alertDefinitionId")?$(i+2):alDFid;
}
}END{
if(dt!=""){
printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid;
}
}' /tmp/alerts.xmlin the above i want to pass the content of alDFid to someScript.sh script. But current implementation is passing string "alDFid " to the script. |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem passing a search pattern to AWK inside a script loop | quinestor | Shell Programming and Scripting | 14 | 03-14-2012 11:12 AM |
| Passing --usage as argument to awk script | kristinu | Shell Programming and Scripting | 2 | 11-09-2011 08:31 PM |
| Passing value as a command line argument in awk script. | humaemo | Shell Programming and Scripting | 4 | 07-25-2011 04:24 AM |
| Passing argument to system call in awk script | mikesimone | Shell Programming and Scripting | 6 | 03-02-2010 06:41 PM |
| Passing argument to awk script | AkumaTay | UNIX for Dummies Questions & Answers | 1 | 08-03-2001 11:12 AM |
|
|