You must provide the following 3 files: 1) Bash shell script 2)user creation log
ID: 3869118 • Letter: Y
Question
You must provide the following 3 files:
1) Bash shell script
2)user creation log file
3)the input file
to do the following on Ubuntu server:
User account creation script
Write a shell script that will create user accounts and set their home directories. The script will be ran on an Ubuntu server so make sure to research the use of the useradd command in this flavor of Linux. The account creation will be as follows:
Input file format
Your script will read the account information from a text file which has the following format
FirstName,LastName,PhoneNumber,email
Reading the File
Your script will read the file line by line and will create the account as follows
Your script must generate a user name with the following format
FirstLetterOfFirstNameLastName
Example
for user Bill Roberts, the user name created must be brobberts. In case the user name is already taken your script must add a number from 1 to as many numbers needed. Some repeated user names could be of the following format:
brobberts
brobberts1
brobberts2
The user account's password
Your script must also create a random password for each user. In order to do this, you will need to install a tool called pwgen
sudo apt-get install pwgen
To generate the password on the command line type:
pwgen 14 1
This generates a string of 14 characters
Creating the account
Once you have those two components, your script must execute the useradd command. After the account was successfully created, your script must create a public_html directory under the user’s home directory.
Log File
After the user account is created and its home directory is configured correctly, your script must output the username, password and home directory absolute path to a userCreation.log file.
Deliverable (3 Files)
You must provide your shell script, user creation log file, and the input file.
Explanation / Answer
1).
#!/bin/bash
# Counting the number of lines in a list of files
# for loop over arguments
# count only those files I am owner of
if [ $# -lt 1 ]
then
echo "Usage: $0 file ..."
exit 1
fi
echo "$0 counts the lines of code"
l=0
n=0
s=0
for f in $*
do
if [ -O $f ] # checks whether file owner is running the script
then
l=`wc -l $f | sed 's/^([0-9]*).*$//'`
echo "$f: $l"
n=$[ $n + 1 ]
s=$[ $s + $l ]
else
continue
fi
done
echo "$n files in total, with $s lines in total"
#!/bin/bash
############################################################################
#
# Usage: loc7.sh [options] file ...
#
# Count the number of lines in a given list of files.
# Uses a for loop over all arguments.
#
# Options:
# -h ... help message
# -d n ... consider only files modified within the last n days
# -w n ... consider only files modified within the last n weeks
#
# Limitations:
# . only one option should be given; a second one overrides
#
############################################################################
help=0
verb=0
weeks=0
# defaults
days=0
m=1
str="days"
getopts "hvd:w:" name
while [ "$name" != "?" ] ; do
case $name in
h) help=1;;
v) verb=1;;
d) days=$OPTARG
m=$OPTARG
str="days";;
w) weeks=$OPTARG
m=$OPTARG
str="weeks";;
esac
getopts "hvd:w:" name
done
if [ $help -eq 1 ]
then no_of_lines=`cat $0 | awk 'BEGIN { n = 0; }
/^$/ { print n;
exit; }
{ n++; }'`
echo "`head -$no_of_lines $0`"
exit
fi
shift $[ $OPTIND - 1 ]
if [ $# -lt 1 ]
then
echo "Usage: $0 file ..."
exit 1
fi
if [ $verb -eq 1 ]
then echo "$0 counts the lines of code"
fi
l=0
n=0
s=0
for f in $*
do
x=`stat -c "%y" $f`
# modification date
d=`date --date="$x" +%y%m%d`
# date of $m days/weeks ago
e=`date --date="$m $str ago" +%y%m%d`
# now
z=`date +%y%m%d`
#echo "Stat: $x; Now: $z; File: $d; $m $str ago: $e"
# checks whether file is more recent then req
if [ $d -ge $e -a $d -le $z ] # ToDo: fix year wrap-arounds
then
# be verbose if we found a recent file
if [ $verb -eq 1 ]
then echo "$f: modified (mmdd) $d"
fi
# do the line count
l=`wc -l $f | sed 's/^([0-9]*).*$//'`
echo "$f: $l"
# increase the counters
n=$[ $n + 1 ]
s=$[ $s + $l ]
else
# not strictly necessary, because it's the end of the loop
continue
fi
done
echo "$n files in total, with $s lines in total"
2).
Param
(
$FolderPath = "D: ewfolder*.*",
$ErrorActionPreference = "Stop"
)
$ChildItems = Get-ChildItem $FolderPath -Include *.txt,*.xml
$DestFolders = @()
foreach ($item in $ChildItems) {
if ((($item.LastWriteTime).Month -ne (Get-Date).Month) -or (($item.LastWriteTime).Year -ne (Get-Date).Year)) {
$FolderName = $(Get-Date $item.LastWriteTime -f "yyyyMM").ToString()
$DestFolder = ($item.Directory.ToString() + "" + $Foldername)
$DestFolders += $DestFolder
if (-not (Test-Path $DestFolder)) {
try {
New-Item $DestFolder -ItemType Directory -ErrorAction Stop
} catch {
throw $Error[0]
}
}
try {
Move-Item $item -Destination $DestFolder -ErrorAction Stop
} catch {
throw $Error[0]
}
}
}
foreach ($item in ($DestFolders | Sort-Object | Get-Unique)) {
try {
Add-Type -Assembly System.IO.Compression.FileSystem
$CompressLevel = [System.IO.Compression.CompressionLevel]::Optimal
$SourceFolder = $item
$DestFileName = ($item + ".zip")
[System.IO.Compression.ZipFile]::CreateFromDirectory($SourceFolder, $DestFileName, $CompressLevel, $false)
Remove-Item $SourceFolder -Recurse -ErrorAction Stop
} catch {
throw $Error[0]
}
}
3). input file:-
(
things=""
addToString() {
things="${things},$1"
}
while read line; do addToString $line ;done
echo $things
) < input.txt
test -z "$things" && things="$1" || things="${things},${1}"
things="${things}${things:+,}${1}"
tr ' ' ',' < input.txt | sed 's!,$! !'
#!/bin/bash
while read -r i
do
[[ $things == "" ]] && things="$i" || things="$things","$i"
done < <(grep . input.txt)
echo "$things"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.