Question

In: Computer Science

Use a while loop to read each line in the file modified_etc_passwd directly and process each...

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?

Solutions

Expert Solution

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:


Related Solutions

Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Write a program Median.java to read each file whose name is specified in the command-line arguments....
Write a program Median.java to read each file whose name is specified in the command-line arguments. That is, for each command-line argument, open it as a file and read it. The file contents are zero or more lines each containing a list of comma-separated integers, such as 1,2,3,4 or 99,120,33. You should parse each of these integers and save them in an ArrayList (if you prefer you may use an array, but an ArrayList is likely to be easier for...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
For each of the following answers, determine whether the answer better describes a for loop or a while loop.
For each of the following answers, determine whether the answer better describes a for loop or a while loop.It performs one iteration for every value in a sequence.It uses a loop variable.It continues the loop until the condition becomes false.It continues the loop until reaching the end of the sequence.It can easily cause infinite loops.The exact number of iterations are usally known when starting the loop.
In python explain why would we want to read a file line by line instead of...
In python explain why would we want to read a file line by line instead of all at once?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT