bash typeset padding with zeros


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash typeset padding with zeros
# 1  
Old 07-08-2008
Data 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.

Code:
#! /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


Last edited by rbatte1; 08-03-2017 at 07:11 AM.. Reason: Code tags
# 2  
Old 07-08-2008
Hammer & Screwdriver Perhaps use printf command

Without reading your entire code...

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

Can you apply this logic into your script?
# 3  
Old 07-08-2008
that did the trick. Thank you so much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort, sed, and zero padding date column csv bash scripting

Hello people, I am having problem to sort, sed and zero padding of column in csv file. 7th column only. Input of csv file: 1,2,3,4,5,6,4/1/2010 12:00 AM,8 1,2,3,4,5,6,3/11/2010 9:39 AM,8 1,2,3,4,5,6,5/12/2011 3:43 PM,8 1,2,3,4,5,6,12/20/2009 7:23 PM,8 Output:... (5 Replies)
Discussion started by: sean1357
5 Replies

2. UNIX for Dummies Questions & Answers

Is there a way to add padding to a bash shell script?

I'm grepping several lines and I want all of them to display with the same padding, is there a command to add whitespace before a line? (1 Reply)
Discussion started by: jcnewton13
1 Replies

3. Shell Programming and Scripting

converting ksh to bash - typeset commands

Hi all, Am trying to convert a script written in ksh to a bash shell. At the moment, am stumped with the typeset -u command and I can't find an equivalent of it in bash. integer function is also not working as is the following if statement if ] && ]; then continue fi Is... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

zero padding problem (bash)

Hi there, I need to loop some values, for i in $(seq $first $last) do does something here donefor $first and $last, i need it to be of fixed length 5. so if the input is 1, i need to add zeros in front such that it becomes 00001. It loops till 99999 for example, but the length has to be... (4 Replies)
Discussion started by: jremio
4 Replies

5. Shell Programming and Scripting

bash padding

Hi all Is there a way to pad the output of a bash script see that code below for i in `sed -n '/Start Printer/,/End Printer/p' /u/ab/scripts/hosts.conf | awk '!/^#/ {print $2}' | egrep -v 'broke|primera' `; do pages=`snmpget -Ov -v1 -c public $i sysLocation.0 | awk '{print $2}'` ... (3 Replies)
Discussion started by: ab52
3 Replies

6. Shell Programming and Scripting

Typeset conversion problem from ksh to bash

Hi, typeset -l sgf # all lowercase letters typeset -u SGF # all uppercase letters sgf=$1 SGF=$sgf these lines used in my scripts . It ran fine in ksh but when we convert this to bash it erroring out. I like to know what the use of typeset ?? Thanks & Regards kanagaraj (3 Replies)
Discussion started by: kanagaraj
3 Replies

7. Shell Programming and Scripting

Padding with zeros.

Hi Friends, I would like to left pad with "0's" on first column say (width six) I have a large file with the format: FILE: 1: ALFRED 84378 NY 8385: JAMES 88385 FL 323: SMITH 00850 TX My output needs to be like: 000001: ALFRED 84378 NY 008385: JAMES 88385 FL 000323: SMITH... (10 Replies)
Discussion started by: sbasetty
10 Replies

8. Shell Programming and Scripting

Help needed in padding leading zeros

Hi all, I have file with numeric values. I need to pad each value with leading zeros such that total lenght of each value is 16. Example: cat tmp.txt 502455 50255 5026 5027 5028 Output 0000000000502455 0000000000050255 0000000000005026 0000000000005027 0000000000005028 Any... (12 Replies)
Discussion started by: jakSun8
12 Replies

9. Shell Programming and Scripting

Help with typeset in bash

Hi everybody, hoping you can help. I'm trying to get some scripts working using bash which were written in ksh and I'm struggling with typeset. Specifically typeset -R and typeset -L. We need fixed length variables with left and right justification and bash does not seem to do it. Spent ages on... (5 Replies)
Discussion started by: Ian_H
5 Replies

10. HP-UX

Padding zeros after removing commas in file

Hi Gurus, There is a ASCII file in which a comma is used as a seperator for the amount field when the amount exceed seven digits: e.g. 0001300,000. Now, this comma needs to be removed from this field, after padding leading zeros (to maintain the ASCII positions) e.g. 00001300000.... (1 Reply)
Discussion started by: pranag21
1 Replies
Login or Register to Ask a Question