0

I have a shell script as follows

ln -s -f /ctf/message/dave.inbox /home/alice/imoprt.inbox
/ctf/message/sendmsg
echo hello

The purpose of the script is to first make a link, then run a program, and provide hello as input to the program. It all works except providing hello as the input. I can do this easily by writing a script just like

echo hello

but then I am unable to do some other things, such as linking before. Is there a way to do this sequence of commands?

9
  • 1
    Are you trying to do echo hello | /ctf/message/sendmsg? Commented Sep 21, 2020 at 22:32
  • What if I want to do a series of input? Like multiple inputs to the program one after the other Commented Sep 21, 2020 at 22:34
  • feed it all the input you want. Perhaps you'll want to use a heredoc to give multi-line input, but you can also do printf 'line1\nline2\n' | sendmsg. It depends on what you want to do. Commented Sep 21, 2020 at 22:36
  • Or, you may want to create a fifo and run sendmsg asynchronously and then let the script continue, feeding data to the program through the fifo. Commented Sep 21, 2020 at 22:37
  • @Ben - I think more details of just what you want to do are needed. Based on your comment, it sounds like you need some form of loop. Commented Sep 21, 2020 at 22:42

1 Answer 1

2

There are a few common ways to provide input to a command:

echo input | cmd
{ echo one; echo two; echo three; } | cmd
cmd <<EOF
line 1 of input 
line 2 of input
EOF
trap 'rm -f fifo' 0
mkfifo fifo
cmd < fifo &
{
echo line 1 of input
var=$( echo line 2 of input generated dynamically )
echo "$var"
echo another line of input
} > fifo
wait
Sign up to request clarification or add additional context in comments.

2 Comments

Great for the use of mkfifo, but the heredoc version could be simpler written without a pipe, using just cmd <<EOF.
@user1934428 Thank you. It is amazing! Even after decades of railing against UUOC how easy it is to fall into it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.