0

I have this script myprogram

#!/bin/bash
echo This is the first one ${1}.
echo This is the second one ${2}.

and the input file test.txt

Hi
Hello

and I hope to use input redirection to run the script with input from test.txt, which should output

This is the first one Hi.
This is the second one Hello.

I'm trying to use

./myprogram < test.txt

but it's not working. The only thing it printed out is

This is the first one
This is the second one

Can anyone help me with it?

1
  • 1
    Your script doesn't ever try to read from standard input, so what you redirect on stdin doesn't have any effect. Commented Sep 23, 2016 at 0:24

1 Answer 1

4

Positional parameters (aka command line arguments) are not related to stdin. Here's an example that uses both:

$ cat myscript
#!/bin/bash
echo "These are the first two arguments: $1 and $2"
read -r first
echo "This is the first input line on stdin: $first"
read -r second
echo "This is the second input line on stdin: $second"

$ ./myscript foo bar < test.txt
These are the first two arguments: foo and bar
This is the first input line on stdin: Hi
This is the second input line on stdin: Hello
Sign up to request clarification or add additional context in comments.

Comments

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.