So I am not sure if you are handing these scripts in to be marked, but if you are, you will lose marks on the first task as it does not meet the brief:
Quote:
The program can be started with numbers parameter between 1 and 9.
|
At no time do you accept or test for any parameters at the command line, so if I were to run:
Code:
#!/bin/bash
echo "Choose a number between 1-9"
close() {
for((i=1;i<=n;i++))
do
for((k=i;k<=n;k++))
do
echo -ne " ";
done
for((j=1;j<=i;j++))
do
echo -e "$i \c"
done
echo ""
done
}
read n
if [[ n -le 9 && n -ge 1 ]]; then
close
fi
And ran it with an argument:
Code:
$ ./pyramid.sh 5
Choose a number between 1-9
So instead of printing my pyramid up to 5 I get asked again what number I would like to choose.
As a learning exercise, here is a list of things I would mark you down on if grading the work:
1. Script has no help option in case the user does not know what choices there are when running it, ie. like the fact you can pass an argument at the command line
2. Command line arguments are ignored (so does not meet the brief)
3. Question to user and reading of the answer are so far apart that following the code is confusing
4. Users response is not tested at all to see if it is a number, ie. I can enter the letter 'a' and the code simply finishes
5. As with point 4, if the user enters a number outside the range the script simply finishes and you have no message to advise why (even though in a simple script like this it is obvious, it is a good practice to get into)
6. When testing for numbers, like in all your for loops, it is better to use (()) as you can then use standard arithmetic signs like <= or >=
7. 'close' as a function name doesn't seem to make much sense?? Maybe draw_pyramid or write_nums, but something more on what the function does so later when you look at it (maybe 5 years later) you don't have to read all the code to discover that the function does not 'close' anything
8. You go to the trouble of creating a function but then use global variables. Look up the reserved word local
9. No message to advise the script has completed successfully (not always required but might have given you extra marks
)
10. No comments at all throughout code to advise what is happening (again thinking of looking at the code in the future)
On the positive side:
1. Code adequately tests number range
2. Correct data is shown once number provided
As a tip, maybe look into the printf command as it could replace the need for your first internal if (ie. where you print the spaces before printing the first number on the line)
You may wish to apply some of the above to your next task as well