Question
(Linux Bash Scripting Question) The following completed code is for a script called createuser that has the following functions: #!/bin/bash if [ $# -eq 0
(Linux Bash Scripting Question)
The following completed code is for a script called "createuser" that has the following functions:
#!/bin/bash
if [ $# -eq 0 ]; then
echo "createuser: usage: createuser [-p] [-d path] [newuserid]"
fi
DFLAG=false
PFLAG=false
USERDIR=''
USERID=''
while [ $# -ne 0 ]; do
case $1 in
-*p*)PFLAG=true
;;&
-*d) if [ $# -gt 1 ]; then
DFLAG=true
USERDIR=$2
shift
fi
;;
-*);;
*)USERID=$1
esac
shift
done
if [ "$USERID" = '' ]; then
echo "createuser: No userid entered" 1>&2
exit 1
fi
getent passwd $USERID 1>/devull 2>&1
if [ $? -eq 0 ]; then
echo "createuser: user '"$USERID"' already exists"
fi
if [ $DFLAG = false ]; then
USERDIR='/home/'$USERID
fi
useradd -d$USERDIR -m $USERID
if [ $PFLAG = true ]; then
passwd $USERID
fi
For the above code, kindly add to the code to include the following:
For part (b), the code has to work with both -d/path/to/dir and -pd/path/to/dir. Back references and BASH_REMATCH can be used for it and it will take slightly less code in the script, as that will simply be a variable assignment. Substrings can also be used, however, it might be more complicated to cover both circumstances.
createuser which creates a new user account. The script will give an error message if the userid already exists or if a userid is not provided. The script will create the new user's home directory, set ownership & permissions correctly. The script will copy the contents of /etc/skel into the new directory The script will handle the following flags: Option Description -d Path for home directory. If -d is not specified the home directory is created at /home/userid. -P Prompt for password. If -p is not specified the password should be left blank. (b) The -d flag will now support the following forms two (2) forms: i. createuser -d/path/to/dir (no space between the d and the path) ii. createuser -d /path/to/dir (c) Use regular expression to ensure that the path for -d is an absolute path. Output an error message if the path is not an absolute pathStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started