Bash Scripting - Variables, Arrays, If-Then-Else
Vložit
- čas přidán 2. 06. 2024
- In this video, I'm going to cover a few of the fundamentals of bash scripting. I will make a quick and dirty bash script as an example.
REFERENCED:
► www.gnu.org/software/bash/
WANT TO SUPPORT THE CHANNEL?
💰 Patreon: / distrotube
💳 Paypal: www.paypal.com/cgi-bin/webscr...
🛍️ Amazon: amzn.to/2RotFFi
👕 Teespring: teespring.com/stores/distrotube
SOCIAL PLATFORMS:
🗨️ Mastodon: mastodon.technology/@distrotube
💬 IRC: irc://freenode #distrotube
👫 Reddit: / distrotube
DT ON THE WEB:
🕸️ Website: distrotube.com/
🌐 Gopherhole: gopher://distro.tube
📁 GitLab: gitlab.com/dwt1
FREE AND OPEN SOURCE SOFTWARE THAT I USE:
📽️ Open Broadcaster Software: obsproject.com/
🎬 Kdenlive: kdenlive.org
🎨 GIMP: www.gimp.org/
🔊 Audacity: www.audacityteam.org/
💻 VirtualBox: www.virtualbox.org/
Your support is very much appreciated. Thanks, guys! - Věda a technologie
The & (ampersand) sign means, you are executing the program in background, so what you did is running sleep and geany at the same time.
Thanks for the video, very nice introduction to scripting! I really enjoy you showing the mistakes and little of how you think while solving them.
Thanks, Wagner!
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
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 :)
Thank you, I was looking for a video like this!
> /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.)
Thank you DT. I'm new to bash and I need to learn it for Bioinformatics and this has been a wonderful introduction to the world of bash.
Good info, Derek. I'm going to look into using rofi in a similar fashion.
Thanks Derek, your video prompted me do some self teaching (which I presume was the aim of the video). Thought I'd share the feedback so you keep your video's coming. Cheers
Just got my first Linux cert, your videos were really helpful. 🙏
.fr ? Parlez-vous français ? Cool, je ne savais pas. Merci pour la video, c'est très instructif !
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.
Great video, used it to launch of pdf files I have for label printing. Thanks
Very helpful tutorial thanks D.T for making this useful contents. 👉👍👈
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.
Umm, _why_ should you make your scripts posix compliant? If you have bash, use bash, and take advantage of all of its advanced features. That's what the shebang is there for-to tell the system which interpreter to use when executing the script.
There's no need to hobble yourself with the limited syntax of posix unless your script needs to be completely portable across multiple, unknown platforms.
I don't know why people always make such a fuss about posix compliance and portability, when most scripts are written to solve a specific task on a specific machine, with an environment the writer knows about.
Besides, you will only very rarely find a linux system, at least, that doesn't have bash installed. And if you are the type of person who finds himself frequently porting scripts around, you're probably also the kind of person who knows what to do if it isn't.
And finally, even posix scripts can't be safely run on unfamiliar hardware without at least a cursory check of the available commands and file paths.
Did you watch the video? He literally did a whereis bash to find out exactly where it is first. He never made it seem like that 100% would always work. What he DID say would always work was /usr/bin/env
For GUI text editors Kate is fantastic. BTW every time I hear shebang I think of William Hung. How many people remember and know what I am talking about.
Thank you, Derek. It's always good to see bourne again shell testifying.
Thank you DT i added this to autostart nvidia-settings --assign CurrentMetaMode="nvidia-auto-select +0+0 {ForceFullCompositionPipeline=On}"
Learnt something today.
Dd if= of=/dev &
&( Ampersand) at the of a line gets command run in background so u dont have to wait for dd to finish
Thanks for all your words of wisdom help advice guidance etc.
Hi, I see you are into Arcolinux. I love it, and I’d like to see more content on it from you. Great video!
Acrolinux is a themed archlinux with an installer it saves u some time in not learning how to write your own config ...
It is not that different from arch whatever goes for arch goes for arcolinux too..
@@ishanagarwal475 You're right. I've already installed Arch, and created my configs. But Arcolinux is the easiest way to get Arch running with some sane configurations. I think that the approach to the problem can be different to achieve the same result. You can go with Arch and install what you need and keep all tidy and minimal, or you can go with Arco and just uninstall most of the stuff you will not use. I think it is interesting that I manages to work very well with both approaches, but when I tried to so a similar path with Manjaro, I just got crazy. By the way, you can install ArcolinuxD, and have a very functional bare minimum Arch installation, in under 10 minutes. From there you can configure as you wish... Take a look here: czcams.com/video/F-FA5swPiX4/video.html
Thanks for sharing 👍
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
Old videos. I haven't played CSGO probably in a year.
Hello, thanks for the video. Would you mind doing a tutorial on configuring xterm? I have come across quite a few on urxvt, but not anything useful on xterm.
The configuration for xterm is similar to urxvt: you use the .Xresources file.
Hey!! I d love to see how we could check if choice was in the list and if so just use choice to launch dired
Thank you, Sir
Before you run your shell scripts always lint them with shellcheck! There are a lot of pitfalls when writing shell scripts.
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.
In GNU Emacs GUI, we can resize the font with
C-x C-+ #Increse font's size
C-x C-- # Decrease font's size
Thank you.
You can usually increase the font in the terminal. st f.ex. uses ctrl+shift+{page up, page down}. problem with using terminal, is that emacs don't control how text is displayed. The terminal does. GUI and terminal are very different.
Been meaning to learn bash scripting but was just too lazy. But now that DT has uploaded it, I will get to learn it.
Any text editor? Can I use Microsoft Notepad?
Yes, you can.
@@DistroTube YES!!! Now I just need to figure out how to get it installed on Arch...
@@secwriteups Oh really? I did not know that. The joke is on me now I suppose. I don't even use notepad at work anymore due to it's lack of syntax highlighting.
Yeah use wine haha 🍷
@Svein Are Karlsen Actually, Notepad got support for Unix line endings in May 2018. It's still one of the least impressive text editors around though.
The & shovesd the comman into the background. So "sleep 3 &" creats a job in the background and 3 seconds later. If you use the jobs command, this will show all the background jobs that running.
Thank you for uploading another quality video.
Thanks! :D
How did you get transparent background in Emacs? I WANT IT 😭
Install a compositor, such as compton. And tweak it up to add transparency
Derek you're my hero
איזה יופי, אני לא לבד פה
This is so cool.
What WM are you currently using ?
Xmonad in this video.
I've /bin/bashing the keyboard!
lame, I know... not as bad as shebang!
sudo apt purge sudo
After that just use su and then reinstall sudo
How i can run my script one time on startup? (I'm on debian stretch)
Will vary depending on what desktop or window manger you run. There is a way to autostart programs in every desktop. Google the solution for what you are running.
Sounds like you would like a systemd service. Search "Create A Custom Systemd Service" for more details.
Once created, enable it to run on startup.
If you want to interact with your script at some point, you can use a tmux session to house the process. e.g.
ExecStart=tmux new-session -s SessionName ScriptFile
ExecStop=tmux kill-session -t SessionName
Then you can use `tmux attach -t SessionName` to interact. Once you are done simply press "Ctrl-B" followed by "D" to detach
you could add a line in your ~/.xinitrc file with the line:
./myscript.sh &
Good gist of bash shell scripting with Emacs. Still use the g(vim) here. Had never jumped into emacs. It does have some cool options.
What's the point of touching the file first if you can go straight to the editor and specify the filename while saving?
& is put in background, why do you want to start these programs in the background?
Did you create Bobby's world?
Right when I was learning bash
Back in the day when you made this useful video you never looked at the camera. This is weird. We'd like you to look at us sometimes! I think your later videos improve on this point.
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.
Someone running non-POSIX-compliant shell like FISH will run into problems if we leave the shebang line.
@@md.imamulislam7 every unix-like system I have ever seen uses a POSIX-compliant shell for /bin/sh regardless of what the user's personal shell is.
speaking of bash... I did write a nifty .bash_profile that gives me information about a system when I login via SSH. things like space on a particular drive, the temp of the cpu, heh I even added the weather in that area.
Nice!
Easy!
henlo
I really want to learn but man it is not easy as it look.
Friends don't let friends use EMACS
Great video, but I'm the annoying guy who has to point out that "ampersign" is not a word. The word you're looking for is "ampersand".
Hmm. If I add it as an entry to Urban Dictionary, does it become a real word? :D
@@DistroTube Yes, but you'll have to come up with a much dirtier meaning.
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.
I hope to do another video continuing where I left off with this script, since I didn't really make the script do what I originally wanted. And yes, choice will be a part of that video if/when I get to it.
TLDW:
step 1: install gentoo
what does tldr mean? talk less draw more?
@@ciatronic5904 oops it's tldw fixed it
The coding feels similar, I am no expert so please excuse my question, but is it possible that the bash is using C++ or a similar programming language ?
Bash is written in c (like most gnu projects) there maybe some pass through
Bash syntax is bash syntax. But the underlying base decends from the original bourne shell and the posix standard, with a good number of other features "borrowed" from ksh, and zsh, and other sources.
Cross-pollination is very common among scripting and programming languages, and you'll find that a lot of them have taken a hefty dose of inspiration from C-family syntax. Not really surprising, since so much of the underlying infrastructure is written in it.
micro!
mmmm
Text editor? No, no, no, no, no... echo “~~~” >> .txt
no, no... cat >> .txt
you forgot 'fi'
I love you're channel, DT, but this video was painful to watch. Maybe you should have planned a little better.
I disagree. I gained a lot of knowledge from this video.
@@RM-ff1lm
It does have a lot of information. I'm referring to the delivery, and the many mistakes he made which could have been misleading for a noob. Misinformation is worse than lack of information. He didn't even know why his scripts were failing.
@@scifregizmoguy I'm sure he did most of that on purpose because he was showing how to correct the problems you run into when you create your own scripts. I wish I had this video when I first started scripting because I had no idea how to fix my mistakes.
@@RM-ff1lm Fair enough
shouldve stopped listing editors after mentioning geany. electron based editors must die
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...
Damn dude chill out he’s just trying to teach us some useful stuff, everyone makes mistakes. I’m guessing you weren’t always as tech savvy as you are now