Find the answer to your Linux question:
Results 1 to 7 of 7
Hi Guys This is my first time posting on this forum so I would like to say hello to everyone. I have a problem that I hope someone can solved. ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Location
    Sydney
    Posts
    6

    Unhappy seq number incrementing

    Hi Guys

    This is my first time posting on this forum so I would like to say hello to everyone.

    I have a problem that I hope someone can solved. It could be a very simple answer but this problem has me stuck.

    I am writing a script so that I do not have to sit at the terminal of our Tape Storage server and type fsclean -t <TAPE NUMBER> eg 000001, 000002 to remove unlinked media written to our tapes. I would otherwise need to sit at the terminal and type fsclean -t 000001 then fsclean -t 000200 and so on till i got to 200.

    So I am trying to write a bash script that will ask me how many tapes I want to remove unlinked media from and the ask me what tape to start from. Because I am going to clean tapes in batches depending on the amount of offline time I can get on a given day. So the number of tapes to clean might vary from 3 to 5 or 10.

    I have used the READ command to convert these 2 numbers into variables $TAPE_NO and $START_TAPE_NO.

    Basically all I want to do is some how get linux to calculate a number sequence starting from the tape number I enter (ie. 000001) and then print in sequence the numbers after that using the variable $TAPE_NO (ie. 5 tapes to be cleaned including the tape number i enter). Then I want to get a list that is like this:
    000001
    000002
    000003
    000004
    000005 etc...

    Which I will then put into a for loop command to run fsclean -t <INSERT NUMBER SEQUENCE> and loop that till the last tape number has been used.

    I thought I could do this from the linux command seq but the problem i am facing with this command is it does not keep the starting 0's nor can I get a result from seq if i enter seq 00010 and want to increment it by 5. I have entered just in the command line seq 000001 5 and i get:
    1
    2
    3
    4
    5

    I some how need to get the starting 0's to stay as tape numbers entered into my command need to be exact. And also make sure that if i enter tape number 000020 and want to scan 5 tapes that the command will just continue the sequence 000021, 000022, etc..

    Is seq the right command to use if not what is an alternative command what would achieve the same results.

    My Script so far:

    #!/bin/bash

    #
    # ------------------------------ Defining System Tool Paths----------------------------------------------
    ID=/usr/bin/id;
    ECHO=/bin/echo;
    READ=/usr/bin/read;
    CLEAR=/usr/bin/clear

    #FSMEDINFO=/usr/adic/TSM/bin/fsmedinfo;
    GREP=/usr/bin/grep;
    #
    # ----------------------------- Start of Script are you root? ---------------------------------------------
    if (( `$ID -u` != 0 )); then { $ECHO "Sorry, must be root. Exiting..."; exit; } fi;
    #
    #
    #
    # ---------------------------- Start of Main Script ------------------------------------------------------
    #
    #
    $CLEAR
    $ECHO -n "This script will clean any inactive media from the Tape in a Quantum Tape Library. ";
    $ECHO
    $ECHO
    $ECHO -n "How many tapes do you want to clean? "
    read TAPENO
    #
    $ECHO -n "Which tape would you like to start with. Please ender full tape number (e.g.. 000001 or 000010): "
    read START_TAPENO
    $ECHO
    $ECHO
    $CLEAR
    #
    $ECHO " Confirm Details "
    $ECHO
    $ECHO
    $ECHO -n "You have selected to clean $TAPENO tapes starting from tape $START_TAPENO. Would you like to proceed? (n): "
    $READ STARTSCRIPT
    #
    if [ $STARTSCRIPT == 'n' ]; then
    exit;
    fi
    #
    for REAL_TAPE_NO in `seq $START_TAPENO $TAPENO`
    do
    echo $REAL_TAPE_NO
    done

  2. #2
    Just Joined! sathiya's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    97
    you have to use the -w option, as

    Code:
    seq -w 00001 5
    00001
    00002
    00003
    00004
    00005

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Location
    Sydney
    Posts
    6
    Quote Originally Posted by sathiya View Post
    you have to use the -w option, as

    Code:
    seq -w 00001 5
    00001
    00002
    00003
    00004
    00005
    I have tried to use seq -w 00001 5 and I still only get:
    1
    2
    3
    4
    5

    I am using Red Hat EL 4 if that makes any difference?

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    The seq command seems to be different on some RedHat & CentOS editions. Here is a work-around:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate 0-padding to left.
    
    # Infrastructure details, environment, commands for forum posts. 
    set +o nounset
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
    echo "(Versions displayed with local utility \"version\")"
    c=$( ps | grep $$ | awk '{print $NF}' )
    version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
    [ "$c" = "$s" ] && p="$s" || p="$c"
    version >/dev/null 2>&1 && version "=o" $p seq
    set -o nounset
    
    echo
    echo " Failure without format:"
    seq -w 00001 5
    
    echo
    echo " Success with format:"
    seq --format=%05.f 00001 5
    
    exit 0
    producing:
    Code:
    $ ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.18-164.11.1.el5, i686
    Distribution        : CentOS release 5.4 (Final)
    GNU bash 3.2.25
    seq (GNU coreutils) 5.97
    
     Failure without format:
    1
    2
    3
    4
    5
    
     Success with format:
    00001
    00002
    00003
    00004
    00005
    See man pages for seq and printf(3c) ... 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 )

  5. #5
    Just Joined!
    Join Date
    Mar 2010
    Location
    Sydney
    Posts
    6
    Thanks drl that has done the trick

  6. #6
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    You may try the following bash built-in expansion too :

    Code:
    echo {1..00005}
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  7. #7
    Just Joined!
    Join Date
    Mar 2010
    Location
    Sydney
    Posts
    6

    Red face

    Quote Originally Posted by nmset View Post
    You may try the following bash built-in expansion too :

    Code:
    echo {1..00005}
    I have tried this command and all i get is

    1 2 3 4 5

    I am not sure how this particular command works for me? It may just be my lack of knowledge

Posting Permissions

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