1

I have an unix shell script. I have put -x in shell to see all the execution step. Now I want to capture these in one log file on a daily basis. Psb script.

#!/bin/ksh -x   
Logfile= path.log.date    
Print " copying file" | tee $logifle     
Scp  -i key source destination | tee -a $logfile.    
Exit 0;
2
  • Logfile= path.log.date is a bug. You cannot have spaces in an assignment in shell. It should be Logfile=path.log.date. This isn't your actual script is it? (You have some case sensitivity issues too) Commented Feb 15, 2018 at 14:48
  • Ok thanks rest all look goods? Commented Feb 15, 2018 at 14:49

3 Answers 3

1

First line of the shell script is known as shebang , which indicates what interpreter has to be execute the below script.

Similarly first line is commented which denotes coming lines not related to that interpreted session.

To capture the output, run the script redirect your output while running the script.

ksh -x scriptname >> output_file 

Note:it will output what your script's doing line by line

Sign up to request clarification or add additional context in comments.

1 Comment

#!bin/ksh -x >> $ Logfile Logfile= path.log.date Print " copying file" | tee -a $logifle Scp -i key source destination | tee -a $logfile. Exit 0; Is the above is right? Or do I need to remove tee command
0

There are two cases, using ksh as your shell, then you need to do IO redirection accordingly, and using some other shell and executing a .ksh script, then IO redirection could be done based on that shell. Following method should work for most of the shells.

$ cat somescript.ksh
#!/bin/ksh -x

printf "Copy file \n";
printf "Do something else \n";

Run it:

$ ./somescript.ksh 1>some.log 2>&1

some.log will contain,

+ printf 'Copy file \n'
Copy file 
+ printf 'Do something else \n'
Do something else 

In your case, no need to specify logfile and/or tee. Script would look something like this,

#!/bin/ksh -x      
printf "copying file\n"    
scp -i key user@server /path/to/file   
exit 0

Run it:

$ ./myscript 1>/path/to/logfile 2>&1

2>&1 captures both stderr and stdout into stdout and 1>logfile prints it out into logfile.

3 Comments

Thank you..Actually I want to creat a shell script and us line. ed .sh in control-m command How can I add 1>some.log 2&>1 after.sh in controlm command line.Pls help.thanks
Thank you. How can I use this stderr and stout in control-m command line. Because I want to use in control-m job
I don't know anything about control-m-job. You can ask that as a separate question. The command line in itself should be standalone, if you are using ksh or any shell. and see: stackoverflow.com/help/someone-answers
0

I would prefer to explicitly redirecting the output (including stderr 2> because set -x sends output to stderr).

This keeps the shebang short and you don't have to cram the redirecton and filename-building into it.

#!/bin/ksh
logfile=path.log.date
exec >> $logfile 2>&1  # redirecting all output to logfile (appending)
set -x               # switch on debugging
# now start working
echo "print something"

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.