bash typeset padding with zeros

Hi everybody,

I have a question about typesetting. I originally wrote a script for use with ksh and now I am on a system that I cannot modify, and it only has bash.

In the original script I just did typeset -RZ4 variable and it would add the leading zeros. In bash, it doesn't work.

I've looked all over and I can't find anything useful that will help me format the variable.

the script is just a simple script to expire about a thousand tapes from netbackup's media database. It's very tedious to do it one at a time with the tapes being labeled NB0000 - NB0999.

so the variable I am typesetting will always be 4 digits.

Any help would be appreciated.

here is the code. it was my first attempt at shell scripting so don't make fun of it too much.

#! /bin/ksh
# Script to expire tapes in NBU
# REQUIRES that NBU to be installed (DUH)
# Tapes should be in order (ie. NR0000, NR0001, NR0002, etc)

# This ensures that our format will be preserved.
# Makes sure $addon has four digits.
typeset -RZ4 addon

# Get two letter prefix for tapes
clear
echo "Please enter the 2 letter prefix (ie. NR, AB, CD)"
read prefix

# Get the four digits of the first tape
echo "Please enter the last 4 digits of the first tape (ie. 0000 0001 00002)"
read addon

# Get the four digits of the last tape
echo "Please enter the last 4 digits of the last tape to expire"
read last

#Increment the last variable by one so that the while loop will work
let last++

# This creates our tape name (ie NR0000
newfix=$prefix$addon

# While loop to increment through the tapes
while (( $addon <  $last))
do
/usr/openv/netbackup/bin/admincmd/bpexpdate -d 0 -m $newfix -force
let addon++
newfix=$prefix$addon
done
echo " "
echo " "
echo "By the power of Grey Skull, the tapes have expired!"
exit 0

Without reading your entire code...

> myvar="123"
> myvart=$(printf "%.6d" "$myvar")
> echo $myvart
000123

Can you apply this logic into your script?

that did the trick. Thank you so much!