In: Computer Science
Use a while loop to read each line in the file modified_etc_passwd directly and process each line as follows:
Use a combination of the echo and cut commands to assign the contents of the home directory field of the current record to a variable called homedir
Use a combination of the echo and cut commands to assign the contents of the username field to a variable called username
If homedir exists as a directory o Use a combination of the echo and cut commands to assign the contents that appears before the + of the GECOS field to a variable called fullname o If /home/STUDENTS/majors/username exists Echo fullname and append to the file majors.txt Example: echo $fullname >> majors.txt o Otherwise If /home/STUDENTS/nonmajors/username exists Echo fullname and append to the file nonmajors.txt Otherwise Echo username and append to the file not_student.txt Otherwise o Echo the current record and append to the file no_homedir.txt
When finished, execute your script with official data file provided and create 4 clean output files.
How do I write this in VNC Viewer?
Working code implemented in Shell.
main.sh Source Code:
#This script checks to see if certain directories exist and
where they are located.
# Then it generates some lists in files that contain
which group each individual
# user belongs to.
#! bin/bash
#read each line of input file
while read line;do
#create variable names for the home directory and
username
homedir=`echo $line|cut -d: -f6`
username=`echo $line|cut -d: -f1`
#if the home directory exists
if [ -d $homedir ];then
#create a variable for the full
name
fullname=`echo $line|cut -d:
-f5|cut -d+ -f1`
#if the user's directory is a
subdirectory of 'majors'
if [ -d
/home/STUDENTS/majors/$username ];then
#put full name
in 'majors.txt' list file
echo $fullname
>> majors.txt
else
#if the user's
directory is a subdirectory of 'nonmajors'
if [ -d
/home/STUDENTS/nonmajors/$username ];then
#put full name in 'nonmajors.txt' list
file
echo $fullname >> nonmajors.txt
else
#else put username into 'not_student.txt' list
file
echo $username >> not_student.txt
fi
fi
else
#else: put the line read into
'no_homedir.txt' list file
echo $line >>
no_homedir.txt
fi
#input file
done < modified_etc_passwd
Code Screenshots: