Question: I need help with this assignment. The part with the number is working well but is not creating the results.txt file. - program2.sh should append
I need help with this assignment. The part with the number is working well but is not creating the results.txt file.
- program2.sh should append your name and the current date and time to a NEW file called results.txt -program2.sh should then do the following: How: - Ask the user to enter a number. - Verify that the number is between 1 and 50, inclusive. - If the number is not between 1 and 50, then keep asking the user to enter a number until it is valid. - Use a loop from 1 to the value entered by the user - Sum the results of all the included numbers, but do not include any output yet. - After the loop ends, display the sum and append it to the results.txt file on a new line as follows Sum of numbers is xxx
This is the code:
#!/bin/bash
###WELCOME#####
#Storing name and date in variables
name="Mike"
date=$(date)
#inserting name and date variables into results.txt file
echo "Name: $name" > results.txt
echo "Date:$date" >> results.txt
#ASking the user to enter a number
echo -n "Enter numnber : "
read n
#checking for the between 1 and 50
while [[ $n -le 1 || $n -ge 51 ]]
do
#uf number is not between 1 and 50, asking the user to enter the number again
echo -n "Enter numnber between 1-50 : "
read n
done
#calculating the sum of n numbers
sum=0
for (( i=1; i<=$n; i++ ))
do
sum=$(( $sum + $i ))
done
#printing the sum to the screen
echo "Sum of numbers is $sum"
#inserting sum results to the file
echo "Sum of numbers is $sum" >> results.txt
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
