Bash Scripting for Beginners: Complete Guide to Getting Started - Case Statements (Part 13)

Поділитися
Вставка
  • Опубліковано 23 гру 2024

КОМЕНТАРІ • 22

  • @deprimarvin5382
    @deprimarvin5382 2 роки тому +27

    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.

  • @paujoan401
    @paujoan401 Рік тому +7

    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

    • @xichen2677
      @xichen2677 Рік тому +1

      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.

    • @paujoan401
      @paujoan401 Рік тому +2

      @@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.

    • @xichen2677
      @xichen2677 Рік тому +4

      @@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!

  • @AlbusRegis
    @AlbusRegis 2 роки тому +4

    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.

  • @udayarpandey3937
    @udayarpandey3937 2 роки тому +12

    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.

  • @mad8ry
    @mad8ry 4 місяці тому +1

    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

  • @LinuxCloud79
    @LinuxCloud79 2 роки тому +7

    I believe that the single semicolon at the end of "read distro" line isn't necessary.

  • @jojobobbubble5688
    @jojobobbubble5688 Рік тому +1

    Great series Jay! Learning lots!! Please keep up the great work!!

  • @k.chriscaldwell4141
    @k.chriscaldwell4141 2 місяці тому

    Thank you.

  • @tbr79
    @tbr79 Рік тому +7

    why is there a ; after the read distro ?

  • @angeltrasvina
    @angeltrasvina 10 місяців тому

    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?

  • @AimanElmalmine
    @AimanElmalmine 6 місяців тому

    thx, i love you

  • @farheenmaheen
    @farheenmaheen Рік тому

    Good Work

  • @Alpha_Sadigh
    @Alpha_Sadigh Рік тому

    ☯🙏
    Perfect!

  • @guilherme5094
    @guilherme5094 2 роки тому

    👍!

  • @cagedtigersteve
    @cagedtigersteve Рік тому +2

    I wish these would go a little faster. I just want to know the syntax nuances...I know how to program.

  • @Leverquin
    @Leverquin 3 місяці тому

    its so weird to see ";;" and not just ";"

  • @lonewolf3069
    @lonewolf3069 Рік тому

    thanks❤