';' input character misinterpreted

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; } 
+5
source share
1 answer

When executing your program, for example:

 your_program_name sjhjh;ls 

on the command line, you are actually invoking two programs. The first is your_program_name sjhjh (so argv[1] is "sjhjh" ), and the second is ls . You need to make sure that the rest of the command line will not be checked by the shell, and this is done by correctly quoting:

 your_program_name 'sjhjh;ls' 
+14
source

Source: https://habr.com/ru/post/1262787/


All Articles