It should be noted that ';;' is the most used operator for the end of a code block, which means: "Stop matching patterns as soon as one is succesful." But there are others: - ';&' = if the pattern matches, the code block of the next choice is executed, too, no matter if the pattern for that choice matches or not - ';;&' = if the pattern matches, the code block of the next matching pattern is executed, too. Nevertheless you have to use one operator at each end of a code block but the very last.
Here is a cleaner solution combining the knowledge from the previous lesson (functions). A function that calls itself it's called recursive function (does the same job as the while loop while keeping all the logic inside the function scope without modifying any external state). function prompt_favorite_distro () { echo 'What is your favorite Linux distro?' echo "1 - Arch" echo "2 - CentOS" echo "3 - Debian" echo "4 - Mint" echo "5 - Ubuntu" echo "6 - Something else..." echo "x - exit" read -r distro; case $distro in 1) echo "Arch is a rolling release";; 2) echo "Centos";; 3) echo "Debian";; 4) echo "Mint";; 5) echo "Ubuntu";; 6) echo "6 - Something else...";; x) echo "Alright then, see ya." && exit 0;; *) echo "$distro is not a valid option. Please select a valid option" && prompt_favorite_distro;; esac } prompt_favorite_distro
Are you sure your script is running the function in a recursive manner? If you run it & choose e.g. 2, it exits. That's not recursive. :). You missed calling the function inside the function to make it really recursive.
@@xichen2677 Here is the recursion. *) echo "$distro is not a valid option. Please select a valid option" && prompt_favorite_distro;; The idea is to only call the prompt if there isn't a valid option. There's no point in keeping prompting the user if he passed a valid option. Here recursion is a tool for error handling rather than for continual iteration.
@@paujoan401 Thank you for the reply! I just realize I misunderstood the expected behaviour of your script. You are absolutely correct! That's a great thought! Wish you a nice day!
Defining the default in case statements is actually very important, even if it is unreachable in theory users will always find that edge case you did not think of, either out of carelessness or of malice.
Jay my brother, can you please make a similar series on: -Nginx -Apache -RAID -Git -Any monitoring server like (Nagios, Zabbix or Prometheus) -Docker and Kubernets ?? It would be great help to all the Linux SysAdmins over the world.
It should be noted that ';;' is the most used operator for the end of a code block, which means: "Stop matching patterns as soon as one is succesful."
But there are others:
- ';&' = if the pattern matches, the code block of the next choice is executed, too, no matter if the pattern for that choice matches or not
- ';;&' = if the pattern matches, the code block of the next matching pattern is executed, too.
Nevertheless you have to use one operator at each end of a code block but the very last.
Here is a cleaner solution combining the knowledge from the previous lesson (functions). A function that calls itself it's called recursive function (does the same job as the while loop while keeping all the logic inside the function scope without modifying any external state).
function prompt_favorite_distro () {
echo 'What is your favorite Linux distro?'
echo "1 - Arch"
echo "2 - CentOS"
echo "3 - Debian"
echo "4 - Mint"
echo "5 - Ubuntu"
echo "6 - Something else..."
echo "x - exit"
read -r distro;
case $distro in
1) echo "Arch is a rolling release";;
2) echo "Centos";;
3) echo "Debian";;
4) echo "Mint";;
5) echo "Ubuntu";;
6) echo "6 - Something else...";;
x) echo "Alright then, see ya." && exit 0;;
*) echo "$distro is not a valid option. Please select a valid option" && prompt_favorite_distro;;
esac
}
prompt_favorite_distro
Are you sure your script is running the function in a recursive manner? If you run it & choose e.g. 2, it exits. That's not recursive. :). You missed calling the function inside the function to make it really recursive.
@@xichen2677 Here is the recursion.
*) echo "$distro is not a valid option. Please select a valid option" && prompt_favorite_distro;;
The idea is to only call the prompt if there isn't a valid option. There's no point in keeping prompting the user if he passed a valid option. Here recursion is a tool for error handling rather than for continual iteration.
@@paujoan401 Thank you for the reply! I just realize I misunderstood the expected behaviour of your script. You are absolutely correct! That's a great thought! Wish you a nice day!
Defining the default in case statements is actually very important, even if it is unreachable in theory users will always find that edge case you did not think of, either out of carelessness or of malice.
Jay my brother, can you please make a similar series on:
-Nginx
-Apache
-RAID
-Git
-Any monitoring server like (Nagios, Zabbix or Prometheus)
-Docker and Kubernets
??
It would be great help to all the Linux SysAdmins over the world.
you don't need to create a variable "finished". You can just assign 1 to the 'distro' variable and it is still going to do the trick
I believe that the single semicolon at the end of "read distro" line isn't necessary.
Great series Jay! Learning lots!! Please keep up the great work!!
Thank you.
why is there a ; after the read distro ?
Hello!
Why did you use upper case letter X in chmod command but in Class 02 - Hello World you used lower case x?
Is it the same then?
thx, i love you
Good Work
☯🙏
Perfect!
👍!
I wish these would go a little faster. I just want to know the syntax nuances...I know how to program.
Then why not just google it?
then just read documentation.
its so weird to see ";;" and not just ";"
thanks❤