Find the answer to your Linux question:
Results 1 to 4 of 4
Hi Experts , We recently migrated from Unix to GNU Linux . We were using the command typeset -Z3 for incrementing counter values like ( 001,002,003....999 ) in Unix. We ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2011
    Posts
    1

    Need to prepend Zero's to a Count Variable


    Hi Experts ,

    We recently migrated from Unix to GNU Linux .

    We were using the command typeset -Z3 for incrementing
    counter values like ( 001,002,003....999 ) in Unix.

    We are looking for an equivalent command that will perform this operation in Linux. We need the zero's prepended for the
    variable and it cannot be avoided.

    Any help is greatly appreciable. Thanks !!

    Regards,
    Harish

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,377
    This might help:
    Code:
    man printf
    You must always face the curtain with a bow.

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    If you're simply trying to generate sequential numbers, you might try the seq command. In particular, the -w flag:
    Code:
    alex@niamh:~$ seq -w 1 10
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,286
    Hi, harishraghunathan.

    Welcome to the forum.

    If you have lots of the typeset statements, and you want to change as little as possible, you could consider using shells that recognize the option "-Z3". However, the printf solution noted by Irithori could be made to work in many shells.

    Here is a script that runs a fragment from a shell to demonstrate these ideas. The main script causes the three commonly-available shells bash, ksh, and zsh to execute the shell fragment commands. The versions of the shells are noted.
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate comparison of bash, ksh, zsh for "typeset".
    
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C ksh zsh
    
    FILE=${1-shell-fragment}
    
    pl " Input data file $FILE:"
    cat -n $FILE
    
    pl " Results, bash:"
    bash $FILE
    
    pl " Results, ksh:"
    ksh $FILE
    
    pl " Results, zsh:"
    zsh $FILE
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    GNU bash 3.2.39
    ksh 93s+
    zsh 4.3.6
    
    -----
     Input data file shell-fragment:
         1	# Shell fragment to test availability of:
         2	#   typeset -Z3 v1
         3	# In various shells.
         4	
         5	typeset -Z3 v1=3 v2=22 v3=999
         6	echo " v1 is \"$v1\", v2 is \"$v2\", v3 is \"$v3\""
         7	
         8	v4=5
         9	printf " v4 from printf is \"%03d\"\n" $v4
    
    -----
     Results, bash:
    shell-fragment: line 5: typeset: -Z: invalid option
    typeset: usage: typeset [-afFirtx] [-p] name[=value] ...
     v1 is "", v2 is "", v3 is ""
     v4 from printf is "005"
    
    -----
     Results, ksh:
     v1 is "003", v2 is "022", v3 is "999"
     v4 from printf is "005"
    
    -----
     Results, zsh:
     v1 is "003", v2 is "022", v3 is "999"
     v4 from printf is "005"
    As you can see, bash (often the default shell in many GNU/Linux distributions) does not handle "-Z3", but the other two are fine. The printf solution works in all three shells. The printf command is usually a built-in, but there may also be a printf external command.

    The man pages for shells are extremely long, but are your best source of accurate information. Those pages in combination with experimentation will be valuable guides to converting your shell scripts, as well in writing new scripts.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •