Results 1 to 5 of 5

Thread: running shell command from C++ code problem

  1. #1
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default running shell command from C++ code problem

    Hi,

    My program needs to get a list of users on the system. From linux shell it works fine:
    # who | cut -d' ' -f1 | sort -u

    When I start this from code I get an error with exit code 255. I've read that this means that some libraries can't be found. What libraries are necessary and how and where to declare them for C++ program? Are these libraries for who, cut and sort commands?

    Qt Code:
    1. QProcess exec;
    2. exec.start( "who|cut -d' ' -f1|sort -u" );
    3. exec.waitForFinished();
    4. int exitcode = exec.exitCode(); // exitcode is always 255!
    To copy to clipboard, switch view to plain text mode 

    Thanks!!

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: running shell command from C++ code problem

    You need a shell to interpret the command. See for example this post.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    alex chpenst (30th July 2008)

  4. #3
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: running shell command from C++ code problem

    This works fine, thanks a million!!
    Qt Code:
    1. QStringList arguments;
    2. arguments << "-c" << "who|cut -d' ' -f1|sort -u";
    3. QProcess exec;
    4. exec.start("/bin/sh", arguments);
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: running shell command from C++ code problem

    How could we adjust this code to windows command?
    should we replace /bin/sh with win32 or something?

    thanks
    SunnySan

  6. #5
    Join Date
    Apr 2007
    Location
    Ilsfeld, Germany
    Posts
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: running shell command from C++ code problem

    Hi,

    use cmd.exe with /c

    Qt Code:
    1. QStringList arguments;
    2. arguments << "/c dir /b e:\\test\\*.txt | sort";
    3. QProcess exec;
    4. exec.start("cmd.exe", arguments);
    5. exec.waitForFinished();
    6. qDebug() << exec.readAllStandardOutput();
    To copy to clipboard, switch view to plain text mode 
    HTH, Bernd

Similar Threads

  1. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.