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
Keeping your SSH connections alive with autossh iBot UNIX and Linux RSS News 0 05-16-2008 11:20 AM
deleting a line but keeping the same file laiko UNIX for Dummies Questions & Answers 2 05-13-2008 02:08 PM
house keeping script Rakesh Bhat UNIX for Dummies Questions & Answers 7 01-03-2006 01:51 AM
keeping history of command legato SUN Solaris 3 11-04-2004 08:41 PM
Keeping an eye on all user activities shauche UNIX for Advanced & Expert Users 5 05-19-2003 06:07 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 01-31-2002
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 500
Keeping the format ...

Hi all,

Am trying to execute a loop but having some troubles...
Files that will be query'd use the Julian date (eg: cpu032, cpu365) in their naming convention.

I'm a little lost how to maintain the three character format of the numeric portion of the file name while cycling backwards(or forwards for that matter) in a loop.

Something similar to the following:
Code:
xstart=`date '+%j'`
xend=`expr $xstart - 30`

while [ $xstart -gt $xend ] ; do

  echo $xstart
  xstart=`expr $xstart - 1`

done
If $xstart begins at 032 (1-Feb) and $xend becomes 002 (2-Jan) then I should see...
032
031
030
029
...
004
003
002

Unfortunately I'm not - and is only displaying the significant digits.
Is there any way around this??
  #2 (permalink)  
Old 01-31-2002
seismic_willy seismic_willy is offline
Registered User
  
 

Join Date: Jan 2002
Location: Houston
Posts: 11
Here's one that might help:

xstart=`date '+%j'`
xend=`expr $xstart - 30`
while [ $xstart -gt $xend ] ; do
if [ $xstart -lt 10 ]; then
echo 00"$xstart"
elif [ $xstart -lt 100 ]; then
echo 0"$xstart"
else
echo $xstart
fi
xstart=`expr $xstart - 1`
done

It's not the prettiest, but it does the trick.
  #3 (permalink)  
Old 01-31-2002
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 500
Thanks seismic_willy,

I've already use the same techique in another script for reporting stats, but thought that there may have been another way.

Again, thanks.
  #4 (permalink)  
Old 01-31-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
With ksh, just use "typeset -Z3 xstart" at the top of the script. Then ksh will maintain the leading zeros.
  #5 (permalink)  
Old 02-01-2002
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 500
Thanks Perderabo,

Is there a similar way to do the same in `bash` ?
  #6 (permalink)  
Old 02-01-2002
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Bash does not support that option to typeset.
Also, if you're using a Linux-based OS, you most likely have PDKSH installed, instead of ksh. Most versions of pdksh do not support that either - only the "real" ksh can be trusted...

Good news though! You can get David Korn's KSH 93 from here:
http://www.research.att.com/sw/download/

Using AT&T's package is a bit of a pain, but it's worth it.
  #7 (permalink)  
Old 02-01-2002
Jimbo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
If you cannot use typeset, you can format it with awk:

Code:
#!/bin/sh

xstart=`date '+%j'`
xend=`expr $xstart - 30`

while [ $xstart -gt $xend ] ; do

  xname=`awk 'BEGIN {printf "%3.3d",'$xstart';exit}'`
  echo $xname
  xstart=`expr $xstart - 1`

done
Closed Thread

Bookmarks

Tags
linux

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 04:12 AM.


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