Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#4 execution of exec() internal

closed
nobody
General (151)
1
2001-10-22
2000-08-09
Anonymous
No

I have noticed that when place the following line in my bsh file that I get undesirable results:

exec( "cat /myhome/contacts | grep test | grep -v grep | awk '{print $2}'" );

Results

# This file is automatically generated during installation

Component Primary POC Secondary POC
------------------------------------------------------------------------

test xyz@uu.net xyz@uu.net

The results should be xyz@uu.net. I have noticed this when I try to string commands together like in UNIX (Solaris) command line within my bsh files

Discussion

  • Pat Niemeyer
    Pat Niemeyer
    2000-08-13

    • priority: 5 --> 1
     
  • Pat Niemeyer
    Pat Niemeyer
    2000-08-13

    This is probably outside the scope of bsh (an environmental or java issue) but we should try to reproduce it to make sure we're not munging the quotation syntax.

     
  • Daniel Martin
    Daniel Martin
    2001-10-22

    Logged In: YES
    user_id=287867

    Starting multiple processes by separating commands with a
    vertical bar (|) and then connecting output to input is NOT
    something that happens automatically in unix -- it's a
    function of the shell that separates the commands.

    exec doesn't start an instance of /bin/sh; if that's what
    you want, then you can't use the builtin since the builtin
    uses java's one argument exec function which doesn't allow
    arguments containing spaces. (java's one argument exec
    uses a default StringTokenizer()) What you can do is write
    your own function that does what you want:

    void mySystem(String shellCommand) {
    Process p = Runtime.getRuntime().exec(new String []
    {"/bin/sh","-c",shellCommand});
    InputStream i = p.getInputStream();
    StringWriter buf = new StringWriter();
    while ((int j = i.read()) >= 0)
    buf.write(j);
    print(buf.toString());
    buf.close();
    i.close();
    }

    Note that this is highly unix-centric; you might be able to
    do some of the above on Windows NT machines with "cmd.exe"
    and "/c" instead of "/bin/sh" and "-c", but no guarantees.
    (And it's certainly pointless in all-java environments)

    I would suggest that the builtin exec() be enhanced to
    allow a String [] argument form in addition to the current
    form. Then, the above function could be replaced with:
    exec(new String [] {"/bin/sh","-c",shellCommand});

    This new form would work just like the current exec, except
    that it would call Runtime.exec(String []) instead of
    Runtime.exec(String).

     
  • Pat Niemeyer
    Pat Niemeyer
    2001-10-22

    • status: open --> closed