Question

In: Computer Science

⦁   Write a Bash script that prompts for user input and reads a string of text...

⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “.

⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.


  

⦁   Write a short Bas script to display even numbers in a set of 13 to 21 using a WHILE loop technique.

⦁   Re-write your WHILE loop script in previous question using a FOR loop for the same set of numbers of 13 to 21.

Solutions

Expert Solution

1. Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “.

Solution

#!/bin/bash    

# Read the user input     

echo "Enter a string: "  

read input_string

if [ input_string == ' ' ];  

then    

  echo "Enter a not null string: "  

  read input_string

else  

   echo "$input_string"  

fi  

2. Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.

#!/bin/bash  
#Script to print an element of an array with an index of 2 and all elements of array 
  
#declaring the array  
declare -a example_array=( 3 5 13 14  )  
  
#printing the element with index of 2  
echo ${example_array[2]}  
#output is 13

#Printing all the elements  
echo "${example_array[@]}" 
#output is 3 5 13 14

3.Write a short Bas script to display even numbers in a set of 13 to 21 using a WHILE loop technique.

#!/bin/bash  
#While Loop  
  
i=13 
while [ $i -le 21 ]  
do  
rm = $i%=2; 
((i++))
if [[ "$rm" == 0 ]];  
then  
    echo "This is an even Number : $i"  
else
 continue
fi  

done  

4.  Re-write your WHILE loop script in previous question using a FOR loop for the same set of numbers of 13 to 21.

#!/bin/bash  
#for Loop 

for num in {13..21}  
do  
  rm = $num%=2; 
  if [[ "$rm" == 0 ]];  
  then  
    echo "This is an even Number : $i"  
  fi 
done

Hope it will help you, have a great day!!!...

Thank you.


Related Solutions

Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Write a Flask app in which a Python script prompts the user to enter a string...
Write a Flask app in which a Python script prompts the user to enter a string and then that string, as entered, gets displayed on an otherwise blank HTML file, called output.html. Show the Python/Flask code and the html code of the output.html file, one below the other as part of your answer.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a bash script that... create new user ./finalProject user if a new user is to...
Write a bash script that... create new user ./finalProject user if a new user is to be created ask for the new users information and use it when creating the new user add a new printer ./finalProject printer ask anything you need in order to create the new printer (I.e. name) permissions ./finalProject permissions ask what document and permissions the user wants restarting the computer ./finalProject restart
Write a short bash script that takes in two arguments from a user, asks the user...
Write a short bash script that takes in two arguments from a user, asks the user whether they would like to add, subtract, multiply, or divide. Each of these operations must be a function that returns data.
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three...
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three points A, B, and C, namely (xA, yA), (xB, yB), (xC, yC), forming a triangle, storing each in a variable (for a total of 6 variables). The script should then calculate the centroid G = (xG, yG) using xG = xA+xB+xC 3 and yG = yA+yB+yC 3 , storing each in a variable. Finally, the script should print all four points. Sample output: Enter...
Write a MIPS assembly program that prompts the user first for a string, then for a...
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer...
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user...
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT