In: Computer Science
⦁ 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.
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.