Question

In: Computer Science

LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt...

LINUX

In Linux command line write a shell script ex1.sh that uses IF THEN to

Prompt the user to "Enter a number between 1 and 10". (Hint: Use the 'echo' and 'read' commands in the script. See the slide about the 'read' command)

If the number is less than 5, print "The number is less than 5" (Hint: You will read input into a variable; e.g. read NUM. In the IF statement, enclose $NUM in quotes; e.g. "$NUM". Also, remember you want to use the Integer Operators like –lt.)

Run 'cat ex1.sh'

Run 'sh ex1.sh' and enter 3 when prompted

Run 'sh ex1.sh' and enter 5 when prompted

Run 'sh ex1.sh' and enter 7 when prompted

Solutions

Expert Solution

Shell Script: ex1.sh

#!/bin/bash

# Prompting user to enter a number
echo -n " Enter a number between 1 and 10: "

# Reading a number
read NUM

# If condition to check for number that is less than 5
if [ $NUM -lt 5 ]; then

    echo "\n The number is less than 5 \n"

else
  
    # Number entered is greater than 5
    echo "\n The number is greater than or equal to 5 \n"

fi

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:


Related Solutions

Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the...
Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the files from two named directories into a new third directory. Timestamps must be preserved while copying files from one directory into another. Task Requirements Three directory names must be supplied as arguments to your script when it is executed by the user. In the beginning of your script you need to check that the first two directory names provided (e.g. dir1 and dir2) exist...
Create shell scripts, .sh file. Please include a shebang line at the top of the script...
Create shell scripts, .sh file. Please include a shebang line at the top of the script as well as appropriate comments through out. Write a script that plays a simple “guess the number” game with the user. It should select a random integer in the range [0 - 10], ask the user to guess the value, and provide feedback of the form “too high”, “too low”, or “congratulations” as appropriate. After the correct value is guessed, the script should terminate.
Given the Linux shell command descriptions, please write down the corresponding shell commands in a terminal...
Given the Linux shell command descriptions, please write down the corresponding shell commands in a terminal window.             a) list the current directory contents with the long format             b) print the name of the working directory             c) change directory to the root directory             d) change the permissions of file "myfile" (owned by yourself) so that (1)you can read/write, but cannot execute; (2) all other users cannot read/write/execute this file.             e) make a new directory called “mydir”
Perl is a programming language that can be used on Linux. Write a Perl shell script...
Perl is a programming language that can be used on Linux. Write a Perl shell script named phone.pl that prompts the user to enter first or last or any portion of person’s name, so that can be found the appropriate entry in the phone directory file called “phones”. If the user tries to enter name as the argument on the command line, he/she will get a warning message “You need to provide name when prompted by this script!” If the...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input. Convert String (Part 2) Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1. Convert String (Part 3) For the script in...
Introduction Write in C++ at the Linux command line a program that is the same as...
Introduction Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started. You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements....
Write a brief shell script that will take in a specific file name, prompt the user...
Write a brief shell script that will take in a specific file name, prompt the user whether they would like to gzip, bzip2, or xz compress the file. Depending on response, the script then ought to compress the provided file with the corresponding method
Write shell script code that displays the process tree on a Linux machine. The process tree...
Write shell script code that displays the process tree on a Linux machine. The process tree should show the process identifier of each process. Those processes that have the same ancestor should be sorted by process identifier instead of by name. The process tree should show the full names of all threads, if any and available. Whenever the user identifier associated with a process differs from the user identifier associated with the parent of that process, the process tree should...
Write a BASH shell script (see the Important Notes section) that acts as a DOS command...
Write a BASH shell script (see the Important Notes section) that acts as a DOS command interpreter (the user enters the DOS command and the script executes the corresponding Linux command). The script should loop continuously until the user enters the QUIT command Prior to accepting a command, display a prompt containing your first name and the greater than symbol (>) Example prompt: linux> The DOS command, and any arguments, should be stored in variables $command – the DOS command...
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT