Question

Exercise 1: Write a shell script that loops through the /etc/passwd file one line at a...

Exercise 1:

Write a shell script that loops through the /etc/passwd file one line at a time. Prepend each line with a line number followed by a colon and then a space.

Example output:

1: root:x:0:0:root:/root:/bin/bash

2: daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

3: bin:x:2:2:bin:/bin:/usr/sbin/nologin

4: sys:x:3:3:sys:/dev:/usr/sbin/nologin

Exercise 2:

Write a shell script that asks the user for the number of lines they would like to display from the /etc/passwd file and display those lines.

Example output:

How many lines of /etc/passwd would you like to see? 3 root:x:0:0:root:/root:/bin/bash

daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin

Exercise 3:

Write a shell script that that allows a user to select an action from the menu. The actions are to show the disk usage, show the uptime on the system, and show the users that are logged into the system. Tell the user to enter q​​ to quit. Display "Goodbye!" just before the script exits.

If the user enters anything other than 1​​, ​2​, ​3​, or ​q​, tell them that it is an "Invalid option."

You can show the disk usage by using the df​​ command. To show the uptime, use the uptime​ command. To show the users logged into the system, use the who​ ​ command. Print a blank line after the output of each command.

Example output:

  1. Show disk usage.
  2. Show system uptime.
  3. Show the users logged into the system.

What would you like to do? (Enter q to quit.) 2

14:59:08 up 3 days, 7:36, 7 users, load average: 0.13, 0.22, 0.33

  1. Show disk usage.
  2. Show system uptime.
  3. Show the users logged into the system.

What would you like to do? (Enter q to quit.) 4

Invalid option.

  1. Show disk usage.
  2. Show system uptime.
  3. Show the users logged into the system.

What would you like to do? (Enter q to quit.) q

Goodbye!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
EXERCISE_NUMBER=0
TOTAL_ALLOWED_EXERCISE=3

# Exercise 1:
# Write a shell script that loops through the /etc/passwd file one line at a time. Prepend each line
# with a line number followed by a colon and then a space.
# Example output:
# 1: root:x:0:0:root:/root:/bin/bash
# 2: daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
# 3: bin:x:2:2:bin:/bin:/usr/sbin/nologin
# 4: sys:x:3:3:sys:/dev:/usr/sbin/nologin
function e1() {
        local line_number=0;
        grep -v "^#" /etc/passwd | while read line
        do
                echo $line_number:" "$line
                ((line_number++))
        done
}


# Exercise 2:
# Write a shell script that asks the user for the number of lines they would like to display from the
# /etc/passwd file and display those lines.
# Example output:
# How many lines of /etc/passwd would you like to see? 3
# root:x:0:0:root:/root:/bin/bash
# daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
# bin:x:2:2:bin:/bin:/usr/sbin/nologin
function e2() {
        read -p "How many lines of /etc/passwd would you like to see? " number_of_lines_user_see
        local line_number=0;
        grep -v "^#" /etc/passwd | while read line
        do
                if [ $number_of_lines_user_see -le $line_number ]
                        then
                                break
                fi
                echo $line_number:" "$line
                ((line_number++))
        done
}


# Exercise 3:
# Write a shell script that that allows a user to select an action from the menu. The actions are to
# show the disk usage, show the uptime on the system, and show the users that are logged into
# the system. Tell the user to enter q​to quit. Display "Goodbye!" just before the script exits.
# If the user enters anything other than 1​, 2​, 3​, or q​, tell them that it is an "Invalid option."
# You can show the disk usage by using the df​command. To show the uptime, use the uptime
# command. To show the users logged into the system, use the who​command. Print a blank
# line after the output of each command.
# Example output:
# 1. Show disk usage.
# 2. Show system uptime.
# 3. Show the users logged into the system.
# What would you like to do? (Enter q to quit.) 2
# 14:59:08 up 3 days, 7:36, 7 users, load average: 0.13, 0.22, 0.33
# 1. Show disk usage.
# 2. Show system uptime.
# 3. Show the users logged into the system.
# What would you like to do? (Enter q to quit.) 4
# Invalid option.
# 1. Show disk usage.
# 2. Show system uptime.
# 3. Show the users logged into the system.
# What would you like to do? (Enter q to quit.) q
# Goodbye!

function e3() {
        while true
        do
                echo "1. Show disk usage."
                echo "2. Show system uptime.="
                echo "3. Show the users logged into the system."
                read -p "What would you like to do? (Enter q to quit.) " action
                case $action in
                        1)
                                df -h
                                echo
                                ;;
                        2)
                                uptime
                                echo
                                ;;
                        3)
                                whoami
                                echo
                                ;;
                        "q")
                                echo "Goodbye!"
                                break
                                ;;
                        *)
                                echo "Invalid option"
                                ;;
                        esac
        done
}


function run() {
        if [ $# -ne 1 ]
        then
                echo "No exercise selected!"
                read -p "Please pass in the exercise number[1-${TOTAL_ALLOWED_EXERCISE}]: "  EXERCISE_NUMBER
        else
                EXERCISE_NUMBER=$1
    fi
        run_exercise $EXERCISE_NUMBER
}

function run_exercise() {
        if [ "$1" -le "$TOTAL_ALLOWED_EXERCISE" ]
                then 
                        FUNCTION_NAME="e$1"
                        $FUNCTION_NAME
    else
        echo "Wrong exercise number passed: "$1
    fi
}
run $@
Add a comment
Know the answer?
Add Answer to:
Exercise 1: Write a shell script that loops through the /etc/passwd file one line at a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a shell script to solve the following business problem. Create a file called whoislogin.out...

    Write a shell script to solve the following business problem. Create a file called whoislogin.out that contains the output of the who command, then append to the whois.out file, the output of the date and finger commands. Your script should prompt to pick any users from the list of users currently logged onto the system. Use the variable concept to search for the specific user from those displayed on the screen coming from the who is.out file created. Show a...

  • Write a script using the bash shell to add new users to the system. The script...

    Write a script using the bash shell to add new users to the system. The script should read user information from a comma-delimited file (each field should be separated by a comma). The filename should be passed to the script as a command-line argument. Be sure the script contains a usage clause. The input file should contain the user information with one user per line. As the system administrator, you should determine the information necessary for each user to properly...

  • 1. Write the command to set the following permissions on a script file with the following...

    1. Write the command to set the following permissions on a script file with the following name; myscript.sh Owner: full access; read, write and execute Group: read and execute Other: no access 2. Create a new file with the name "myfirstscript.sh". Write a new script that performs the following tasks in sequence; Display the usernames of currently logged in users; sorted from a-z HINT: You will need to filter the output of the whocommand Display a list of usernames who...

  • Problem 1 Write a BASH script to create a user account from the Linux system on...

    Problem 1 Write a BASH script to create a user account from the Linux system on which your script is run. The script should process two positional parameters. First positional parameter is supposed to be a string with a user name (e.g., user_name) Second positional parameter is supposed to be a string with a user password (e.g., user_password) In your script: Check if two positional parameters were passed to your script when it was invoked If NOT, print an appropriate...

  • Using linux Write one-line New Tab ands that determine and print the following desired output: 1....

    Using linux Write one-line New Tab ands that determine and print the following desired output: 1. The total word count in files file1.txt, file2.txt, and file3.txt combined (assume the three files exist in the current directory) 2. How many user accounts are not allowed to log in to the system. You need to calculate the number of users in file /etc/passwd that include /nologin on their line. 3. The name of the 6 largest files in/usr/bin , while suppressing any...

  • Objective : Write a C Shell script which copies all files(*.java and *.class) from your home dire...

    Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output:                                                    << CS Directory Analysis >>      Date:   ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files:   0 files Plain text files:   10 files File have read permissions: 3 files File have...

  • Hello, please help with the following, you're tasked with writing a shell script named “rpsm.sh”....

    Hello, please help with the following, you're tasked with writing a shell script named “rpsm.sh”. The script reports and prints (to stdout) selected storage management information. Think of it as “RePort Storage Management”. The easy way to describe what the script needs to do is to look at what it should display in the case where you provide incorrect parameters, or in the case where you provide “-h” for help: Usage: ./rpsm.sh <options>< directory> Reports selected information about specified directory...

  • 8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollment file that will be specified on the command line ii. If the file does no...

    8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollment file that will be specified on the command line ii. If the file does not exist, is a directory, or there are more or less than one parameters provided, display an appropriate error/usage message and exit gracefully Display the number of a specified major who are taking a given class iii. The following is a sample...

  • The first script is validate.sh. This is a simple form validation script that will be used...

    The first script is validate.sh. This is a simple form validation script that will be used to verify the inputs given. Normally, this would be done to validate input from a website or another program before entry into a database or other record storage. In this case, we will keep it simple and only focus on the input validation step. In particular, the script should prompt the user for four values: first name, last name, zip code, and email address....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT