Bash Scripting on Linux (The Complete Guide) Class 05 - If Statements

98,385
0
Published 2022-11-28
There are a lot of "Ifs" in life. "If" you'll get the job, "If" you win the lottery, "If" only you knew the answer to all things. But there's no "Ifs" about it, you'll learn all about If Statements in Bash with this video. We'll go through how to set up an if statement, why you'd want to write one in the first place, and some additional bash-related shenanigans!

LPI Linux Essentials Course Available
Check out the new course on Udemy! ➜ learnlinux.link/lpi-course
learnlinux.link/lpi-course

Check out the Linux Shop
In the official shop, you'll find Shirts, hats, stickers, bags and more!
merch.learnlinux.tv/

5% discount on LPI exam vouchers
After you finish Jay's new course, get 5% off an LPI exam voucher here:
learnlinux.link/lpi-voucher

Become a Patron
Show your support for Learn Linux TV on Patreon and get access to exclusive perks!
learnlinux.link/patron

Become a Channel Member
Show your support for Learn Linux TV here on YouTube and get access to exclusive perks!
learnlinux.link/member

Mastering Ubuntu Server: 4th Edition
Jay's latest book covers everything you need to know in order to master Ubuntu Server. It's available here:
ubuntuserverbook.com/

Linux Gear and Kits
Check out Jay's choice of hardware products, audio/video equipment, and more.
learnlinux.link/amazon

Grab an awesome Pi-powered KVM
Support Learn Linux TV and grab yourself a TinyPilot KVM here:
learnlinux.link/tinypilot

Note: Royalties and/or commission is earned from each of the above links

Individual Sections
00:00 - Pre-Intro
00:48 - Intro
01:13 - Understanding "If Statements" in Bash and why they're useful
01:42 - An example of an If Statement within a Bash Script on Linux
04:43 - Marking the script executable and running it
07:19 - Creating an If/Else conditional in Bash
12:07 - Using an If Statement in a Bash Script to check for the existence of a file
14:22 - Further explanation of checking for the existence of files and directories on Linux
15:20 - Using the which command on Linux to check if a command is present
16:19 - Creating a Bash Script to install a package if it's not already installed
21:52 - How to use the apt command inside a Bash Script on Linux
23:02 - Using the -y option with apt to assume yes (bypass/skip prompts)

OTHER BASH SCRIPTING SERIES EPISODES
- Class 01 ➜ Introduction: linux.video/bash1
- Class 02 ➜ Hello World: linux.video/bash2
- Class 03 ➜ Variables: linux.video/bash3
- Class 04 ➜ Math Functions: linux.video/bash4
- Class 05 ➜ If Statements: linux.video/bash5
- Class 06 ➜ Exit Codes: linux.video/bash6
- Class 07 ➜ While Loops: linux.video/bash7
- Class 08 ➜ Universal Update Script: linux.video/bash8
- Class 09 ➜ For Loops: linux.video/bash9
- Class 10 ➜ Where To Store Your Scripts: linux.video/bash10
- Class 11 ➜ Data Streams: linux.video/bash11
- Class 12 ➜ Functions: linux.video/bash12
- Class 13 ➜ Case Statements: linux.video/bash13
- Class 14 ➜ Scheduling Jobs (Part 1): linux.video/bash14
- Class 15 ➜ Scheduling Jobs (Part 2): linux.video/bash15
- Class 16 ➜ Arguments: linux.video/bash16
- Class 17 ➜ Building a Backup Script: linux.video/bash17
- Class 18 ➜ Closing: linux.video/bash18

LEARN SOMETHING NEW
- How to create a bootable flash drive for installing Linux ➜ linux.video/flash-usb
- Understanding Linux permissions ➜ linux.video/perms
- OpenSSH Guide ➜ linux.video/ssh
- LVM Deep-dive ➜ linux.video/lvm
- How to better secure OpenSSH ➜ linux.video/secure-ssh

Official *Learn Linux TV on the Web*
• Main site ➜ www.learnlinux.tv/
• Community ➜ community.learnlinux.tv/

Learn Linux TV provides technical content that will hopefully be helpful to you and teach you something new. However, this content is provided without any warranty (expressed or implied). Learn Linux TV is not responsible for any damages that may arise from any use of this content. The person viewing Learn Linux TV's content is expected to follow their best judgement and to make their best decisions while working with any related technology. Always make sure you have written permission before working with any infrastructure. Also, be sure that you're compliant with all company rules, change control procedures, and local laws.

#commandline #linuxtutorial #bash

All Comments (21)
  • @DevotionToChaos
    You are a legend. Most guides have been too fast for me to truly take in and digest at the same time, but with you, I feel like I can follow your pace and understand the connections that you are making between individual topics and the knowledge you pile on through a single video never has me flustered and feeling dumb. Thanks a lot! I also dig your verbose, reassuring style.
  • @whyYUbee
    These lessons help me so much with my automation projects! I'm so glad I found it on YT. Thank you!
  • @heckyes
    Oh man. Been waiting for this series for a long time and didn't realize it. I feel like I'm about to get super nerdy. It really feels like the difference between a linux user and a linux power user is the ability to write bash scripts to fully take command of the system.
  • @glendubie
    Thanks for all you do Jay. You are by far one of the best Linux teachers on and off youtube.
  • The "command" trick in the end is actually pretty smart because it just tries to instantly tell if the executable exists and passes the result to the IF as a boolean so the IF itself resolves out on its own right on the spot. Pretty cool.
  • @LinXnerd
    I've been experimenting along with your lessons. I found that "if which $command" works in place of "command -v $command". It might be a little less confusing for some.
  • @MrShiffles
    Really enjoying this particular series, and really needed to further brush up on BASH in general...keep up the great work, Jay!
  • @LinXnerd
    I must say: I haven't had such a great instructor since the Professors that I had in high school. I'll never forget Learn Linux TV and you, Jay.
  • This is so well explained and such an invaluable resource. I barely understand Linux at all, and your videos are doing a thorough job of explaining everything without too much repetition, there's just the right amount to help key concepts stick. Thank you, definitely subscribing and saving to show others.
  • @tariqashraf7575
    Such a simple way of explaining complex concepts. Hats off
  • @nichtgestalt
    Damn, I neglected this channel way to long and now that I binge watch this playlist I really regret it! This is maybe one of the most easy to understand Tutorial series of (Bash-) scripting I have ever seen. That's meaning something coming from me, since I'm not natively speaking English (so the man pages are quite hard to understand). Also it is hard for me to wrap my head around scripting and programming in general. So thank you very much for sharing this series for free. I and many other appreciate it very much!
  • this youtube videos are wonderful, well detailed and the fact that he takes his time to explain some things. I learnt some new commands and really understood what they were used for. I felt smart while watching your videos because I understood them easily. i am definitely subscribing
  • @supeer5605
    thanks i was watching cs50 but got overwhelmed by the bash script and this playlist is helping alot
  • @fartingfrogs
    I like to refer the beginning line of a bash script as a hashbang because the hash sign comes first which makes it easier for me to remember! Good video!
  • This guy is great at explaining concepts. The only issue is, is that unless there is an exercise that is used continuously in everyday life, then the lesson will not stick.
  • @Jeff_Seely
    Hello Jay and thanks you for all of your instructional videos. I first started with UNIX in the 80's and had limited exposure to the OS. Around 1993, I grabbed an Idiots guide to Linux and a cd of SUSE Linux which was entirely Bash in xterm ( I don't think there were any of the flavors that had a GUI) and made an SMB server using Samba. I left cold turkey and did not pick up again until post-911 and this time I did get a GUI with Mint and for the next year I prostlized it as being the best distro. Fast fwd to this year and I've picked Linux back up and I will be staying this time as Linux is the best desktop I have ever used. I am not blessed with a good memory and I have to basically start from scratch, watch tutorials, and read. This is all just to say thank you! You are a very gifted instructor my friend. You teach in a way where you don't assume people know something about the subject and it is exactly what I need. And this is a great venue. There isn't some knuckhead who feels the need to compete with the instructor in the classrom, which I find very rude and a distraction. Just pure learning! So thank you for your sacrifice of time, expense, and all that goes with this channel. You certainly didn't have to devote you time to help us but I am glad for this measure of selflessness. I appreciate it and don't take it for granted. I'll be around for more!
  • @BRBTechTalk
    9:45 Kind of how I like my conversations ... if you have to repeat yourself I am no longer interested in talking to you, I am not stupid and my ears work.
  • @MECHANISMUS
    Why use 'command'? It has only two switches: -p and -v. The first just runs the argument as it would run alone by itself and the second equals to 'which'. So in this case here just use the 'which' and be done. Why 'command'?
  • @Al_L.
    This has been such a great lesson, many thanks!