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
Assignment 2 Create a Java program called UserNames that reads the user's first and last name...
Assignment 2 Create a Java program called UserNames that reads the user's first and last name (separately), then prints a string composed of the first 4 letters of the user's last name, followed by the first letter of the user's first name, followed by a random number in the range of 10 to 99 (inclusive). You can assume the first name is at least one letter long and the last name is at least 4 letters. After doing this once,...
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...
Use Rstudio to create a script by following the instruction below. I've created the first question...
Use Rstudio to create a script by following the instruction below. I've created the first question code but I can not continue it further. Thank you. ## Write a function called `make_introduction` that takes in two arguments: name, and age. ## This function should return a string value that says something like "Hello, my name is {name}, and I'm ## {age} years old". make_introduction <- function(name, age){ print(paste("Hello, my name is", name, "and I'm", age, "years old")) } ## Create...
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...
Use a Switch statement.A switch statement is just another way of writing an IF statement. The...
Use a Switch statement.A switch statement is just another way of writing an IF statement. The user will enter f, s, j, or r from the keyboard for freshman, sophomore, junior, or senior. Use character data type for c++. Then you can say something like: char level ='f'; level=toupper(level); You would need a loop for 1 to 5, to test for f, s, j, r and also an illegal grade level. Use a "z" for the illegal grade level Convert...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT