开发者

Linux Bash Input

Have a question about linux bash. I want to start a program and then send input开发者_StackOverflow中文版 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.


./chat << EOF
this is the input to chat
EOF


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


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


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


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 $#
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜