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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using system("command")

Status
Not open for further replies.

sylar

Programmer
Aug 25, 2007
3
FR
Hi,

1) I would like to execute in gawk a shell command looking like this:

---------------------------------------------------------------
#!/bin/gawk -f
begin{
system("echo 'test' | gawk '{print $0}'")
}
---------------------------------------------------------------

but I get this error message:

---------------------------------------------------------------
gawk: cmd. line:1: '{print
gawk: cmd. line:1: ^ invalid char ''' in expression
---------------------------------------------------------------

2) How define functions in BEGIN{}
For example :

---------------------------------------------------------------
#!/bin/gawk -f
begin{
func test(var) # definition of some function
{
if (match($0,var)!=0)
{
print NR":\t"$0
}
}
while ("cat testfile" | getline > 0) # execution of this function
using the output of "cat filetest"
{
test("foo")
}
}
---------------------------------------------------------------

Or how to define a function we need to use in BEGIN{} ?
 
Hi

First of all, [tt]BEGIN[/tt] with uppercase :
Code:
#!/bin/gawk -f
[red]BEGIN[/red] {
   system("echo 'test' | gawk '{print $0}'")
}
Then how you execute that script ? It works for me.

And just move the [tt]function[/tt] definition out from the [tt]BEGIN[/tt] block and will work :
Code:
#!/bin/gawk -f
[red]BEGIN[/red] {
   while ("cat testfile" | getline > 0) # execution of this function
using the output of "cat filetest"
   {
      test("foo")
   }
}

func test(var) # definition of some function
{
   if (match($0,var)!=0)
   {
      print NR":\t"$0
   }
}

Feherke.
 
Concerning the first script : i am currently testing it under windows xp with gawk 3.1.3 build with MinGW, found here:

And even with this simple example:
-------------------------------
BEGIN {
system("gawk 'BEGIN{}'")
}
-------------------------------

I get this message:
------------------------------------------------------
gawk: cmd. line:1: 'BEGIN{}'
gawk: cmd. line:1: ^ invalid char ''' in expression
------------------------------------------------------

Maybe a windows specific problem ...
 
I think I've found the problem.


Under windows, it is not advised to run win32 builds of unix programs with the default sucking shell %windir%\system32\cmd.exe


If you run this in cmd.exe:
gawk 'BEGIN{print NR}'
you will get the error message:
------------------------------------------------------
gawk: cmd. line:1: 'BEGIN{}'
gawk: cmd. line:1: ^ invalid char ''' in expression
------------------------------------------------------
On the other hand, if you use an unix shell (i was using a MinGW build of zsh), you don't get this problem.


But when you call the system() command in gawk.exe, it runs the command with cmd.exe instead of the shell you are using to launch gawk.exe


Thus, if you run this:
------------------------------------------------------
BEGIN {
system("echo gawk 'BEGIN{print NR}' | sh")
}
------------------------------------------------------
it will work


While this:
------------------------------------------------------
BEGIN {
system("echo gawk 'BEGIN{print NR}' | cmd")
}
------------------------------------------------------
is the same as
------------------------------------------------------
BEGIN {
system("gawk 'BEGIN{print NR}'")
}
------------------------------------------------------
 
Hi

Well, I am also on Windows XP, but I use CygWin. My shell is [tt]bash[/tt] and the [tt]awk[/tt] implementation is [tt]gawk[/tt]. I started CygWin with the cygwin.bat they provided and there I have a perfect Linux environment, excepting the terminal window. There your codes using the [tt]system()[/tt] function works correctly.

Personally I did not tried MinGW and after my experiences with CygWin probably never will.

Feherke.
 
This works on win xp (note the escaped quotes)

system("echo 'test' | \"C:/Program Files/GnuWin32/bin/gawk\" {print $0}")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top