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.
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.
@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
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.
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.
@@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.
`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.
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.
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?
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.
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.
Glad you found it useful.
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.
@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
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.
Not watched this yet, but:
The expression on the thumbnail....
Yes. This is the correct expression,
for case statements. :)
so very looking forward to this one, bumped to head of next playlist :D
yeah i get that joke :D
Thank you for this.
Yet another reason I keep referring ppl to your channel.
Much appreciated!
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.
Please ignore the "\"s above, but without them it was interpreting the "*"s as markup for bold.
Is the while loop necessary for the script in the video? I mean can you simply do:
getopts "aem"
Case
...
Esac
@@mats4554 Yes, it's necessary. The options are parsed sequentially. It works very much analogously to C's getopt(3).
@@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.
@@pdr. Can you provide an example where it is necessary, I don't quite get it ...
Great explanation very educational. So basically you want to write your scripts as TRUE statements?
14 seconds is crazy
`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.
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.
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?
So.../dev/null is a special file on your system. Anything written to it disappears. Poof!! It's gone! :D
6:38 E&E
Bro's face when there's another scripting language with switch statements (definitely not my face)
Void Linux got no love in the Favorite Distro script. :(
"esac" is just "case" backwards.
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.
oops forgot one