Find the answer to your Linux question:
Results 1 to 6 of 6
I want to include some error checking in my code for the system command system("ls -z"); didn't not print any error in the terminal. system("ls") gave an exit status of ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    5

    Exit status of system command??

    I want to include some error checking in my code for the system command

    system("ls -z"); didn't not print any error in the terminal.

    system("ls") gave an exit status of 1 in my system,does this value vary on different systems??

    please help.

  2. #2
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,789
    i believe ANY system command exist with status of 0 if it fails, and any other number if it succeeds. So if you wanted an operator to check it failed, check the status is 0, if it is, it failed, otherwise it succeeded.
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  3. #3
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    system() returns -1 on error and returns the status code of the shell command otherwise. It is standard practice to exit anything with a status code of 0 meaning success. Anything else is an error.

  4. #4
    Just Joined!
    Join Date
    Dec 2003
    Posts
    41
    As was pointed out above, a return of zero means success and any non zero means failure. You can check this by running some commads (then some incorrent commands) and check the return code after each command by using: echo $?

  5. #5
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    As far as completely exiting a command or a main function, this is standard practice. However a function can really return any value, int pointer char whatever. The system() just so happens to work like I just described, which is pretty logical behavior.

  6. #6
    Just Joined!
    Join Date
    Apr 2007
    Posts
    5
    i tried the below and it worked.
    system(cmd) ==0 || die(msg)

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...