I wrote a program to split the input string using ';' as a terminator and print the part of the line that is after the ';'. The program displays the correct output when the substring is next ';' in the input line is not a valid terminal command, but also prints command not found . On the other hand, it does not print anything when the substring followed by ';' is a valid terminal command and executes a substring in the form of a command, for example. if "sjhjh; ls" is entered, it will execute the ls command.
How do I get rid of the command not found ? 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; }
source share