1
if [ who | grep $user | grep pts ]
then
              write $user
                   message here
                   EOF
fi

Here if I execute it, it is blocking at write and I have to enter a message manually and then press Ctrl+d.

Is there a way to send message and EOF through program?

4

You can pipeline your input from one program to another like this:

echo "message here"|write $user

or like this:

cat /tmp/message|write $user
1
  • 1
    Or even write $user < /tmp/message.
    – Jos
    Mar 20 '15 at 10:22
3

What you are probably looking for is the following style of writing it (equivalent to other methods)

write $user <<EOF
    message here
    as opposed to the
       echo "asd" | write $user
    method, using here-doc redirection transparently allows multiple lines
    and reads everything until seeing the delimiter
    so the sent message end here:
EOF

Note: you are not directly "sending an EOF" here, the shell just understands the word "EOF" as your chosen mark of ending the input and will end the input there - and write doesnt ask you to press ctrl+d in this case, because it is reading from the stuff the shell is piping to it.

Note 2: You may find useful knowing that pretty much every comand-line tool in the unix world allows for this kind of default shell tricks, as they are only relying on the incredibly universal concept of stdin - write, as many other tools, will read from what is piped to it - and only ask the user interactively for input if none such is present and a user input method would make sense.

For more info, there is a question asking for exactly this over here: https://stackoverflow.com/questions/2500436/how-does-cat-eof-work-in-bash

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.