Redirect ouput to the same script file with date
Hi,
I'm trying to redirect output of a script file to the same file with date and log extension but I cant. I try with : Code:
/arq/uti/xxxxxx.sh > /arq/log/"$(basename "$1")"_`date +%Y%m%d_%H%M%S`.log Code:
_20140320_095532.log xxxxxx__20140320_095532.log Could you help me please? I'm using ksh. Thanks |
The name of the script is $0, not $1. Even better
Code:
$(readlink -f "$0") A way to tell the script to redirect all the output to a file could be Code:
#!/bin/ksh |
Quote:
if I execute: Code:
/arq/uti/xxxxx.sh > /arq/log/"$(basename "$(readlink -f "$0")"_$(date +%Y%m%d_%H%M%S).log |
Well, you missed a closing parenthesis and a closing double quotes, therefore it shows the secondary prompt waiting for further input. Anyway, to be more clear that command line doesn't work as you expect, since $0 will be substituted by the name of the running shell, which is ksh and not from the name of the script.
To be substituted by the name of the script the value $0 must be inside the script itself. This the reason why I suggested the exec command as the first statement of your code. It will redirect all the output from subsequent statements to the file specified. In other word your ksh script should begin with: Code:
#!/bin/ksh Code:
/arq/uti/xxxxx.sh |
Quote:
There is no alternative? Thanks, sorry for my english! |
What about (for lots)
Code:
for i in *.sh; do $i > /arq/log/"$(basename -s .sh $i)"_$(date +%Y%m%d_%H%M%S).log; done or singly Code:
i=script.sh; $i > /arq/log/"$(basename -s .sh $i)"_$(date +%Y%m%d_%H%M%S).log |
Yes. You have two alternatives:
1) Run the script as Code:
set -- /arq/uti/xxxxx.sh && /arq/uti/xxxxx.sh > /arq/log/"$(basename ${1/.sh/})_$(date +%Y%m%d_%H%M%S).log 2) Edit your scripts using sed and the -i option. For example, suppose you have all your scripts under /arq and they are suffixed with .sh Code:
find /arq -name \*.sh -exec sed -i.bck '1iexec > /arq/log/"$(basename "$(readlink -f "${0/.sh/}")")"_$(date +%Y%m%d_%H%M%S).log' {} \; Please note the option Code:
-i.bck |
Quote:
Code:
i="/arq/uti/xxxxx.sh"; $i > /arq/log/"$(basename -s .sh $i)"_$(date +%Y%m%d_%H%M%S).log Try `basename --help' for more information. ---------- Post added 03-20-14 at 06:17 AM ---------- Quote:
|
Quote:
Code:
basename --version |
Quote:
Code:
set -- /arq/uti/xxxxx.sh && /arq/uti/xxxxx.sh > /arq/log/"$(basename "${1/.sh/}")"_$(date +%Y%m%d_%H%M%S).log |
All times are GMT -5. The time now is 04:04 PM. |