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

Поділитися
Вставка
  • Опубліковано 15 чер 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!
  • Наука та технологія

КОМЕНТАРІ • 110

  • @secwriteups
    @secwriteups 4 роки тому +22

    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 4 роки тому +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.

  • @med5032
    @med5032 4 роки тому +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.)

  • @arminlinzbauer
    @arminlinzbauer 3 роки тому +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

  • @anonpoliticallyincorrect9721
    @anonpoliticallyincorrect9721 4 роки тому +2

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

  • @JonDoe-gi5zf
    @JonDoe-gi5zf Рік тому

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

  • @user-gi2mn5yf5j
    @user-gi2mn5yf5j 4 роки тому +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 :)

  • @sys7emH4cked
    @sys7emH4cked Рік тому +1

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

  • @andrewpalm2103
    @andrewpalm2103 4 роки тому +2

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

  • @ndanielamaha5618
    @ndanielamaha5618 Рік тому +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.

  • @chreshcat
    @chreshcat 2 роки тому +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

  • @arkansawdave7428
    @arkansawdave7428 4 роки тому +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 2 роки тому +1

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

  • @__-wv1my
    @__-wv1my 4 роки тому +3

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

  •  4 роки тому +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 3 роки тому +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 Рік тому +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

  • @AnzanHoshinRoshi
    @AnzanHoshinRoshi 4 роки тому +4

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

  • @sippstea5453
    @sippstea5453 4 роки тому +3

    Learnt something today.

  • @seraph_86
    @seraph_86 9 місяців тому

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

  • @laarags
    @laarags 2 роки тому +1

    Thanks for sharing 👍

  • @joetheman74
    @joetheman74 4 роки тому +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.

  • @santoshgujar5237
    @santoshgujar5237 2 роки тому +1

    Thank you, Sir

  • @ViniciusProvenzano
    @ViniciusProvenzano 4 роки тому +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 4 роки тому +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 4 роки тому +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: ua-cam.com/video/F-FA5swPiX4/v-deo.html

  • @yiannifunataoldotcom
    @yiannifunataoldotcom 3 роки тому +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

  • @sheikhshakilakhtar6844
    @sheikhshakilakhtar6844 4 роки тому +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  4 роки тому

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

  • @benpit5012
    @benpit5012 2 роки тому +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.

  • @urugulu1656
    @urugulu1656 4 роки тому +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  4 роки тому

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

  • @bahathir_
    @bahathir_ 4 роки тому +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 4 роки тому

      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 4 роки тому +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.

  • @stylianoslayranos6881
    @stylianoslayranos6881 4 роки тому +2

    Thank you for uploading another quality video.

  • @piotrarturklos
    @piotrarturklos 4 роки тому +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.

  • @themagicrabbit1877
    @themagicrabbit1877 4 роки тому +7

    Any text editor? Can I use Microsoft Notepad?

    • @DistroTube
      @DistroTube  4 роки тому +8

      Yes, you can.

    • @themagicrabbit1877
      @themagicrabbit1877 4 роки тому +1

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

    • @themagicrabbit1877
      @themagicrabbit1877 4 роки тому +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 4 роки тому

      Yeah use wine haha 🍷

    • @themagicrabbit1877
      @themagicrabbit1877 4 роки тому +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.

  • @devon660
    @devon660 4 роки тому +2

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

  • @kellingc
    @kellingc 3 роки тому

    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.

  • @spidertube1000
    @spidertube1000 4 роки тому +3

    Derek you're my hero

    • @90hijacked
      @90hijacked 4 роки тому

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

  • @SuperCape
    @SuperCape 4 роки тому +1

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

    • @RoKishDubbz
      @RoKishDubbz 4 роки тому

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

  • @DaveSomething
    @DaveSomething 4 роки тому +8

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

    • @toosafelol
      @toosafelol 4 роки тому

      After that just use su and then reinstall sudo

  • @fossware
    @fossware 2 роки тому

    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 2 роки тому

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

  • @danielyount9812
    @danielyount9812 4 роки тому +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.

  • @stereo123
    @stereo123 Рік тому

    Did you create Bobby's world?

  • @kentjensen4504
    @kentjensen4504 4 роки тому +3

    This is so cool.

  • @juanpmarinr
    @juanpmarinr 4 роки тому +2

    What WM are you currently using ?

  • @elisium8242
    @elisium8242 4 роки тому +1

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

    • @DistroTube
      @DistroTube  4 роки тому

      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 4 роки тому

      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 4 роки тому +3

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

  • @arcanelore168
    @arcanelore168 10 місяців тому

    Easy!

  • @linuxterm7622
    @linuxterm7622 4 роки тому +2

    Right when I was learning bash

  • @DaveSomething
    @DaveSomething 4 роки тому +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.

  • @Broly_1
    @Broly_1 4 роки тому +1

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

  • @artadams1
    @artadams1 3 роки тому +1

    Friends don't let friends use EMACS

  • @shwaybotx
    @shwaybotx 3 роки тому +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.

  • @_yuri
    @_yuri 4 роки тому +2

    henlo

  • @robertmarder126
    @robertmarder126 4 роки тому +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 4 роки тому +3

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

    • @robertmarder126
      @robertmarder126 4 роки тому +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.

  • @kentkou1872
    @kentkou1872 4 роки тому +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  4 роки тому

      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.

  • @kevinklement2621
    @kevinklement2621 4 роки тому +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  4 роки тому

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

    • @kevinklement2621
      @kevinklement2621 4 роки тому +2

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

  • @Cyanwasserstoff
    @Cyanwasserstoff 4 роки тому

    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 4 роки тому +1

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

    • @davidh.4944
      @davidh.4944 3 роки тому

      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.

  • @caremyok
    @caremyok 4 роки тому +1

    mmmm

  • @linuxrant
    @linuxrant Рік тому

    micro!

  • @cam2291
    @cam2291 4 роки тому +7

    TLDW:
    step 1: install gentoo

    • @ciatronic5904
      @ciatronic5904 4 роки тому +1

      what does tldr mean? talk less draw more?

    • @cam2291
      @cam2291 4 роки тому +2

      @@ciatronic5904 oops it's tldw fixed it

  • @natemaia9237
    @natemaia9237 4 роки тому

    you forgot 'fi'

  • @hademvids
    @hademvids 3 роки тому +1

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

    • @denisde4ev
      @denisde4ev 3 роки тому +1

      no, no... cat >> .txt

  • @scifregizmoguy
    @scifregizmoguy 4 роки тому

    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 4 роки тому +1

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

    • @scifregizmoguy
      @scifregizmoguy 4 роки тому +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 4 роки тому +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 4 роки тому +1

      @@RM-ff1lm Fair enough

  • @hoobsug
    @hoobsug 4 роки тому +1

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

  • @pepeshopping
    @pepeshopping 4 роки тому +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 4 роки тому +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