How to distinguish between "command not found" and "command with no result"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to distinguish between "command not found" and "command with no result"
# 1  
Old 08-28-2009
How to distinguish between "command not found" and "command with no result"

system() call imeplemented in solaris is such a way that:
Command not found - return code 1
Command executed successfully without Output - return code 1
how to distinguish between these two based on return code in a c - file?
Can you help on this ?
# 2  
Old 08-28-2009
System isn't supposed to behave that way. Can you post some code you are using exhibiting the issue ?
# 3  
Old 08-28-2009
A simple c file:

Code:
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
int main()
{
int exitcode;
char *buf="/var/tmp/bin/em_validation";
char *buf1="grep madhu x";
char *buf2="grep ding x";
char *buf3="ls";
char *buf4="ls > output";

exitcode=system((const char*)buf);
if ((exitcode != -1) && WIFEXITED(exitcode))
     exitcode = WEXITSTATUS(exitcode);
printf("\ncommand not found = %d\n",exitcode);

exitcode=system((const char*)buf1);
if ((exitcode != -1) && WIFEXITED(exitcode))
     exitcode = WEXITSTATUS(exitcode);
printf("\ngrep no output = %d\n",exitcode);

exitcode=system((const char*)buf2);
if ((exitcode != -1) && WIFEXITED(exitcode))
     exitcode = WEXITSTATUS(exitcode);
printf("\ngrep with output = %d\n",exitcode);

exitcode=system((const char*)buf3);
if ((exitcode != -1) && WIFEXITED(exitcode))
     exitcode = WEXITSTATUS(exitcode);
printf("\nsuccessful command ls  = %d\n",exitcode);

exitcode=system((const char*)buf4);
if ((exitcode != -1) && WIFEXITED(exitcode))
     exitcode = WEXITSTATUS(exitcode);
printf("\nsuccessful command with no output ls > outputfile  = %d\n",exitcode);



return exitcode;
}

file x contains:
ding
# 4  
Old 08-29-2009
I have 127 as return status for the not found command and 0 for the successful with no output one.
What are your results ? What OS are you running
# 5  
Old 08-31-2009
sati01> uname -a
SunOS sati01 5.10 Generic_137137-09 sun4u sparc SUNW,Sun-Fire-V240

On this I am getting Status =1 for command not found.
Getting the same result on 5.9 also.
# 6  
Old 08-31-2009
Interesting. It looks like system return value meaning has changed between Solaris 10 and OpenSolaris (which I use):

On Solaris 10 (SunOS 5.10) system(3C) manual page:

Code:
RETURN VALUES
     The system() function executes vfork(2) to  create  a  child
     process that in turn invokes one of the exec family of func-
     tions (see exec(2)) on  the  shell  to  execute  string.  If
     vfork()  or the exec function fails, system() returns -1 and
     sets errno to indicate the error.

On OpenSolaris (SunOS 5.11):

Code:
RETURN VALUES
     The system() function executes posix_spawn(3C) to  create  a
     child  process  running  the shell that in turn executes the
     commands in string. If posix_spawn() fails, system() returns
     -1  and sets errno to indicate the error; otherwise the exit
     status of the shell is returned.

I'm afraid you'll have to use a different approach to distinguish these cases.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX and Linux Applications

Problem on SQLplus command ""bash: sqlplus: command not found""

Hi all, i face an error related to my server ""it's running server"" when i use sqlplus command $ sqlplus bash: sqlplus: command not found the data base is up and running i just need to access the sqlplus to import the dump file as a daily backup. i already check the directory... (4 Replies)
Discussion started by: clerck
4 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

7. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies
Login or Register to Ask a Question