Sponsored Content
Top Forums Shell Programming and Scripting bash typeset padding with zeros Post 302212831 by jwheeler on Tuesday 8th of July 2008 12:35:48 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
MT(1L)																	    MT(1L)

NAME
mt - control magnetic tape drive operation SYNOPSIS
mt [-V] [-f device] [--file=device] [--rsh-command=command] [--version] operation [count] DESCRIPTION
This manual page documents the GNU version of mt. mt performs the given operation, which must be one of the tape operations listed below, on a tape drive. The default tape device to operate on is taken from the file /usr/include/sys/mtio.h when mt is compiled. It can be overridden by giving a device file name in the environment variable TAPE or by a command line option (see below), which also overrides the environment variable. The device must be either a character special file or a remote tape drive. To use a tape drive on another machine as the archive, use a filename that starts with `HOSTNAME:'. The hostname can be preceded by a username and an `@' to access the remote tape drive as that user, if you have permission to do so (typically an entry in that user's `~/.rhosts' file). The available operations are listed below. Unique abbreviations are accepted. Not all operations are available on all systems, or work on all types of tape drives. Some operations optionally take a repeat count, which can be given after the operation name and defaults to 1. eof, weof Write count EOF marks at current position. fsf Forward space count files. The tape is positioned on the first block of the next file. bsf Backward space count files. The tape is positioned on the first block of the next file. fsr Forward space count records. bsr Backward space count records. bsfm Backward space count file marks. The tape is positioned on the beginning-of-the-tape side of the file mark. fsfm Forward space count file marks. The tape is positioned on the beginning-of-the-tape side of the file mark. asf Absolute space to file number count. Equivalent to rewind followed by fsf count. eom Space to the end of the recorded media on the tape (for appending files onto tapes). rewind Rewind the tape. offline, rewoffl Rewind the tape and, if applicable, unload the tape. status Print status information about the tape unit. retension Rewind the tape, then wind it to the end of the reel, then rewind it again. erase Erase the tape. fss (SCSI tapes) Forward space count setmarks. bss (SCSI tapes) Backward space count setmarks. wset (SCSI tapes) Write count setmarks at current position (only SCSI tape). eod, seod Space to end of valid data. Used on streamer tape drives to append data to the logical and of tape. setblk (SCSI tapes) Set the block size of the drive to count bytes per record. setdensity (SCSI tapes) Set the tape density code to count. The proper codes to use with each drive should be looked up from the drive docu- mentation. drvbuffer (SCSI tapes) Set the tape drive buffer code to number. The proper value for unbuffered operation is zero and "normal" buffered operation one. The meanings of other values can be found in the drive documentation or, in case of a SCSI-2 drive, from the SCSI-2 standard. stoptions (SCSI tapes) Set the driver options bits to count for the device. The bits can be set by oring the following values: 1 to enable write buffering, 2 to enable asynchronous writes, 4 to enable read ahead, 8 to enable debugging output (if it has been compiled to the driver). stwrthreshold (SCSI tapes) The write threshold for the tape device is set to count kilobytes. The value must be smaller than or equal to the driver buffer size. seek (SCSI tapes) Seek to the count block on the tape. This operation is available on some Tandberg and Wangtek streamers and some SCSI-2 tape drives. tell (SCSI tapes) Tell the current block on tape. This operation is available on some Tandberg and Wangtek streamers and some SCSI-2 tape drives. densities (SCSI tapes) Write explanation of some common density codes to standard output. datcompression (some SCSI-2 DAT tapes) Inquire or set the compression status (on/off). If the count is one the compression status is printed. If the count is zero, compression is disabled. Otherwise, compression is enabled. The command uses the SCSI ioctl to read and write the Data Compression Characteristics mode page(15). ONLY ROOT CAN USE THIS COMMAND. mt exits with a status of 0 if the operation succeeded, 1 if the operation or device name given was invalid, or 2 if the operation failed. OPTIONS -f, --file=device Use device as the file name of the tape drive to operate on. To use a tape drive on another machine, use a filename that starts with `HOSTNAME:'. The hostname can be preceded by a username and an `@' to access the remote tape drive as that user, if you have permission to do so (typically an entry in that user's `~/.rhosts' file). --rsh-command=command Notifies mt that it should use command to communicate with remote devices instead of /usr/bin/ssh or /usr/bin/rsh. -V, --version Print the version number of mt. MT(1L)
All times are GMT -4. The time now is 10:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy