Part 2 - Linux Shell Scripting - Worth 5 points. - NOT using editors, create a s
ID: 3725868 • Letter: P
Question
Part 2 - Linux Shell Scripting - Worth 5 points. - NOT using editors, create a sh file named: program2.sh - Set this *.sh file to: chmod 755 *.sh - NOT using editors, append your name, current date and time to a NEW file called results.txt - NOT using editors, append to the *.sh file, all the commands needed so that when you execute your sh file, this file will produce the following: How: - Ask the user to enter a number. - Verify that the number is between 1 and 100. - If the number is not between 1 and 100, then keep asking the user to enter a number until it is valid. - Use a for loop from 1 to the value entered by the user - Sum the results of calculating each number mod 5 - After the for loop ends, display the sum of all previous results and append it to the results.txt file as follows Sum of numbers is xxx
Explanation / Answer
Following is the shell command and explanation:
Use touch for create a file using terminal: touch program2.sh
chmod is used to change the file permissions: chmod 755 program2.sh
755 = drwxr-xr-x
4 = read
2 = write
1 = execute
so , 7 = rwx, 5 = re
append your name, current date and time to a NEW file called results.txt:
create file using cat and then append the name in it,then append the date in the file:
cat>results.txt
date>>results.txt
first write the command cat>>results.txt then enter this script:
read num
if [ $num -ge 100 ]
then
read num
else
for num1 in $(seq 1 $num);
do
x=`expr $num1 % 5`
sum=`expr $sum + $x`
done
fi
echo "sum is $sum">>results.txt
You shold type this in terminal step by step:
touch program2.sh
chmod 755 program2.sh
cat>results.txt
date>>results.txt
cat>>results.txt
read num
if [ $num -ge 100 ]
then
read num
else
for num1 in $(seq 1 $num);
do
x=`expr $num1 % 5`
sum=`expr $sum + $x`
done
fi
echo "sum is $sum">>results.txt