
Bash Scripting - Variables, Arrays, If-Then-Else DistroTube
video description
Date: 2022-03-30
Related videos
Comments and reviews: 10
Med
> /bin/ls -d....
you have an alias with ls. you can run unaliased ls with \ls like you would escape a space or quote.
> &
& and ; and && all behave differently; so, use them wisely.
---
false; echo -yes- # prints -yes--
true; echo -yes- # prints -yes--
false && echo -yes- # does not echo-
true && echo -yes- # prints -yes-
---
& does everything asynchronously.
> ' (single quotes) - (double quotes) are the same.
no, single quotes will not evaluate your variables. double quotes will. (I think there is something else that will be evaluated using double quotes.)
reply
> /bin/ls -d....
you have an alias with ls. you can run unaliased ls with \ls like you would escape a space or quote.
> &
& and ; and && all behave differently; so, use them wisely.
---
false; echo -yes- # prints -yes--
true; echo -yes- # prints -yes--
false && echo -yes- # does not echo-
true && echo -yes- # prints -yes-
---
& does everything asynchronously.
> ' (single quotes) - (double quotes) are the same.
no, single quotes will not evaluate your variables. double quotes will. (I think there is something else that will be evaluated using double quotes.)
reply
Armin
Your reasoning for using the options variable was to avoid having to come in and change a bunch of stuff whenever the directories or files have changed, yet this is exactly what you'll have to do because of your if-statements whenever the number of directories is not equal to 6. A better way of doing this would be to simply check whether or not a subdirectory with the name of $choice exists in $myDir and then open emacs at that location:
if [ -d -$-myDir-$-choice-- ]; then
exec emacsclient -c -$-myDir-$-choice--
else
echo -No such directory-
fi
reply
Your reasoning for using the options variable was to avoid having to come in and change a bunch of stuff whenever the directories or files have changed, yet this is exactly what you'll have to do because of your if-statements whenever the number of directories is not equal to 6. A better way of doing this would be to simply check whether or not a subdirectory with the name of $choice exists in $myDir and then open emacs at that location:
if [ -d -$-myDir-$-choice-- ]; then
exec emacsclient -c -$-myDir-$-choice--
else
echo -No such directory-
fi
reply
Robert
Just FYI, for your shebang line, it's always better to not reference the path directly, so you don't run into the situation you showed here with /bin/bash verses /usr/bin/bash (or even /usr/local/bin/bash as is common on the BSD's)
#!/usr/bin/env bash
/usr/bin/env is so commonly used that no distro dares move it anywhere else.
Of course, what would be even better, is to make your scripts POSIX compliant. Then you can just use #!/bin/sh without worrying what the particular shell is.
reply
Just FYI, for your shebang line, it's always better to not reference the path directly, so you don't run into the situation you showed here with /bin/bash verses /usr/bin/bash (or even /usr/local/bin/bash as is common on the BSD's)
#!/usr/bin/env bash
/usr/bin/env is so commonly used that no distro dares move it anywhere else.
Of course, what would be even better, is to make your scripts POSIX compliant. Then you can just use #!/bin/sh without worrying what the particular shell is.
reply
piotrarturklos
This is awesome, very clear. And you've picked an example project which is a good one for bash, where calling terminal commands is really important to avoid cruft and boilerplate. If you can make another episode, a more technical one with quirks of bash syntax, that would be like a good next step and also very interesting to me personally.
reply
This is awesome, very clear. And you've picked an example project which is a good one for bash, where calling terminal commands is really important to avoid cruft and boilerplate. If you can make another episode, a more technical one with quirks of bash syntax, that would be like a good next step and also very interesting to me personally.
reply
urugulu
im using shellscripts for finding and converting audiofiles around and then removing the source (find in all its glory together with ffmpeg in its respective glory is just to much for me to remember and execute on a day to day basis).
btw i spotted csgo footage on your machine is there anything to expect
reply
im using shellscripts for finding and converting audiofiles around and then removing the source (find in all its glory together with ffmpeg in its respective glory is just to much for me to remember and execute on a day to day basis).
btw i spotted csgo footage on your machine is there anything to expect
reply
kent
I know you are demostrating the if else condition controls.
But at the end 'if else' does not make sense as you are doing exactly the same thing with selected directory names. why not just do this:
exec emacsclent -c $-myDir-$-choice-
Script will be much shoter and cleaner.
reply
I know you are demostrating the if else condition controls.
But at the end 'if else' does not make sense as you are doing exactly the same thing with selected directory names. why not just do this:
exec emacsclent -c $-myDir-$-choice-
Script will be much shoter and cleaner.
reply
j3
You had me at Dmenu scripting I am totally into that. So I used to script a lot to get the most out of my text based mud game without cheating. Since I quit playing games I have missed scripting a lot. I don't know much about bash scripts but this is a breath of fresh air :)
reply
You had me at Dmenu scripting I am totally into that. So I used to script a lot to get the most out of my text based mud game without cheating. Since I quit playing games I have missed scripting a lot. I don't know much about bash scripts but this is a breath of fresh air :)
reply
pepeshopping
Wow...
-Teaching- scripting and -not expecting that to happen-.
What exactly did you expect from launching the command sleep to the background??
(Hint: It returns immediately!).
Find a better teacher...
reply
Wow...
-Teaching- scripting and -not expecting that to happen-.
What exactly did you expect from launching the command sleep to the background??
(Hint: It returns immediately!).
Find a better teacher...
reply
Kre-imir
You are definitely not -on the safe side- by using #!/usr/bin/bash instead of the more normal #!/bin/bash. Also, if you write your scripts POSIX compliant, which you should, use #!/bin/sh. That's being on the safe side.
reply
You are definitely not -on the safe side- by using #!/usr/bin/bash instead of the more normal #!/bin/bash. Also, if you write your scripts POSIX compliant, which you should, use #!/bin/sh. That's being on the safe side.
reply
arkansawdave1974
Haha. I've watched about 25 seconds of this and YES, it exactly what I've been wondering. I like using a terminal and have been doing it for years but I have no real idea how scripting works. Perfect. Thanks.
reply
Haha. I've watched about 25 seconds of this and YES, it exactly what I've been wondering. I like using a terminal and have been doing it for years but I have no real idea how scripting works. Perfect. Thanks.
reply
Add a review, comment
Other channel videos















