Bash Scripting - Variables, Arrays, If-Then-Else

Sdílet
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

Komentáře • 110

  • @secwriteups
    @secwriteups Před 4 lety +23

    The & (ampersand) sign means, you are executing the program in background, so what you did is running sleep and geany at the same time.

  • @wagnerrodrigues6440
    @wagnerrodrigues6440 Před 4 lety +13

    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.

  • @arminlinzbauer
    @arminlinzbauer Před 3 lety +5

    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

  • @user-gi2mn5yf5j
    @user-gi2mn5yf5j Před 4 lety +3

    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 :)

  • @anonpoliticallyincorrect9721

    Thank you, I was looking for a video like this!

  • @med5032
    @med5032 Před 4 lety +16

    > /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.)

  • @ndanielamaha5618
    @ndanielamaha5618 Před rokem +1

    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.

  • @andrewpalm2103
    @andrewpalm2103 Před 4 lety +2

    Good info, Derek. I'm going to look into using rofi in a similar fashion.

  • @chreshcat
    @chreshcat Před 2 lety +1

    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

  • @JonDoe-gi5zf
    @JonDoe-gi5zf Před rokem

    Just got my first Linux cert, your videos were really helpful. 🙏

  • @__-wv1my
    @__-wv1my Před 4 lety +3

    .fr ? Parlez-vous français ? Cool, je ne savais pas. Merci pour la video, c'est très instructif !

  • @arkansawdave7428
    @arkansawdave7428 Před 4 lety +6

    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.

  • @alexandrosvangelatos9979
    @alexandrosvangelatos9979 Před 2 lety +1

    Great video, used it to launch of pdf files I have for label printing. Thanks

  • @sys7emH4cked
    @sys7emH4cked Před rokem +1

    Very helpful tutorial thanks D.T for making this useful contents. 👉👍👈

  •  Před 4 lety +17

    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.

    • @davidh.4944
      @davidh.4944 Před 3 lety +18

      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.

    • @andrewa3216
      @andrewa3216 Před rokem +3

      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

  • @joetheman74
    @joetheman74 Před 3 lety +2

    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.

  • @AnzanHoshinRoshi
    @AnzanHoshinRoshi Před 4 lety +4

    Thank you, Derek. It's always good to see bourne again shell testifying.

  • @seraph_86
    @seraph_86 Před 9 měsíci

    Thank you DT i added this to autostart nvidia-settings --assign CurrentMetaMode="nvidia-auto-select +0+0 {ForceFullCompositionPipeline=On}"

  • @sippstea5453
    @sippstea5453 Před 4 lety +3

    Learnt something today.

  • @benpit5012
    @benpit5012 Před 2 lety +2

    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.

  • @ViniciusProvenzano
    @ViniciusProvenzano Před 4 lety +6

    Hi, I see you are into Arcolinux. I love it, and I’d like to see more content on it from you. Great video!

    • @ishanagarwal475
      @ishanagarwal475 Před 4 lety +5

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

    • @ViniciusProvenzano
      @ViniciusProvenzano Před 4 lety +1

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

  • @laarags
    @laarags Před 2 lety +1

    Thanks for sharing 👍

  • @urugulu1656
    @urugulu1656 Před 4 lety +2

    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

    • @DistroTube
      @DistroTube  Před 4 lety

      Old videos. I haven't played CSGO probably in a year.

  • @sheikhshakilakhtar6844
    @sheikhshakilakhtar6844 Před 4 lety +4

    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.

    • @DistroTube
      @DistroTube  Před 4 lety

      The configuration for xterm is similar to urxvt: you use the .Xresources file.

  • @yiannifunataoldotcom
    @yiannifunataoldotcom Před 3 lety +1

    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

  • @santoshgujar5237
    @santoshgujar5237 Před 2 lety +1

    Thank you, Sir

  • @devon660
    @devon660 Před 4 lety +2

    Before you run your shell scripts always lint them with shellcheck! There are a lot of pitfalls when writing shell scripts.

  • @piotrarturklos
    @piotrarturklos Před 4 lety +1

    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.

  • @bahathir_
    @bahathir_ Před 4 lety +1

    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.

    • @loarto
      @loarto Před 4 lety

      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.

  • @sch8836
    @sch8836 Před 4 lety +4

    Been meaning to learn bash scripting but was just too lazy. But now that DT has uploaded it, I will get to learn it.

  • @themagicrabbit1877
    @themagicrabbit1877 Před 4 lety +7

    Any text editor? Can I use Microsoft Notepad?

    • @DistroTube
      @DistroTube  Před 4 lety +8

      Yes, you can.

    • @themagicrabbit1877
      @themagicrabbit1877 Před 4 lety +1

      @@DistroTube YES!!! Now I just need to figure out how to get it installed on Arch...

    • @themagicrabbit1877
      @themagicrabbit1877 Před 4 lety +2

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

    • @popespalace823
      @popespalace823 Před 4 lety

      Yeah use wine haha 🍷

    • @themagicrabbit1877
      @themagicrabbit1877 Před 4 lety +1

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

  • @kellingc
    @kellingc Před 3 lety

    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.

  • @stylianoslayranos6881
    @stylianoslayranos6881 Před 4 lety +2

    Thank you for uploading another quality video.

  • @SuperCape
    @SuperCape Před 4 lety +1

    How did you get transparent background in Emacs? I WANT IT 😭

    • @RoKishDubbz
      @RoKishDubbz Před 4 lety

      Install a compositor, such as compton. And tweak it up to add transparency

  • @spidertube1000
    @spidertube1000 Před 4 lety +3

    Derek you're my hero

    • @90hijacked
      @90hijacked Před 4 lety

      איזה יופי, אני לא לבד פה

  • @kentjensen4504
    @kentjensen4504 Před 4 lety +3

    This is so cool.

  • @juanpmarinr
    @juanpmarinr Před 4 lety +2

    What WM are you currently using ?

  • @DaveSomething
    @DaveSomething Před 4 lety +8

    I've /bin/bashing the keyboard!
    lame, I know... not as bad as shebang!
    sudo apt purge sudo

    • @toosafelol
      @toosafelol Před 4 lety

      After that just use su and then reinstall sudo

  • @elisium8242
    @elisium8242 Před 4 lety +1

    How i can run my script one time on startup? (I'm on debian stretch)

    • @DistroTube
      @DistroTube  Před 4 lety

      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.

    • @harrytsang1501
      @harrytsang1501 Před 4 lety

      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

    • @jeff-kj3pb
      @jeff-kj3pb Před 4 lety +3

      you could add a line in your ~/.xinitrc file with the line:
      ./myscript.sh &

  • @danielyount9812
    @danielyount9812 Před 4 lety +1

    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.

  • @fossware
    @fossware Před 2 lety

    What's the point of touching the file first if you can go straight to the editor and specify the filename while saving?

  • @maxpolaris99
    @maxpolaris99 Před 2 lety

    & is put in background, why do you want to start these programs in the background?

  • @stereo123
    @stereo123 Před rokem

    Did you create Bobby's world?

  • @linuxterm7622
    @linuxterm7622 Před 4 lety +2

    Right when I was learning bash

  • @shwaybotx
    @shwaybotx Před 3 lety +1

    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.

  • @robertmarder126
    @robertmarder126 Před 4 lety +7

    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.

    • @md.imamulislam7
      @md.imamulislam7 Před 4 lety +3

      Someone running non-POSIX-compliant shell like FISH will run into problems if we leave the shebang line.

    • @robertmarder126
      @robertmarder126 Před 4 lety +1

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

  • @DaveSomething
    @DaveSomething Před 4 lety +1

    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.

  • @arcanelore168
    @arcanelore168 Před 10 měsíci

    Easy!

  • @_yuri
    @_yuri Před 4 lety +2

    henlo

  • @Broly_1
    @Broly_1 Před 4 lety +1

    I really want to learn but man it is not easy as it look.

  • @artadams1
    @artadams1 Před 3 lety +1

    Friends don't let friends use EMACS

  • @kevinklement2621
    @kevinklement2621 Před 4 lety +1

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

    • @DistroTube
      @DistroTube  Před 4 lety

      Hmm. If I add it as an entry to Urban Dictionary, does it become a real word? :D

    • @kevinklement2621
      @kevinklement2621 Před 4 lety +2

      @@DistroTube Yes, but you'll have to come up with a much dirtier meaning.

  • @kentkou1872
    @kentkou1872 Před 4 lety +2

    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.

    • @DistroTube
      @DistroTube  Před 4 lety

      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.

  • @cam2291
    @cam2291 Před 4 lety +7

    TLDW:
    step 1: install gentoo

    • @ciatronic5904
      @ciatronic5904 Před 4 lety +1

      what does tldr mean? talk less draw more?

    • @cam2291
      @cam2291 Před 4 lety +2

      @@ciatronic5904 oops it's tldw fixed it

  • @Cyanwasserstoff
    @Cyanwasserstoff Před 4 lety

    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 ?

    • @jeetadityachatterjee6995
      @jeetadityachatterjee6995 Před 4 lety +1

      Bash is written in c (like most gnu projects) there maybe some pass through

    • @davidh.4944
      @davidh.4944 Před 3 lety

      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.

  • @linuxrant
    @linuxrant Před rokem

    micro!

  • @caremyok
    @caremyok Před 4 lety +1

    mmmm

  • @hademvids
    @hademvids Před 3 lety +1

    Text editor? No, no, no, no, no... echo “~~~” >> .txt

  • @natemaia9237
    @natemaia9237 Před 4 lety

    you forgot 'fi'

  • @scifregizmoguy
    @scifregizmoguy Před 4 lety

    I love you're channel, DT, but this video was painful to watch. Maybe you should have planned a little better.

    • @RM-ff1lm
      @RM-ff1lm Před 4 lety +1

      I disagree. I gained a lot of knowledge from this video.

    • @scifregizmoguy
      @scifregizmoguy Před 4 lety +1

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

    • @RM-ff1lm
      @RM-ff1lm Před 4 lety +1

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

    • @scifregizmoguy
      @scifregizmoguy Před 4 lety +1

      @@RM-ff1lm Fair enough

  • @hoobsug
    @hoobsug Před 4 lety +1

    shouldve stopped listing editors after mentioning geany. electron based editors must die

  • @pepeshopping
    @pepeshopping Před 4 lety +2

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

    • @BreakTheBeat852
      @BreakTheBeat852 Před 4 lety +3

      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