0

Have a question about linux bash. I want to start a program and then send input to it. Normally in de terminal I do just, ./chat and then type something.

I dont know how it should be in bash, i tried this:

./chat hi

Really dont how to. Hope someone will have the solution.

6
  • This really depends too much on the program to be answered without more detail. If the program is expecting to receive data from standard input, then typing /chat and then typing your input should work just fine. If it is expecting arguments, then you'll feed it arguments as SpyrosP suggests. If it is expecting a file, then you'll have to give it a file. Commented Mar 14, 2011 at 17:10
  • Are you looking for echo something | ./chat perhaps? Commented Mar 14, 2011 at 17:14
  • the program is just a simple chat, I can chat through the terminal. the program is waiting for input. but how can i give this input with bash? Commented Mar 14, 2011 at 17:16
  • @erik i want to start the program, and then talk several of lines to the program. Commented Mar 14, 2011 at 17:17
  • @Tom: Then run the command that sends these lines (e.g. cat textfile) and pipe it to your program with | ./chat Commented Mar 14, 2011 at 17:19

5 Answers 5

1
./chat << EOF
this is the input to chat
EOF
Sign up to request clarification or add additional context in comments.

2 Comments

No. You can do: ./chat << EOF<newline> everthing between that line and a line with EOF by itself will be the input. If you want a single line, you can do : echo "Hello, welcome?" | ./chat
Since this is bash, there are also here-strings: ./chat <<< 'Hi, I'm a here-string'
1

what you are doing is right. make sure that the script is executable and it accepts command line parameters.

#! /bin/bash
echo Hi $1

./hi SO

o/p

Hi SO

EDIT :

create a new text file with the content that you desire and then ./chat < example.txt

4 Comments

wow, dont know if i understand. i can do "./chat welcom on the chat" ?
that totally depends on the script, if the script that you are executing accepts command arguments, then it works for sure ..
the program is not relying on startup arguments. when the program starts, its listening for input. i want the bash file to start the program, and then send a message "Hello, welcome!"
heres a very simple way .. create a new file text file with Hello, welcome as content and then ./chat < example.txt
1

If I understand you correctly - you want to FIRST get some fixed text in THEN you want to take input from the keyboard ...

IF thats all you want

cat  welcomeText.txt  - | ./chat

cat will concatenate your fixed text (welcomText.txt, a file) it will then read from standard input ("-")

That will be piped ("|") into chat

There are more advanced ways of doing this by creating another file descriptor and selectively write to chat from various sources

Comments

0

The input which you are going to type can be saved in some variable by using the following command:

read var

This would perform the work of scaning the whole input which you type after running the program and storing it in variable "var".

For eg:

Following code will read input and display the same:

read var 
echo $var

Comments

-1

This clarifies the bash commands about command line arguments :

#!/usr/bin/env bash

echo name of script is $0
echo first argument is $1
echo second argument is $2
echo seventeenth argument is $17
echo number of arguments is $#

2 Comments

I see, but how can I communicate with the program without using the arguments?
No, the seventeenth argument is ${17}. If you do $17, you get the first argument followed by a literal "7".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.