Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shell interaction 3

Status
Not open for further replies.

codemut

Programmer
Feb 29, 2008
26
US
A challenge in converting a working bash script into a gawk script is hung up on passing variables for shell interaction. For example, the following is to run within an executable script, ie:
#!/usr/bin/gawk -f
BEGIN {
...
awkvar = 5
awkname = "name"
field = 2

command1 = (./script2.awk awkvar > outputfile""awkname"".txt)

print $1, $field | command1
...
Any ideas?
 
I'd replace this:
command1 = (./script2.awk awkvar > outputfile""awkname"".txt)
with this:
command1 = "./script2.awk "awkvar" > outputfile"awkname".txt"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV, thanks for the suggestion ... it leads to infinite process hang, however.
 
The process hang is fixed, but the situation is as before.

There is this error:
sh: -c: line 0: syntax error near unexpected token `('

Also got this error on a previous occasion from a bash script that was inadvertently using #!/bin/sh instead of #!/bin/bash

Maybe gawk has a default shell-interaction option that can be changed?
 
I should add: my default shell is bash. Regardless, unless specified in a script, I've noticed the system's output can be erroneous.
 
I also use bash and gawk and was receiving the same syntax error...until I double escaped characters with special meaning.
Code:
sql = "\"INSERT INTO \\`cdr\\`.\\`records\\` (\\`id\\`,\\`session_id\\`,\\`record_id\\`,\\`datum\\`,\\`milisec\\`) VALUES (NULL,'" session_id "','" record_id  "','" datum "','" milisec "');\""

print ("mysql --user=user --password=pass -e " sql) | "/bin/sh"
anyways, you should be able to create _any_ line using quoted strings, escaped special characters, and of course parenthesis to impose grouping order.

I am not sure if all above helps, but anyways :) good luck :)
 
zedland,

The code, with PHV's recommended change, seems to work as expected. One confusing thing is that when you are calling script2.awk you are both supplying it with standard input via the print command, and with an input filename, i.e. awkvar. gawk will ignore standard input if input files are specified on the command line (unless you process and clear them all in the BEGIN section I think).

Annihilannic.
 
After isolating two issues, the matter is somewhat resolved. This fix accords with PHV and Annihilannic's comments. Much of the solution, it seems, was to avoid piping data to multiple programs and to re-work the command to take as extra arguments the data that was previously piped. Evidently, the following bash embedding in gawk doesn't work on this system:
cmd = "./prog1 | tee >(./prog2 > oufile1) >(./prog3 > outfile2)"
print cmd | "sh"
The system objects to the "(" and ")". Later, I'll try quoting/escaping these as tehiznogud suggests. For now, each program is given its own feed of the same data. Redundant as it seems, it works.
 
sh is not necessarily an alias for bash (especially on non-Linux systems), and named pipes are unfortunately not a feature of bash on all operating systems, so it's probably best to avoid using them for portability reasons.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top