LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Redirect ouput to the same script file with date (https://www.linuxquestions.org/questions/linux-newbie-8/redirect-ouput-to-the-same-script-file-with-date-4175498831/)

mierdatuti 03-20-2014 03:57 AM

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
Shows me:
Code:

_20140320_095532.log
And I would like:
xxxxxx__20140320_095532.log
Could you help me please?
I'm using ksh.
Thanks

colucix 03-20-2014 04:06 AM

The name of the script is $0, not $1. Even better
Code:

$(readlink -f "$0")
but take in mind that this works inside the script itself. If you use $0 directly in the command line, it will be substituted by the name of the shell (ksh) not the script.

A way to tell the script to redirect all the output to a file could be
Code:

#!/bin/ksh
#
exec > /arq/log/"$(basename "$(readlink -f "${0/.sh/}")")"_$(date +%Y%m%d_%H%M%S).log


mierdatuti 03-20-2014 04:20 AM

Quote:

Originally Posted by colucix (Post 5137924)
The name of the script is $0, not $1. Even better
Code:

$(readlink -f "$0")
but take in mind that this works inside the script itself. If you use $0 directly in the command line, it will be substituted by the name of the shell (ksh) not the script.

A way to tell the script to redirect all the output to a file could be
Code:

#!/bin/ksh
#
exec > /arq/log/"$(basename "$(readlink -f "$0")")"_$(date +%Y%m%d_%H%M%S).log


Thanks!
if I execute:
Code:

/arq/uti/xxxxx.sh  >  /arq/log/"$(basename "$(readlink -f "$0")"_$(date +%Y%m%d_%H%M%S).log
dont ends and it shows me an >

colucix 03-20-2014 04:47 AM

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
#
exec > /arq/log/"$(basename "$(readlink -f "${0/.sh/}")")"_$(date +%Y%m%d_%H%M%S).log

and then you can run it simply as
Code:

/arq/uti/xxxxx.sh

mierdatuti 03-20-2014 04:59 AM

Quote:

Originally Posted by colucix (Post 5137940)
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
#
exec > /arq/log/"$(basename "$(readlink -f "${0/.sh/}")")"_$(date +%Y%m%d_%H%M%S).log

and then you can run it simply as
Code:

/arq/uti/xxxxx.sh

Thanks but I have too much scripts and I won't edit them.
There is no alternative?
Thanks, sorry for my english!

descendant_command 03-20-2014 05:42 AM

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

colucix 03-20-2014 06:01 AM

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
this will set the first argument of the current shell as the name of the script you're going to run.

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' {} \;
This will add the exec statement as the very first line of each .sh script under /arq. If your script have a sha-bang (that is #!/bin/ksh) as the first line, you may want to insert the text at the second line and in that case change 1 with 2 in the sed command.

Please note the option
Code:

-i.bck
it is a way to force sed to save a backup copy of the original file, in case something goes wrong. You will find those .sh.bck files that you can easily remove with a find command.

mierdatuti 03-20-2014 06:16 AM

Quote:

Originally Posted by descendant_command (Post 5137962)
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

When I do:
Code:

i="/arq/uti/xxxxx.sh";  $i > /arq/log/"$(basename -s .sh $i)"_$(date +%Y%m%d_%H%M%S).log
basename: invalid option -- s
Try `basename --help' for more information.

---------- Post added 03-20-14 at 06:17 AM ----------

Quote:

Originally Posted by colucix (Post 5137973)
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
this will set the first argument of the current shell as the name of the script you're going to run.

Secondary prompt with these command :-(

descendant_command 03-20-2014 06:23 AM

Quote:

basename: invalid option -- s
Try `basename --help' for more information.
Works here
Code:

basename --version
basename (GNU coreutils) 8.21


colucix 03-20-2014 06:30 AM

Quote:

Originally Posted by mierdatuti (Post 5137978)
Secondary prompt with these command :-(

My fault, this time. Corrected version:
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.