2

Take a look at the script first:

touch test.txt
touch loc

echo "result1" > loc
loc >> test.txt

echo "result2" > loc
loc >> test.txt

rm loc

I create to files, one of them loc is temporary and I try to write to it, but get the following error message:

5: script.sh: loc: not found
8: script.sh: loc: not found

How can I get it to work?

  • 4
    loc is not a command! Use cat loc at line 5 and 8. – coffeMug Nov 1 '16 at 13:50
-2

Redirection operator expects a string or stream of characters as input side and a file on the other side. So you need to use cat command to first read the file and then redirect the stream characters to the output file.

touch test.txt
touch loc
echo "result1" > loc
cat loc >> test.txt
echo "result2" > loc
cat loc >> test.txt
rm loc
  • 12
    more is a pager, intended for interactive use. Instead you should use cat for this kind of action. – roaima Nov 1 '16 at 14:12
  • Sorry, doesn't know that. I am changing more by cat.. – trishnendu Nov 2 '16 at 10:03
9

loc is not a command. You would need to use cat loc >> test.txt to append it to text.txt. Otherwise you can also avoid the temp file completely:

echo "result1" >> test.txt
echo "result2" >> test.txt

Note, touch test.txt seems also useless.

-1

this will create the test.txt

echo "result1" |  tee -a  test.txt
echo "result2" |  tee -a  test.txt

OR use this in 1-step

echo "result1" && echo "result2" |  tee -a  test.txt
  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Nov 2 '16 at 10:38

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.