1

I have a script which prompts user to select options like 'y' or 'n'. If 'y' is selected, the script proceeds with further execution and if 'n' is selected then it stops.

I want the output of this file to be re-directed to a log file. so used below command:-

./script stop >> script_RUN.log 2>&1

The problem is, the script starts running but does not prompt to ask for options like 'y' or 'n' It is writing this to script_RUN.log.

How can I make the script to prompt user for options and re-direct the further execution to script_RUN.log?

Bharti
  • 11
  • 1
  • 2

2 Answers2

2

you can try using tee command instead.

./script stop | tee script_RUN.log

NOTE:

Only the output of the program will be saved.

EDIT:

if you don't want to see the output on the console at all just redirect it into /dev/null

for example:

 ./script stop | tee script_RUN.log > /dev/null

the above line will write the file into log but dost NOT printout on console

nafas
  • 5,283
  • 3
  • 29
  • 57
  • Though the script accepted the options, I could see the output getting printed on console. – Bharti Apr 14 '15 at 09:11
  • @Bharti hmm, its my personal favourite for that specific reason(it prints out stuff on console while writing into file) :). however, if you don't want to see the output on the console, look at my edited answer – nafas Apr 14 '15 at 09:49
  • This will redirect the questions as well. You need a little shell that allows the operator to enter command `yes and put the rest in the logfile`. – Walter A Apr 14 '15 at 10:52
  • @WalterA I've already mentioned that only output of the program will be printed into the file. – nafas Apr 14 '15 at 11:09
  • You did. And Bharti asked "How can I make the script to prompt user for options". His comment (I think) is about the output after the last prompting. I liked your unedited answer better than with the edits. – Walter A Apr 14 '15 at 11:23
  • @WalterA I c what you mean mate, but he wanted a way to not see stuff on the console, I also personally prefer to use tee without redirecting it. – nafas Apr 14 '15 at 11:33
  • what is the `stop` for ? – arvindh Aug 02 '17 at 16:17
  • @arvindh its part of the OP. – nafas Aug 02 '17 at 16:25
  • @nafas, you mean part of the output. when i give `stop`, the command terminates in between. so i tried without `stop`, and it worked. – arvindh Aug 02 '17 at 16:33
  • @arvindh if you look at the OP, the script requires a parameter in this case this param is `stop`. if your script doesn't take any parameter then you won't need it. for example `-l` is the param for command `ls -l`. how that makes sense. – nafas Aug 03 '17 at 09:48
1

This works like it has to really. You are redirecting stdout and stderr output from the very start. Instead you should try to redirect it in the script after the prompt. I think this would be helpful for you:

redirect COPY of stdout to log file from within bash script itself

Community
  • 1
  • 1
Ostap Khl
  • 183
  • 1
  • 2
  • 9
  • yes correct. but the problem is I do not have code (script.sh) with me to edit it. I only know the functionality of it. I can only invoke it – Bharti Apr 14 '15 at 09:12