Problem:
I need to send a command to a named pipe and need to ensure only the command is sent and not any errant or "fat-fingered" characters.
Example:
I have been using the following:
echo "command" > /path/to/namedPipe
This works fine, but I noticed something odd, that it is possible to send errant keystrokes to the pipe. For example:
echo "command" > /path/to/namedPipe straycharacters
then both <command>
and <straycharacters>
are written to namedPipe. I would not have guessed this would happen, but in some cases it can really mess up my program. I also noticed echo does not seem to require quotes. My command does not require quotes, just the actual <command>
. What would be the best practice to write only <command>
to the pipe?
Possible Solution:
printf "command" > /path/to/namedPipe straycharacters
In my terminal this only prints out <command>
and not <straycharacters>
, and so far it appears to be compatible with my program. However it is a little different than echo because it seems to not include a new line.
printf
is a format string. If it contained%s
,%d
, etc., the remaining arguments will be printed accordingly. How do you guarantee you won't fat finger the first argument? – muru Jun 13 '19 at 04:29