LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-20-2014, 04:57 AM   #1
mierdatuti
Member
 
Registered: Aug 2008
Posts: 53

Rep: Reputation: 15
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
 
Old 03-20-2014, 05:06 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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

Last edited by colucix; 03-20-2014 at 05:18 AM.
 
Old 03-20-2014, 05:20 AM   #3
mierdatuti
Member
 
Registered: Aug 2008
Posts: 53

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
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 >
 
Old 03-20-2014, 05:47 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
 
Old 03-20-2014, 05:59 AM   #5
mierdatuti
Member
 
Registered: Aug 2008
Posts: 53

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
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!
 
Old 03-20-2014, 06:42 AM   #6
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,385

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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

Last edited by descendant_command; 03-20-2014 at 06:56 AM.
 
Old 03-20-2014, 07:01 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 03-20-2014, 07:16 AM   #8
mierdatuti
Member
 
Registered: Aug 2008
Posts: 53

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by descendant_command View Post
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 View Post
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 :-(
 
Old 03-20-2014, 07:23 AM   #9
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,385

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
basename: invalid option -- s
Try `basename --help' for more information.
Works here
Code:
basename --version
basename (GNU coreutils) 8.21
 
Old 03-20-2014, 07:30 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by mierdatuti View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Execute a script and not able to redirect to a file shisha Linux - Newbie 15 09-01-2012 12:15 AM
trying to run a script in the background redirect the output to a file jdwalk Linux - Newbie 5 02-12-2010 08:50 AM
Create a script that asks the user for a file and then appends the date to the file larry5757 Linux - Newbie 2 12-04-2009 07:33 AM
Unable to redirect script output to a file alien12 Linux - Newbie 4 08-25-2009 04:36 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 10:59 AM


All times are GMT -5. The time now is 09:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration