The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script in saved in compress format naveeng.81 Shell Programming and Scripting 1 04-14-2008 08:31 AM
mailx saved messages are unreadable (base64) Squeakygoose UNIX for Dummies Questions & Answers 1 01-30-2008 11:49 AM
Sudo file not saved Asteroid UNIX for Advanced & Expert Users 0 07-30-2007 07:18 AM
only root's crontab gets not saved flok UNIX for Advanced & Expert Users 1 06-25-2007 10:50 AM
Where are the password saved in a UNIX server? hast5 UNIX for Dummies Questions & Answers 12 10-19-2006 04:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-13-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by otheus View Post
Actually, Gee-Money is on the ball. If your script is in a particular directory, you don't need find. Just ls. However, his script is unnecessarily complex:
Code:
ls -1t |sed -n '/name.*\.log$/{p;q}'
would do and be a bit quicker. (Normally, ls prints out in columns UNLESS it's printing out to a pipe. The -1 here just emphasizes that.)

No, ls only prints in columns if the output is going to a terminal. Anywhere else (e.g., pipe or redirection), it prints one file per line.
  #2 (permalink)  
Old 10-13-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by gugs View Post
Our system produce logs when a script is run which may not be daily, the logs have a format: name_YYMMDD.log - both name and .log are consistent, date changes as per the day the script is run.

Is there a way of finding the last saved log?

Since the shell will sort the files automatically, there is no need for any external command:

Code:
set -- name_*.log
shift $(( $# - 1 ))
lastlog=$1
  #3 (permalink)  
Old 10-13-2008
treesloth treesloth is offline
Registered User
  
 

Join Date: Oct 2008
Location: Orem, Utah
Posts: 73
Another cheap and easy way:

Code:
ls -t | grep log$ | head -n 1
I might even use that in a script...
  #4 (permalink)  
Old 10-14-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

There's nothing cheap about using three external commands when you don't need any.
  #5 (permalink)  
Old 10-14-2008
treesloth treesloth is offline
Registered User
  
 

Join Date: Oct 2008
Location: Orem, Utah
Posts: 73
Quote:
Originally Posted by cfajohnson View Post

There's nothing cheap about using three external commands when you don't need any.
Considering the extremely lightweight task being performed, even three external commands are, in fact, extremely cheap with system resources. Also, the three commands in question are among those that many users have in their UNIX toolbox quite readily; hence the "easy". There's really no advantage to declining to use external commands.
  #6 (permalink)  
Old 10-14-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by treesloth View Post
Considering the extremely lightweight task being performed, even three external commands are, in fact, extremely cheap with system resources.

Three external commands are always much more expensive in terms of time and systems resources than none.
Quote:
Also, the three commands in question are among those that many users have in their UNIX toolbox quite readily; hence the "easy". There's really no advantage to declining to use external commands.

External commands use many times more resources and time than shell builtin functions and syntax.

If the tasks are lightweight, that's all the more reason for not using heavy external commands.
  #7 (permalink)  
Old 10-14-2008
treesloth treesloth is offline
Registered User
  
 

Join Date: Oct 2008
Location: Orem, Utah
Posts: 73
Quote:
Originally Posted by cfajohnson View Post

Three external commands are always much more expensive in terms of time and systems resources than none....
Sure, and if the original task were anything even vaguely approaching a heavy task, it would make a difference. The original task was extremely lightweight. Many times virtually nil is, in this case, still very much virtually nil. Yes, there is merit in knowing the internal methods; in this particular case, the difference is utterly irrelevant. My goal is add to the body of methods offered, not to claim that mine is the only one, or even the best for all circumstances.

Last edited by treesloth; 10-14-2008 at 03:52 AM..
Closed Thread

Bookmarks

Tags
unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0