Question

Bash script with branching and looping Part 1: 1. Create a variable called NAME and set...

Bash script with branching and looping
Part 1:
1. Create a variable called NAME and set it equal to the empty string (NAME=””). Create a variable
called NAME_LENGTH and set it equal to the length of your last name. Output the value of the
NAME variable to show that it is empty.
2. Write a FOR loop that executes as many times as there are letters in your name (use the
NAME_LENGTH variable in your condition). Use a CASE statement inside the loop to add letters
to NAME one at a time to spell out your last name. Write the CASE statement so that each time
the loop executes, a single letter is added to the value of the NAME variable (using the +
operator). Output the value of the NAME variable after each letter is added. The 2 best ways to
write the CASE statement are:
a. Use the possible values of NAME for the cases
i. Example: if your last name is Jones, the cases would be “”,J,Jo,Jon,Jone,Jones
b. Use the possible values of the FOR loop condition variable for the cases
i. Example: if you last name has 5 letters, the cases would be 0,1,2,3,4,5
Part 2:
1. Use an IF statement to check that NAME contains all the letters it should.
a. When the program runs, this should always be true by the time you get to this point, but
it would catch an error if anything went wrong with part 1.
b. You can either check that NAME is equal to your last name or check that the length of
NAME is equal to the value of NAME_LENGTH.
2. If the IF statement is TRUE (meaning that NAME is the correct length), use a WHILE loop to
remove the letters in NAME one at a time. Output NAME after each letter is removed.
a. You can do this using the string manipulations we talked about and the ? wildcard
character. Remember that * matches any number of any characters and ? matches any
single character. You can use the ? to remove one single character using the remove from-the-back string manipulation
3. If the IF statement is FALSE, output “Error: NAME does not contain the correct number of
letters.” and have the program exit.

Sample output:

./build_name.sh
L
Li
Lin
Lind
Lin
Li
L
$

Homework Answers

Answer #1
#!/bin/bash

NAME=""
NAME_LENGTH=7
echo "NAME is: $NAME"
for (( i=0 ; i<${NAME_LENGTH}; i++ ));
do
    case $i in
0)
  NAME=$NAME'J';;
1)
  NAME=$NAME'A';;
2)
  NAME=$NAME'V';;
3)
  NAME=$NAME'V';;
4)
  NAME=$NAME'A';;
5)
  NAME=$NAME'J';;
6)
  NAME=$NAME'I';;
esac
echo "$NAME"
done
if [[ $NAME=="JAVVAJI" ]]; then
    while [[ $NAME != ? ]] #== $?
do
    NAME="${NAME::-1}"
    echo $NAME
done
else
    echo “Error: NAME does not contain the correct number of letters.”
    exit
fi

Output:

Know the answer?
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
Similar Questions
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and...
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and no other characters) and prints 1) the "least" character in the string, where one character is less than another if it occurs earlier in the alphabet (thus 'a' is less than 'C' and both are less than 'y') and 2) the index of the first occurrence of that character. When comparing letters in this problem, ignore case - i.e. 'e' and 'E' should be...
Draw a timing diagram for the first four letters of your last name, given the following...
Draw a timing diagram for the first four letters of your last name, given the following specifications about the RS232 serial communications channel:           Baud rate:     115,200 baud           Data length: 8 bits           Parity:           None           Stop bits:      1 The first three letters of your last name should include at least one upper case letter and one or two lower case letters (If your last name is 3 letters or less, use first name). Indicate the bit time for...
PLEASE WRITE A BASH SCRIPT FOR USE IN JUYPTER NOTEBOOK. Initialize a variable INPUT_FILE that stores...
PLEASE WRITE A BASH SCRIPT FOR USE IN JUYPTER NOTEBOOK. Initialize a variable INPUT_FILE that stores the name of the file 'testdata.csv'. Write an if-else statement in a shell that checks if the input file exists. If the file exists, then display a message "testdata.csv exists," otherwise display a message "testdata.csv does not exist." Initialize an array variable named 'ARR_WORDS' as below: ARR_WORDS=("naresh" "sam" " david" ) Initialize an array variable named 'ARR_COUNTS' of size equal to the length of...
1- Create a new script that is called HW9.m and save it. The script will do...
1- Create a new script that is called HW9.m and save it. The script will do the following. Test your code for each of the cases and upload your script as a .m file. [15 points] a. Create a random integer numbers that goes from 0 to 5 and assign it to a variable y using rand and round commands. Hint: Note that rand function only creates random number between 0 and 1. You need to scale the values to...
Create a C Program called Summer_Heat that meets the following criteria: Lines 1,2: Identifies the name...
Create a C Program called Summer_Heat that meets the following criteria: Lines 1,2: Identifies the name of Program that the Programmer Lines 4,5: Includes the header files to allow input and output and use the system commands Line 7: Includes Declarations comment Line 8: Declares counter as an integer with an initial value of 0 Line 9: Initializes a double array called temperature using a single statement and the following 10 values 78.6 82.1 79.5 75.0 75.4 71.8 73.3 69.5...
Create a function in MIPS using MARS to determine whether a user input string is a...
Create a function in MIPS using MARS to determine whether a user input string is a palindrome or not. Assume that the function is not part of the same program that is calling it. This means it would not have access to your .data segment in the function, so you need to send and receive information from the function itself. The program should be as simple as possible while still using necessary procedures. Follow the instructions below carefully. Instructions: ●...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
WITH JAVA Follow the instructions in the attached to complete Task#2 and submit work along with...
WITH JAVA Follow the instructions in the attached to complete Task#2 and submit work along with screenshots of your output. I have attached the class code for Task#1 that you can use while completing Task#2. Task#1 CODE /** SocSecException exception class */ public class SocSecException extends Exception { public SocSecException(String error) { super("Invalid the social security number, " + error); } } Task #2 Writing Code to Handle an Exception 1. In the main method: a. The main method should...
In C# using the Console App (.NET FRAMEWORK) Create and test a Windows Console application that...
In C# using the Console App (.NET FRAMEWORK) Create and test a Windows Console application that displays the following patterns separately, one after the other. You MUST use nested for loops to generate the patterns, like the example in the PowerPoint slides from this chapter. All asterisks (*) should be displayed by a single statement of the form Console.Write("*"); which causes the asterisks to display side by side. A statement of the form Console.WriteLine(); can be used to move to...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT