Logo Background

Unix Typeset Command

  • By on October 22, 2010 | 3 Comments

    Looking for Unix typeset command to format a variable with leading zero? This command is useful especially when you want to format one shell script variable and push it file as trailer count.

    Bear in mind you can only use Unix typeset command in Korn Shell and you won’t have any luck of it in Bash.

    The example as below shows how you can use Unix typeset command to format a variable with leading zero.

    #! /bin/ksh
    typeset -RZ9 LINE_COUNT
     
    LINE_COUNT=213
    echo $LINE_COUNT

    In the end, you will get the output result as 000000213 when you execute the shell script.

    Refer on the Unix typeset command parameter below to understand further why -R and -Z being used in this case.

    set typeset [-HLRZfilprtux[n] [name [=value]]...]
     
    -R
    Right justify and fill with leading blanks. If n is nonzero it defines the width 
    of the field, otherwise it is determined by the width of the value of first assignment. 
    The field is left filled with blanks or truncated from the end if the parameter 
    is reassigned. This turns the L flag off.
     
    -Z
    Right justify and fill with leading zeros if the first nonblank character is 
    a digit and the -L flag has not been set. If n is nonzero it defines the width 
    of the field, otherwise it is determined by the width of the value of first assignment.
    Previous
    Next
    » Oracle SQL Profile Tuning Command
  1. #1 Alitin
    July 13, 2011 10:26 am

    I’m trying to use the typeset command, but no sucessfull
    I am new about programation language.

    I wanna copy files from one folder to another, but I must copy one at a time, in sequence,
    because before copy the next file (input0002.dat), I must use the first one in other program.
    the script is in shell (.sh) and the files are named as follow:
    input0001.dat, input0002.dat, …, input1000.dat

    script summary
    a=1
    while [ $a -le 1000 ]
    do
    cp A/B/C/inp$a.dat A/B/inp$a.dat
    a=`expr $a + 1`
    done

    however, variable “a” don’t get in as “0001”, but only as “1” and obviously it won’t copy the file.

    I need a form of adding zeroes (0) before the variable “a” for the copy be possible.

    I’m trying to add zeroes before the variable “a”, as follow:
    typeset -Z4 v=”$a” where the result is = “0001”
    -Z4 indicates 4 characters, counting with the variable “a” (i.e. 3 zeroes + $a = 0001)
    but this is not working.

    Help me.

    Post ReplyPost Reply
  1. #2 Alitin
    July 13, 2011 11:39 am

    Help typeset command shows:

    typeset: usage: typeset [afFirtx] [-p] name[=value]…

    The -p option will display the attributes and values of each NAME.

    The flags are:
    -a to make NAMEs arrays (if supported)
    -f to select from among function names only
    -F to display function names (and line number and source file name if debugging) without definitions
    -i to make NAMEs have the `integer’ attribute
    -r to make NAMEs readonly
    -t to make NAMEs have the `trace’ attribute
    -x to make NAMEs export

    Post ReplyPost Reply
  1. #3 Alitin
    July 13, 2011 3:47 pm

    RESOLUTION FOR THIS PROBLEM

    I changed the typeset command by the printf command.

    The solution to copy files from a directory to another, so that you will use these files in sequence in other program was:

    #to know the number of existent files in the directory. In my case, the files were named as inpXX1.dat, where XX=00
    cd /directory/A
    nfiles=`ls -l *.dat | egrep -c ‘^-‘`
    echo “$nfiles” #the result is 100

    In my case, there were 100 files, because of this there are three characters after name “inpXXX.dat”, where XXX=(001, 002, …, 100)

    #Inserting the enough number of zeroes in the instant of copy: “%0Xd”, where X is the number of characters

    for ((x=1;x<=$nfiles;x+=1))
    do
    cp /directory/A/inp$(printf "%03d" $x).dat ../B/inp$(printf "%03d" $x).dat
    #After, do what you want with each file…
    done

    Post ReplyPost Reply
Leave a Comment