Changes

Jump to: navigation, search

OPS235 Lab 4 - CentOS7

71 bytes added, 11:26, 25 April 2015
no edit summary
<code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;">
<br>
::&#35;!/bin/bash ::::::&#35; createUsers.bash::&#35; Purpose: Generates a batch of user accounts (user data stored in a text file)::&#35;<br>&#35; USAGE:::&#35;<br>&#35; /root/createUsers.bash [-i {input-path}] [-n {full name}] [-a {aging}] [-g {add to group}] [-o {output-path}]::&#35;::&#35; Author: *** INSERT YOUR NAME ***::&#35; Date: *** CURRENT DATE ***
<br>
&#35; createUsers.bash<br>&#35; Purpose: Generates a batch of user accounts (user data stored in a text file)<br>&#35;<br>&#35; USAGE:<br>&#35;<br>&#35; /root/createUsers.bash [-i {input-path}] [-n {full name}] [-a {aging}] [-g {add to group}] [-o {output-path}]<br>&#35;<br>&#35; Author:*** INSERT YOUR NAME ***<br>&#35; Date: *** CURRENT DATE ***<br><br>if [ $HOME != "root" ] # only runs if logged in as root<br>::then<br>&nbsp;echo "You must be logged in as root." >&2<br>::&nbsp;exit 1<br>::fi<br>
</code>
<br>
<br>
<code style="color:#3366CC;font-family:courier;font-size:.9em;">
<br>::outputFlag="n"<br>::while getopts i:n:a:g:o: name<br>::do<br>:: &nbsp;case $name in<br>:: &nbsp; &nbsp;i) inputFile=$OPTARG ;;<br>:: &nbsp; &nbsp;:) echo "Error: You need text after options requiring text"<br>:: &nbsp; &nbsp; &nbsp; &nbsp;exit 1 ;;<br>:: &nbsp; &nbsp;\?) echo "Error: Incorrect option"<br>:: &nbsp; &nbsp; &nbsp; &nbsp; exit 1 ;;<br>:: &nbsp;esac<br>
</code>
<ol><li value="6">Save your editing session, but remain in the text editor.</li><li>The code displayed below uses logic to exit the script if the input file does not exist. Command substitution is used to store each line of the input file as a positional parameter. There is one subtle problem here: The full names of the users contain spaces which can create havoc when trying to set each line as a separate positional parameter. In this case the sed command is used to convert spaces to plus signs (+), which will be converted back later. Finally, a '''for''' loop is used to create each account ('''useradd''') and mail the user their account information ('''mail'''). Add the following code:</li></ol>
<br>
<code style="color:#3366CC;font-family:courier;font-size:.9em;">
<br>::if [ ! -f $inputFile ]<br>::then<br>::&nbsp; echo "The file pathname \"$inputFile\" is empty or does not exist" >&2<br>::&nbsp; exit 2<br>::fi<br><br>::set $(sed 's/ /+/g' $inputfile) # temporarily convert spaces to + for storing lines as positional parameters<br><br>::for x<br>::do<br>::&nbsp; &nbsp; useradd -m -c "$(echo $x | cut -d":" -f2 | sed 's/+/ /g')" -p $(date | md5sum | cut -d" " -f1) $(echo $x | cut -d":" -f1)<br>::&nbsp; &nbsp; mail -s "Server Account Information" $(echo $x | cut -d":" -f3) <<+<br>::&nbsp; &nbsp; Here is your server account information:<br>::&nbsp; &nbsp; servername: myserver.senecac.on.ca<br>::&nbsp; &nbsp; username: $(echo $x | cut -d":" -f1)<br>::&nbsp; &nbsp; password: $(date | md5sum | cut -d" " -f1)<br>::&nbsp; &nbsp; Regards,<br>::&nbsp; &nbsp; IT Department<br>::+<br>::done<br><br>::echo -e "\n\nAccounts have been created\n\n"<br>::exit 0<br>
</code>
13,420
edits

Navigation menu