I wrote a program to split an input string using ';' as the terminator and print the part of the string that is after ';'. The program shows correct output whenever substring following ';' in the input string is not a valid terminal command but also prints command not found
. On the other hand, it does not prints anything when the substring followed by ';' is a valid terminal command and executes the substring as a command , e.g. in case input "sjhjh;ls" it will execute ls command.
How do I get rid of the command not found
part?
Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char * input;
char * str;
char * word;
char terminator = ';';
if (argc < 2) {
fprintf(stderr,"ERROR, no string provided\n");
exit(1);
}
input = argv[1];
word = strchr(input, terminator);
if (word != NULL) printf("%s\n", word);
return 0;
}
argv
in main and the question could have a completely different focus.breakpoint
s anywhere in your code and, as you debug, the program will stop at the breakpoint. When it does, you can look at the call stack and the value of your variables (you can also change their values), and more. If you do not use an IDE, you give yourself an unnecessary handicap. If you do, you won't need to ask so many questions here ;-)