Question

This assignment will assess your ability to write bash scripts. Drop​ ​down​ ​to​ ​the​ ​root​ ​user​...

This assignment will assess your ability to write bash scripts.

  • Drop​ ​down​ ​to​ ​the​ ​root​ ​user​ ​using​ ​the​ ​command​ ​sudo su -.​
  • Write a bash script named /root/exam02.sh that does the following:
    • Takes exactly 1 argument.
    • If the number of arguments is not 1:
      • Print "Exiting!" and exit with return code 40.
    • If the number of arguments is exactly 1:
      • Print the first argument as per the example below.

Example output for your reference:

The script should have the following output when run with 0 or 2+ arguments:

# /root/exam02.sh
Exiting!
# echo ${?}
40

# /root/exam02.sh testarg1 testarg2
Exiting!
# echo ${?}
40

The script should have the following output when run with exactly 1 argument:

# /root/exam02.sh testarg1
Arg1: testarg1

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. Go to the root directory

HomeworkLib@Chegg-PC:~$ sudo su
[sudo] password for HomeworkLib:

root@Chegg-PC:/home/HomeworkLib# cd

root@Chegg-PC:~# pwd
/root

  1. Write script using vi editor

root@Chegg-PC:~# vi /root/exam01.sh

~

~

"~/exam02.sh" 1 line, 1 character

  1. Script:

#!/bin/bash

if [ $# -ne 1 ]
then
   echo "Exiting!"
   exit 40
elif [ $# -eq 1 ]
then
   echo "Arg1:" $1
fi

  1. Run and Output:

root@Chegg-PC:~# sh /root/exam02.sh
Exiting!
root@Chegg-PC:~# echo $?
40
root@Chegg-PC:~# sh /root/exam02.sh testarg1 testarg2
Exiting!
root@Chegg-PC:~# echo $?
40
root@Chegg-PC:~# sh /root/exam02.sh testarg1
Arg1: testarg1

Add a comment
Know the answer?
Add Answer to:
This assignment will assess your ability to write bash scripts. Drop​ ​down​ ​to​ ​the​ ​root​ ​user​...
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
  • 1. Write 4 simple shell scripts. Each question will ask you to write one or more...

    1. Write 4 simple shell scripts. Each question will ask you to write one or more command lines to solve a problem. Put the command(s) from each problem into a file named problem1.sh, problem2.sh Note that if you want your script to be interpreted by bash, each file should start with the line: #!/bin/bash and will have one (or more, as needed) commands following it. To execute each script in bash, so you can determine if it is working or...

  • Programs/scripts 13. Write a bash shell script that displays the date by using the shell command...

    Programs/scripts 13. Write a bash shell script that displays the date by using the shell command date. 14. Write a command line program that takes a number and a command as arguments. Call that command that number of times. For example, if your program name is "test" test 3 dir <shows directory> dir <shows directory> dir <shows directory>

  • PYTHON 4. While Loops (25%) You will be writing a "shell" application, it will be on...

    PYTHON 4. While Loops (25%) You will be writing a "shell" application, it will be on a loop, taking in commands from the user. The command syntax is as follows command> <arga» <arg1> arg n> The commands you are to implement: Command Description Prints all arguments on the same line, with a space between each. Treats all arguments as floating-point numbers, and prints the average. MUST print an error if given invalid arguments Finds the index of arg1 in arg0....

  • In Bash Write two scripts, 'backup.sh' and 'restore.sh' which do the following: backup.sh accepts a list...

    In Bash Write two scripts, 'backup.sh' and 'restore.sh' which do the following: backup.sh accepts a list of arguments which indicate the name o the output file and a list of files that should be backed up. Will take all of the files provided oher than the first and create a tar-ball(a tarred, gzipped file) with the name provided in the first argument. restore.sh Accepts a single filename of a tar-ball created by the script above and restores the files from...

  • 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...

  • LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command...

    LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command line arguments and does NOT use the read command. I will give you no credit if your bash script includes a read command. Also, you need to have at least one for..in loop in your code even if there may be other ways to do this problem. The script will go through a list of entries in the current directory (do not process subdirectories):...

  • Write a bash script question.sh that accepts one command line argument which is supposed to be...

    Write a bash script question.sh that accepts one command line argument which is supposed to be a positive integer n. The script should print all odd integers from 1 through n. Write a C or C++ program question.c(pp) to read from stdin as many integers as there are available and then print the squares of all these integers. You should test your code by “./question.sh <n> | ./question” assuming the compiled C/C++ program is question. See the following for a...

  • Write a Bash script called hello that uses command line arguments to allow the user to...

    Write a Bash script called hello that uses command line arguments to allow the user to put two strings after the command name, when the script is being executed. These strings should represent a first and last name. The script should then write out a greeting to the user that includes the first and last name. Here is an example of how the script might work (the first line represents what the user types to launch the script): [user@HAL] hello...

  • In this assignment, you will demonstrate your ability to write simple shell scripts. This is a...

    In this assignment, you will demonstrate your ability to write simple shell scripts. This is a cumulative assignment that will challenge you to pull together a variety of lessons from throughout the course. 2 The Assignment Every now and then, I find myself with a large number of files that have inappropriate extensions (the set of characters in the file name after the last :') that need to be changed. For example, a complicated C++ program, developed by someone on...

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