-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
too honest for this site
  • 12,050
  • 4
  • 30
  • 52

1 Answers1

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Or `( echo line1 ; echo line2 ) | yourProg` or `printf "line1\nline2\n" | yourProg` - the possibilities are almost endless :-) – paxdiablo May 04 '17 at 01:17
  • @paxdiablo I didn't mention that because I said that using a here-doc is best for multiple lines. – Barmar May 04 '17 at 01:18
  • I dont get it, where am I going wrong here? Heres my script: – sublimesummer May 04 '17 at 01:22
  • @sublimesummer Don't put code in comments. Edit your question to show what you tried. – Barmar May 04 '17 at 01:22
  • 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. – paxdiablo May 04 '17 at 01:25
  • @paxdiablo Yeah, I didn't want to go into all the details of providing computed input for this question. – Barmar May 04 '17 at 01:26
  • @paxdiablo Now that he's edited the question, he does need more complicated answer. – Barmar May 04 '17 at 01:27