179k views
4 votes
Write a BASH script to create a user account.  The script should process two positional parameters: o First positional parameter is supposed to be a string with user name (e.g., user_name) o Second positional parameter is supposed to be a string with user password (e.g., user_password)  The script should be able to process one option (use getopts command): o Option -f arg_to_opt_f that will make your script direct all its output messages to file -arg_to_opt_f

User Phoexo
by
6.7k points

1 Answer

2 votes

#!/bin/bash

usage() {

echo "Usage: $0 [ -f outfile ] name password" 1>&2

exit 1

}

while getopts "f:" o; do

case "${o}" in

f)

filename=${OPTARG}

;;

*)

usage

;;

esac

done

shift $((OPTIND-1))

name=$1

password=$2

if [ -z "$password" ] ; then

usage

fi

set -x

if [ -z "$filename" ] ; then

useradd -p `crypt $password` $name

else

useradd -p `crypt $password` $name > $filename

fi

User Kivanc
by
6.1k points