-3

Sorry this is a very easy question, how do I print 0 from a shell script? My program is doing scanf for a number, I want the script to enter 0.

I tried echo 0, it doesnt seem to work.

#!/bin/bash
make clean
ls -l
cat udpserver.c
make
ls -l
./udpserver
sleep 2
echo 0 | udpserver
sleep 2
echo 0 | udpserver
4
  • 1
    Possible duplicate of Send string to stdin Commented May 4, 2017 at 1:21
  • @BenjaminW. See his edited question, it's not as simple as the duplicate. Commented May 4, 2017 at 1:35
  • Don'Ät spam tags. This is not related to C. Commented May 4, 2017 at 1:37
  • @Barmar Now it's different... Commented May 4, 2017 at 1:44

1 Answer 1

3

You can't use a pipe to send input to a program that you started previously, you have to use it when starting the program. If you want to pipe multiple commands to it, put them in a subshell.

(sleep 2; echo 0; sleep 2; echo 0) | ./udpserver
Sign up to request clarification or add additional context in comments.

7 Comments

Or ( echo line1 ; echo line2 ) | yourProg or printf "line1\nline2\n" | yourProg - the possibilities are almost endless :-)
@paxdiablo I didn't mention that because I said that using a here-doc is best for multiple lines.
I dont get it, where am I going wrong here? Heres my script:
@sublimesummer Don't put code in comments. Edit your question to show what you tried.
Barmar, wasn't criticising your answer (in fact I voted for it), but there are situations where a heredoc doesn't cut it. Specifically, if the input has to be more complex, such as ( expr $bytes / 1024 ) | processKilobytes. The OPs question is nowhere near that level of complexity as you rightly point out.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.