Reputation: 25
I am working on a script where I want to iterate between the numbers 1 to 15, but want it shown as 01 02 03 ... 13 14 15. Essentially what I am trying to do is add 15 users using the newusers command and using this script as < to the command. newusers needs to be in this format:
pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell
Basically, it should look like this when I run the script with arguments =
cstuser01:EzVlK9Je8JvfQump:1001:1001:CST8177 user:/home/cstuser01:/bin/bash
cstuser02:EsKOfvhgnWpiBT6c:1002:1002:CST8177 user:/home/cstuser02:/bin/bash
cstuser03:qzQuR5vRgxdzY6dq:1003:1003:CST8177 user:/home/cstuser03:/bin/bash
I got most of it working but I am getting the error below:
./15users.sh: 57: ./15users.sh: Illegal number: 08
Here is my script so far (I took out a couple sections with error checking) =
#!/usr/bin/env bash
PATH=/bin:/usr/bin ; export PATH
umask=022
#num=1 (this variable is needed depending on which loop I use below)
user=$prefix"user"
uid=1001
gid=$uid
home=/home/$user
shell=/bin/bash
pad=printf "%02d\n"
#echo "pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell"
#PASSWD=$(openssl rand -base64 12)
I originally had this but ran into a few problems:
while [ $NUM -le 15 ] ; do
if [ $NUM -lt 10 ] ; then
NUM=0$NUM
fi
echo "$USER$NUM:$(openssl rand -base64 12):$UID:$GID:$GECO:$HOME$NUM:$SHELL"
UID=$(( UID + 1 ))
GID=$(( GID + 1 ))
NUM=$(( NUM + 1 ))
done
A friend of mine suggested this, it works perfectly fine. But I am trying to future proof this thing. What if I have a 100 or 1,000 users to add.
for NUM in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 ; do
echo "$USER$NUM:$(openssl rand -base64 12):$UID:$GID:$GECO:$HOME$NUM:$SHELL"
done
This didn't work:
for num in {01..15} ; do
i=09
echo "$(( 10#$num + 1 ))"
10
done
I then tried this but isn't padding 0 or increasing uid or gid =
for (( num=1; num<=15; num++ )) ; do
echo "$user$pad$num:$(openssl rand -base64 12):$uid:$gid:$geco:$home$num:$shell"
done
I tried this as well but seq prints vertically not horizontally:
#iterate=$(seq -w 1 15)
for $iterate ; do
echo "$user$num:$(openssl rand -base64 12):$uid:$gid:$geco:$home$num:$shell"
done
Upvotes: 1
Views: 403
Reputation: 25
I fixed it, thanks #Inian, here it is. I have one more nitpicky thing, that I want to ask about though.
#!/usr/bin/env bash
PATH=/bin:/usr/bin ; export PATH
umask=022
I want to make a correction the error-checking below. The output should look like this =
cstuser01:EzVlK9Je8JvfQump:1001:1001:CST8177 user:/home/cstuser01:/bin/bash
cstuser02:EsKOfvhgnWpiBT6c:1002:1002:CST8177 user:/home/cstuser02:/bin/bash
cstuser03:qzQuR5vRgxdzY6dq:1003:1003:CST8177 user:/home/cstuser03:/bin/bash
How do you allow for spaces in the Gecos field i.e. CST8177 user if I am blocking null arguments below? The Gecos is supposed to be essentially your full name, so obviously it will have spaces. But when I try to run it like so:
./15users cat "Feline user" (for example) it pumps out an error message which I created for error checking.
#This if statements checks if you passed anything other than 2 arguments to the script file in the command-line
if [ "$#" -ne 2 ] ; then
echo 1>&2 "$0: Expecting only two arguments to be passed to the script. Found: $# arguments, which was or were ($*)"
echo 1>&2 "Usage: $0 Username-prefix GECOS/Description"
exit 2
fi
#This case statement checks to ensure that all arguments passed to the command line are letters only and contain no special characters or blank arguments
Also is there a way to make these two case statements below into one since they are checking the first and second argument for the same things?
case "$1" in
'' ) echo 1>&2 "$0: Expecting two arguments that use alphabetic characters. Found: $# arguments, which was or were ($*)"
echo 1>&2 "Usage: $0 Username-prefix GECOS/Description (Cannot use blank arguments)" ;;
*[![:alpha:]]* ) echo 1>&2 "$0: Expecting two arguments that use only letters. Found: $# arguments, which was or were ($*)"
echo 1>&2 "Usage: $0 Username-prefix GECOS/Description (No numbers, spaces, or special characters)" ;;
* ) prefix=$1 ;;
esac
#This case statement checks to ensure that all arguments passed to the command line are letters only and contain no special characters or blank arguments
case "$2" in
'' ) echo 1>&2 "$0: Expecting two arguments that use alphabetic characters. Found: $# arguments, which was or were ($*)"
echo 1>&2 "Usage: $0 Username-prefix GECOS/Description (Cannot use blank arguments)" ;;
*[![:alpha:]]* ) echo 1>&2 "$0: Expecting two arguments that use only letters. Found: $# arguments, which was or were ($*)"
echo 1>&2 "Usage: $0 Username-prefix GECOS/Description (No numbers, spaces, or special characters)" ;;
* ) geco=$2 ;;
esac
num=1
user=$prefix"user"
uid=1001
gid=$uid
home=/home/$user
shell=/bin/bash
#echo "pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell"
#PASSWD=$(openssl rand -base64 12)
for (( num=1; num<=30; num++ )) ; do
echo "$user$(printf "%02d\n" $num):$(openssl rand -base64 12):$uid:$gid:$geco:$home$(printf "%02d\n" $num):$shell"
uid=$(( uid + 1 ))
gid=$(( gid + 1 ))
done
Upvotes: 0
Reputation: 67507
bash
brace expansion should do the job!
$ for i in {01..15}; do echo "user$i"; done
user01
user02
user03
user04
user05
user06
user07
user08
user09
user10
user11
user12
user13
user14
user15
Upvotes: 1