I need to create a script.
The script must accept the following command line options: -n account name
, and -r number of accounts
.
Upon execution the script must create a batch of user accounts based on the account name and number of accounts.
For example:
/usr/local/bin/batch_user -n bob -r 5
should create the user accounts bob1, bob2, bob3, bob4, bob5
This is the first time I use getopts
and I'm having some problems understanding how to use it, here's what I wrote so far:
#!/bin/bash
usage () { $0 -n usernanme -r number of creations };
while getopts ":n:r:" arg; do
case "${arg}" in
n)
n=${OPTARG}
;;
r)
r=${OPTARG}
;;
esac
done
First, I'm not sure how to parse the data after the first option (-n) then how do I run on the number (-r) to create the users? Thanks in advance
You can use:
#!/bin/bash
usage () { echo "$0 -n usernanme -r number of creations"; }
while getopts ":n:r:" arg; do
case "${arg}" in
n) n=${OPTARG} ;;
r) r=${OPTARG} ;;
esac
done
for ((i=0; i<r; i++)); do
echo "Adding user: ${n}${i}"
done