0

So I'm trying to execute a command in a bash script multiple times with different but same number of arguments while redirecting the output to a text file. For example ...

./test arg1 arg2 arg3 >> output.txt

This works if I do it at the command line. In a script where I have...

command ./test 10 5 option1 >> output.txt


command ./test 5 10 option2 >> output.txt

And run the script, I get command not found. If I remove the redirect to ouput.txt, the script then works / prints to the terminal window

I tried searching online but I could not find examples where arguments were used which then redirected

Anyone able to help?

10
  • Why do you have command ./test in the script and ./test otherwise? Please, post the actual lines that you tried. – muru Apr 21 '16 at 12:43
  • I thought you needed command as a prefix for a bash script. I got it from some website. As for the actual text, it's a class project I'm testing. The actual program / arguments won't matter – cpd1 Apr 21 '16 at 12:45
  • You need command only if the command is a builtin you want to override (like using a different echo or kill). Well, how can you decide whether the actual program or arguments matter, when clearly you have something that should work (with or without command), and it doesn't? Maybe one of your script's arguments is a special character. Maybe you ran the script in a different directory. Leave the decision making to us. – muru Apr 21 '16 at 12:48
  • Ok I made the change. It's two ints and a string – cpd1 Apr 21 '16 at 12:52
  • 2
    SImple solution make the first redirect a direct write > and not an append >>. your simply trying to write into a file which dooes not exist. Or add the line touch output.txtabove all others. There is a nice little tool which can really help you debugging scripts, its called shellchecksimply google for it. – Videonauth Apr 21 '16 at 12:57
0

The issue had to do with writing the script in Windows in Sublime Text and then transferring it to a Linux server. Switching the mode in Sublime Text made the difference

Videonauth also pointed out that I should have just one redirect symbol ">" since for the first line since it is a new file

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.