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 ...
- 05-23-2007 #1Just Joined!
- Join Date
- May 2007
- Posts
- 4
Face Problems in Scripting
the above is the code i figure out after reading some online BASH guideCode: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
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.
- 05-23-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Hi James,
You can format the numbers with printf, try this:
RegardsCode: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
- 05-24-2007 #3Just 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
when i try to mkdir /home/test$userCode:if [ a < 10 ] then user=`printf "%01d"$a` else user=`printf $a` fi
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?
- 05-24-2007 #4Linux 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:
You don't have to use an if else statement.Code:user=`printf "%02d" $a`
Regards
- 05-25-2007 #5Just Joined!
- Join Date
- May 2007
- Posts
- 4
- 05-25-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
- 05-25-2007 #7Just Joined!
- Join Date
- May 2007
- Posts
- 4
- 05-25-2007 #8that 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.Code:
printf "%01d" 9
In fact, here's my script:
It produces the following output: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: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
- 05-25-2007 #9Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631


Reply With Quote
