Simple Bash Scripting Examples (Case Statements)

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

КОМЕНТАРІ • 32

  • @BrickTamlandOfficial
    @BrickTamlandOfficial 2 місяці тому +5

    that first script you made is great and you can adapt it into a menu with various options like to run apt update or update docker containers etc. very cool.

    • @DistroTube
      @DistroTube  2 місяці тому +1

      Glad you found it useful.

  • @elgringomexicano
    @elgringomexicano 2 місяці тому +4

    For anyone else who followed along and ran into the issue with the second script of having no default output for when the user doesn't input any flags, add this to the script after the while loop:
    if (( $OPTIND == 1 )); then
    echo "Hello, $NAME!" # or whatever you want the default option to be
    fi
    And in the first script if you want to make it so the user input matches the case statement case-insensitively (i.e. "Arch" or "arch" etc.) include the line "shopt -s nocasematch" at the top of the script.

  • @anon_y_mousse
    @anon_y_mousse 2 місяці тому +1

    You should also demonstrate shell pattern matching with cases so that you don't have to have a huge pipe of every case where a given distro might be entered, such as *[Dd]istro*.
    Also, the Slackware package manager is indeed unknown, so spot on.

  • @YTit69
    @YTit69 2 місяці тому +1

    @DistroTube Great thing again!
    Propose you to make few videos about how to make useful things in everyday usage. Or some direct situations likes Docker, Ansible, backups, proper linux setup for security

  • @siljrath
    @siljrath 2 місяці тому +7

    Not watched this yet, but:
    The expression on the thumbnail....
    Yes. This is the correct expression,
    for case statements. :)

    • @siljrath
      @siljrath 2 місяці тому +1

      so very looking forward to this one, bumped to head of next playlist :D

    • @koduflower2000
      @koduflower2000 2 місяці тому

      yeah i get that joke :D

  • @Not-THAT-ChrisPratt
    @Not-THAT-ChrisPratt 2 місяці тому +2

    Thank you for this.
    Yet another reason I keep referring ppl to your channel.

  • @pdr.
    @pdr. 2 місяці тому +2

    Couple of notes to help with engagement:
    Normally, local variables are in lower case by convention, i.e. "$distro" and should be quoted when used. This is for safety, and a good habit even when not strictly required.
    Patterns in case statements can be globs, e.g. \*[Mm]int\* or \*buntu\* which will cover a lot of cases.
    You could also use lsb_release -si but I guess that's not quite the same script then.

    • @pdr.
      @pdr. 2 місяці тому +1

      Please ignore the "\"s above, but without them it was interpreting the "*"s as markup for bold.

    • @mats4554
      @mats4554 2 місяці тому

      Is the while loop necessary for the script in the video? I mean can you simply do:
      getopts "aem"
      Case
      ...
      Esac

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

      @@mats4554 Yes, it's necessary. The options are parsed sequentially. It works very much analogously to C's getopt(3).

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

      @@mats4554 Well, actually, technically you're right since only one flag is used at a time, but since it's just example code I think it's good to show the loop like in the more general case.

    • @mats4554
      @mats4554 2 місяці тому

      @@pdr. Can you provide an example where it is necessary, I don't quite get it ...

  • @flipinfin
    @flipinfin 2 місяці тому

    Great explanation very educational. So basically you want to write your scripts as TRUE statements?

  • @AlexDavid-s1p
    @AlexDavid-s1p 2 місяці тому +2

    14 seconds is crazy

  • @halfsourlizard9319
    @halfsourlizard9319 2 місяці тому +1

    `fi` / `esac` were weird choices, even for the time ... `end if` / `end case` seem like they would have made more sense -- and been only trivially more typing.

    • @omairtech6711
      @omairtech6711 Місяць тому

      Glad I am not the only one that thinks this way. Especially considering most other programming languages have it the way you suggest, so our brains get hard wired into expecting them that way. I have been coding in python(and other languages too every now and then) for years and the bash way of doing this feels so weird and out of place for me.

  • @amos660
    @amos660 2 місяці тому +2

    DT!!! what's up,baby

  • @marsdrums6298
    @marsdrums6298 2 місяці тому +1

    So, I'm not familiar with programming language and I see that you used /dev/null which there is a file named null in /dev. Mine had nothing in it. So, either no actual data gets written to that file or it's automatically deleted? I'm half tempted to try this out and see if it will actually write to /dev/null...
    EDIT: Okay, so to answer my question, I recreated the Greetings script and... No, it does not write to the /dev/null file. So, it must just get erased and never seen again or does it actually go to another file somewhere?

    • @DistroTube
      @DistroTube  2 місяці тому +1

      So.../dev/null is a special file on your system. Anything written to it disappears. Poof!! It's gone! :D

  • @halfsourlizard9319
    @halfsourlizard9319 2 місяці тому

    6:38 E&E

  • @ralphwuu
    @ralphwuu 2 місяці тому +1

    Bro's face when there's another scripting language with switch statements (definitely not my face)

  • @MichaelWilliams-lr4mb
    @MichaelWilliams-lr4mb 2 місяці тому +1

    Void Linux got no love in the Favorite Distro script. :(

  • @egidijus6973
    @egidijus6973 2 місяці тому

    "esac" is just "case" backwards.

  • @jacobwerner8533
    @jacobwerner8533 2 місяці тому

    my brain instinctly wants to do c stuff when i do bash. another way i came up to do the last example is:
    #/bin/bash
    echo -n "Enter your name: "
    read NAME
    case $1 in
    a|-a)
    echo "Good morning, $NAME!"
    ;;
    e|-e)
    echo "Good evening, $NAME!"
    ;;
    *)
    echo "Hello $NAME"
    ;;
    esac
    now you can enter flags by letter or with hyphen, and no error message.