In: Computer Science
Shell script program that asks a number between 5 to 9. If the input is not between 5 and 9, display an error message and ask again.
Output
1
22
333
4444
55555
666666
7777777
88888888
999999999
Hey here is answer to your question.If you liked the answer feel free to upvote. Thanks
#!/bin/bash
echo "Enter a number : "
read rows
if [ "$rows" -ge 9 ]; then
echo "Invalid number"
exit
fi
if [ "$rows" -le 5 ]; then
echo "Invalid number"
exit
fi
number=1
for((i=1; i<=rows; i++))
do
for((j=1; j<=rows - i; j++))
do
echo -n " "
done
number=$i
k=1
for((j=1; j<=2*i - 1; j++))
do
if [ $j -lt $i ];
then
echo -n "$number "
elif [ $j -eq $i ];
then
echo -n "$number "
else
echo -n "$number "
fi
done
echo
done
this is a bash script run it using.
bash sccript_filename
Nm #!/bin/bash echo "Enter a number : " read rows if [ "$rows" -ge 9 ]; then echo "Invalid number" exit 4 + ONO Fi 14 16 if [ "$rows" -le 5 ]; then echo "Invalid number" exit fi number=1 for((i=1; i<=rows; i++)) do for((j=1; j<=rows - i; j++)) do echo - - - done number=$i k=1 for((j=1; j<=2*i - 1; j++)) do if [ $j -lt $i ]; then echo -n "$number" elif [ $j -eq $i ]; then echo -n "$number" else echo -n "$number" 33 35 done input Output Success #stdin #stdout Os 4292KB Enter a number : 1 2 2 2 33333 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6