-2

I have a script called head.sh, it looks like this:

#!/bin/bash
{
    read line1
    read line2
    read line3
}
echo $line1
echo $line2
echo $line3

And I need to call it like this:

sh head.sh < rhymes.txt

when I hardcode the filename in it works, but I can't work out how to pass in the file.

closed as off-topic by Kusalananda, Anthony Geoghegan, Anthon, Stephen Rauch, G-Man Sep 30 '17 at 18:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Kusalananda, Anthony Geoghegan, Stephen Rauch
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    Are you reinventing utility "head"? Have you tried 'head -n 3' instead? – Jaroslav Kucera Sep 30 '17 at 7:56
  • 2
    There was wrong direction >, now you changed to < and it must work now. – αғsнιη Sep 30 '17 at 8:33
  • 1
    You've written what claims to be a bash shell script (see its first line) but you're running it with sh. On some systems this may be a completely different shell. Use bash head.sh < rhymes.txt to run it. – roaima Sep 30 '17 at 8:54
  • 2
    You should also quote your variables when you use them. (Not always essential but good practice. And avoids surprises.) For example echo "$line1" – roaima Sep 30 '17 at 8:55
  • 1
    You say “this is a very simple question.”   You’re wrong; it’s not a question at all.   What are you asking? – G-Man Sep 30 '17 at 18:16
0

I've tried your script like this and it works:

cat some_file | sh ./head.sh > rhymes.txt

Or you're not allowed to use pipe in your excercise?

EDIT: OK, however still, your script works for me:

$ cat test
11111111
22222222
33333333
4444
555
66
7
$ sh ./head.sh < test
11111111
22222222
33333333
$ cat ./head.sh 
#!/bin/bash
{
    read line1
    read line2
    read line3
}
echo $line1
echo $line2
echo $line3
  • Thanks for getting back. The file rhymes.txt contains the text which I want to pass through head.sh! – NoneOYerBeez Sep 30 '17 at 8:22
  • Edited above to reflect this – NoneOYerBeez Sep 30 '17 at 8:22
  • I've updated the post according to your comment. – Jaroslav Kucera Sep 30 '17 at 8:29

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