Find the answer to your Linux question:
Results 1 to 9 of 9
Code: LIMIT=20 for((a=0;a < LIMIT; a++)) do echo "Adding User$a..." useradd -m -u 19$a -g test -d /home/test$a -s /bin/sh -c "Test$a" tester$a done echo; exit 0 the above is ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    4

    Face Problems in Scripting

    Code:
    LIMIT=20
    for((a=0;a < LIMIT; a++))
    do
    echo "Adding User$a..."
    useradd -m -u 19$a -g test -d /home/test$a -s /bin/sh -c "Test$a" tester$a
    done
    echo;
    exit 0
    the above is the code i figure out after reading some online BASH guide
    but everytime i add the first few digits from 0 - 9 [190 - 199] is added to the
    System users and 10 -19 [1910 - 1919] is to the Normal user

    how can i add another 0 infront of those Single Digit number 0-9 to prevent them from adding into the System Users? 0 - 9 -> 00 - 09

    Thanks James.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Hi James,

    You can format the numbers with printf, try this:

    Code:
    LIMIT=20
    for((a=0;a < LIMIT; a++))
    do
      echo "Adding User $a ..."
      user=`printf "%02d" $a`
      useradd -m -u 19$user -g test -d /home/test$user -s /bin/sh -c "Test$user" tester$user
    done
    echo;
    exit 0
    Regards

  3. #3
    Just Joined!
    Join Date
    May 2007
    Posts
    4
    Great it works but all numbers have a 0 infront[after some tweaking]
    i just need those 0 - 9 to have a 0 infront

    IF i use a if else condition i have think of

    Code:
    if [ a < 10 ]
    then
    user=`printf "&#37;01d"$a`
    else
    user=`printf $a`
    fi
    when i try to mkdir /home/test$user
    it did make some directories but they have no 0 infront for all folders as in test0 - test99 after executing
    but it said no such file or directory for the line {if [ a < 10 ]}

    any ideas?

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    In your printf statement there must be a space between the arguments.
    This should work for the digits 0-99:

    Code:
    user=`printf "&#37;02d" $a`
    You don't have to use an if else statement.

    Regards

  5. #5
    Just Joined!
    Join Date
    May 2007
    Posts
    4
    Quote Originally Posted by Franklin52 View Post
    In your printf statement there must be a space between the arguments.
    This should work for the digits 0-99:

    Code:
    user=`printf "%02d" $a`
    You don't have to use an if else statement.

    Regards
    Thanks but if i want to apply it to 0 - 9 only and leave the rest untouch?

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by james99 View Post
    Thanks but if i want to apply it to 0 - 9 only and leave the rest untouch?
    Right, try this to see what you get:

    Code:
    LIMIT=20
    for((a=0;a < LIMIT; a++))
    do
      user=`printf "%02d" $a`
      echo $user
    done
    Regards

  7. #7
    Just Joined!
    Join Date
    May 2007
    Posts
    4
    Quote Originally Posted by Franklin52 View Post
    Right, try this to see what you get:

    Code:
    LIMIT=20
    for((a=0;a < LIMIT; a++))
    do
      user=`printf "&#37;02d" $a`
      echo $user
    done
    Regards
    Thanks.
    I solve it by using 2 FOR loops
    1 with user=`printf "%01d" $a` for 0 - 9 Results : 00-09
    another is user=`printf $a` for 10 - 99 Results : 10-99

  8. #8
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Code:
    printf "%01d" 9
    that produces "09"? It shouldn't...and you shouldn't need two different for loops. The "%02d" pattern means "Treat the argument as a number. The number should be 2 characters long, and if it is less than 2 characters, then left-pad it with 0s.

    In fact, here's my script:
    Code:
    #!/bin/bash
    
    # @(#)  printf-2-digits         Demonstrate printf's formatting abilities
    
    LIMIT=20
    
    for((a=0; a < LIMIT; a++)); do
            user=$(printf '%02d' $a)
            echo $user
    done
    It produces the following output:
    Code:
    alex@danu ~/test/bash $ ./printf-2-digits 
    00
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    DISTRO=Arch
    Registered Linux User #388732

  9. #9
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by Cabhan View Post
    Code:
    printf "%01d" 9
    that produces "09"? It shouldn't...and you shouldn't need two different for loops. The "%02d" pattern means "Treat the argument as a number. The number should be 2 characters long, and if it is less than 2 characters, then left-pad it with 0s.

    In fact, here's my script:
    Code:
    #!/bin/bash
    
    # @(#)  printf-2-digits         Demonstrate printf's formatting abilities
    
    LIMIT=20
    
    for((a=0; a < LIMIT; a++)); do
            user=$(printf '%02d' $a)
            echo $user
    done
    [/code]
    That's what I've early mentioned:

    Right, try this to see what you get:

    Code:
    LIMIT=20 
    for((a=0;a < LIMIT; a++)) 
    do 
      user=`printf "%02d" $a` 
      echo $user 
    done
    But I think he didn't try it

    Regards

Posting Permissions

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