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

  Рет қаралды 98,542

Learn Linux TV

Learn Linux TV

Күн бұрын

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

Пікірлер: 92
@whyYUbee
@whyYUbee 9 күн бұрын
These lessons help me so much with my automation projects! I'm so glad I found it on YT. Thank you!
@DevotionToChaos
@DevotionToChaos 3 ай бұрын
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.
@heckyes
@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
@glendubie Жыл бұрын
Thanks for all you do Jay. You are by far one of the best Linux teachers on and off youtube.
@nichtgestalt
@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!
@porcellinoveille
@porcellinoveille 9 ай бұрын
Same situation here. Jay is really clear
@BRBTechTalk
@BRBTechTalk 6 ай бұрын
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.
@LinXnerd
@LinXnerd 5 ай бұрын
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.
@georgeindestructible
@georgeindestructible Жыл бұрын
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.
@randallwimpey9664
@randallwimpey9664 7 ай бұрын
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.
@MrShiffles
@MrShiffles Жыл бұрын
Really enjoying this particular series, and really needed to further brush up on BASH in general...keep up the great work, Jay!
@supeer5605
@supeer5605 10 ай бұрын
thanks i was watching cs50 but got overwhelmed by the bash script and this playlist is helping alot
@Al_L.
@Al_L. 4 ай бұрын
This has been such a great lesson, many thanks!
@noitnettaattention
@noitnettaattention 3 ай бұрын
Just trying to introduce/teach someone to a SIMPLE use of if/else statements, will BLOWWW any novice brain to pieces with an additional and completely unnecessary complex stuff you introduced sir! No one doubts that you are knowledgeable, just you yourself need to learn how to get under the "skin" of any dumbfounded beginner, giving him/her a real chance to grasp and understand the given info, not make them to hate it for life !
@tariqashraf7575
@tariqashraf7575 11 ай бұрын
Such a simple way of explaining complex concepts. Hats off
@BRBTechTalk
@BRBTechTalk 6 ай бұрын
12:46 I have done a few online Linux lessons over the years. One lesson I have adopted was to create a bin folder in your home directory to run scripts from and store scripts you create, to me this just makes sense. If you run a bad script in your ~bin no harm will come to your ~home directory.
@idoubtyourcommitment
@idoubtyourcommitment 8 ай бұрын
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.
@khadijahdolapoadesina1600
@khadijahdolapoadesina1600 4 ай бұрын
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
@fartingfrogs
@fartingfrogs 2 ай бұрын
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!
@junyto
@junyto 26 күн бұрын
Thanks for clarifying the issue of command and $command, it was very confusing.
@salarghaffarian4914
@salarghaffarian4914 3 ай бұрын
Great contents. Thanks for your great efforts on making these valuable contents.
@wilversanchez8275
@wilversanchez8275 Жыл бұрын
Awesome video! Thanks for sharing this content
@cybcmd-64
@cybcmd-64 8 ай бұрын
Great video and examples.
@naciribraimo7967
@naciribraimo7967 8 ай бұрын
Thank you Jay. you are saving my life.
@faridohenegyan9688
@faridohenegyan9688 Ай бұрын
Do you have any tutorials on databases? Would love to see those cos you just drive home key concepts that immediately sticks. An excellent instructor that you are. Kudos!!!!
@noweare1
@noweare1 7 ай бұрын
Thanks, this will help me learn and use more linux commands
@fabiobianchi3104
@fabiobianchi3104 9 ай бұрын
Thanks for the class!!!
@xila8861
@xila8861 4 ай бұрын
Amazing lesson
@Jeff_Seely
@Jeff_Seely 7 ай бұрын
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!
@danielbertram
@danielbertram Жыл бұрын
Amazing class!
@anamesry
@anamesry 3 ай бұрын
thanks teacher for this great lesson I really love it. Thank you a lot
@budragchaakhureltogoo3569
@budragchaakhureltogoo3569 21 күн бұрын
Very useful!
@LinXnerd
@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.
@hussamelshehawy
@hussamelshehawy Жыл бұрын
came to say the same, I though why not to use which instead of command -v as they give the same output.
@abkmf
@abkmf 5 ай бұрын
You're the best at explaining Linux concepts (e.g. Systemd). Ps. commenting from a vanilla Arch distro
@lifelongkingdomskills8208
@lifelongkingdomskills8208 3 ай бұрын
Great Tutorials
@flipinfin
@flipinfin Жыл бұрын
Great video very educational easy to follow along. i just made this script on arch linux. NOTE on Arch Linux based it's sudo pacman -Sy && pacman -S --noconfirm --needed
@johntoliver5747
@johntoliver5747 Жыл бұрын
Thanks!
@YannMetalhead
@YannMetalhead 9 ай бұрын
Great video.
@iGarrettt
@iGarrettt Жыл бұрын
Thank you for making this bash course! It's exactly what I needed. On the subject of man pages, there is also a tool you can install "tldr" (tealdeer is package name in arch) where it works similar to man pages but it will give you a brief description and a couple of color coded examples. An example would be running "tldr test" similar to "man test"
@flinkdeldinky
@flinkdeldinky Жыл бұрын
Thanks! I didn't know about tldr.
@porcellinoveille
@porcellinoveille 9 ай бұрын
Just installed it. Thx.
@nicosilva4750
@nicosilva4750 Жыл бұрын
Really appreciate this. Wanted to see if replacing 'command -v' with 'which' would work (and it does), and discovered that if you replace 'htop' with 'sl' you get something pretty cool. Thanks so much for this!!! Loving this series.
@porcellinoveille
@porcellinoveille 9 ай бұрын
I guess it works also with the type command.
@shintyxd788
@shintyxd788 4 ай бұрын
Yea you do get something pretty cool XD
@jojobobbubble5688
@jojobobbubble5688 Жыл бұрын
Great series. How would I look for available commands if I'm writing a script?
@porcellinoveille
@porcellinoveille 9 ай бұрын
Plus the "command -v" command integrates all source for commands to be tested if they exist. In the first version of the test, using "/usr/bin/" excluded all commands from other directories like "usr/sbin/" etc
@DevOps691
@DevOps691 9 ай бұрын
thanks!
@georgeindestructible
@georgeindestructible Жыл бұрын
I think it's important to note in order to remind people that giving the choice of automated continuation of executions of commands on any script is most always better than just have it run everything with a "dry" -y for a lot of reasons, a flag variable could do the trick with a nested if i assume with the example given from this video at least. This matters most in something really significant so it's not like you will always need to have it (the choice) especially if you want to do something fast (even though having some material saved as templates is great and a good idea to keep around).
@flipinfin
@flipinfin Жыл бұрын
Depends what you're building for others to an extent most commands using sudo needs passwords especially fdisk. Though at the same time if you download a package you don't wanna keep sitting there typing your passwd especially when your compiling from source lol
@user-uq3qh2cy9v
@user-uq3qh2cy9v 2 ай бұрын
hi, what is the symbol following sudo apt update? how do we input that symbol? Thank you.
@vinamrajha1571
@vinamrajha1571 2 ай бұрын
11:25 instead of using ge,gt,lt,le etc simply just use conventional comparison operator i.e.,
@user-ck8xt6hl2k
@user-ck8xt6hl2k 5 ай бұрын
Is the indentation important like it is in python? Will it work for example if between "!" and the evaluated statement there isn't space written?
@longrolstral
@longrolstral 8 ай бұрын
A-ha! I learnt how to get stopped jobs back. Great!
@AlbusRegis
@AlbusRegis Жыл бұрын
If anybody wants to know, the if statement checks wether what goes after the "if" returns 0 as its exit code or something else. It is also possible to use the brackets by themselves and check their return code some other way. If only there was a convinient video on this channel explaining waht return codes are...
@asaskald
@asaskald Жыл бұрын
[[ -f ./06-episode ]] && printf "yep "
@unbekannter_Nutzer
@unbekannter_Nutzer Жыл бұрын
Did you watch "Class 06 - Exit Codes"? (I didn't.)
@josel82
@josel82 Жыл бұрын
I was just asking myself that question. Thanks 👍🏽
@bluuu8684
@bluuu8684 7 ай бұрын
Next video explained what is exit code... but not explaining what you told me. Thanks bro!
@MECHANISMUS
@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'?
@haltutor8014
@haltutor8014 Жыл бұрын
thanks for this lesson, too. why didn't you have to input your password when you ran sudo commands in the script?
@gunnerjoe53
@gunnerjoe53 Жыл бұрын
Hello, Just a note: With update and install on the same line: sudo apt update && sudo apt install -y $command this script would not run the apt install part because of a GPG WARNING in the apt update. Once the GPG WARNING was cleared up the script ran as advertised. It would run fine with commands on separate lines: sudo apt update sudo apt install -y $command Joe
@mago2007
@mago2007 Жыл бұрын
I totally understand the -y after the install command, you don't want to answer [Y/n], but what about the password request? How do you avoid this? I'm missing something
@TheUruz
@TheUruz 5 ай бұрын
how come you specified a sudo operation in the script but you both didn't enter it in the script and either when you launched the script? how can that work? at 19:12
@abdulmuqusit4747
@abdulmuqusit4747 2 ай бұрын
Wouldn't writing 'sudo' int the script at 24:43 will ask user for a password?
@littlefrank90
@littlefrank90 Жыл бұрын
Btw if you need to remember all these weird syntaxes bash has (very unintuitive to me) you can just use the manual page for bash: man bash It's a very long page, so you can search stuff typing "/stuff" and pressing "n" to search forward and "N" to find the word backwards.
@rohanmehta7553
@rohanmehta7553 Жыл бұрын
Why do we write command=/usr/bin/htop?
@user-xi4jv4wv9f
@user-xi4jv4wv9f 2 ай бұрын
Anyone else having a problem with -f flag? The script written at 13:30 doesn't work for me, and the -f is not highlighted in the editor, using bash version 4.4.20(1).
@biolf1
@biolf1 9 ай бұрын
What about multiple files or software?
@yerranagesh1607
@yerranagesh1607 Ай бұрын
In the code you have used sudo command for installing htop, but it did not prompted for password in my case it is asking for password why? how to automate this.
@deathesther2817
@deathesther2817 6 ай бұрын
how can u able to take yes or no input as user and use as a condition in if else like mynum = " " if [ $mynum == yes ] then echo ... else .... [ $mynum == no ] ...
@michaelajayi3336
@michaelajayi3336 10 ай бұрын
How did you save and exit ?
@tinacole1450
@tinacole1450 5 ай бұрын
trying to install on a Mac ... having issues with htop. I got a java issue but I downloaded it and it still did not work. I've used htop once in a controlled environment for my bioinformatics program. It worked there but. not on my laptop
@henrylemmon6685
@henrylemmon6685 6 ай бұрын
how can you run sudo in a bash script to install programs? I see that it works but I do not understand how it is done with no password prompt?
@user-hg6ce3vq4x
@user-hg6ce3vq4x 7 ай бұрын
how do I get rid of entering the password for the sudo command ? Because it ask me to enter it when I run the bash script
@redwisdome
@redwisdome 6 ай бұрын
such a nice training but its a hard to undrestanding for non-native lang
@fundamentalcoach
@fundamentalcoach Жыл бұрын
hmm the "fg" command does not work in my case... i have paused the classes in between... maybe something got lost in between?
@georgesomaru8973
@georgesomaru8973 5 ай бұрын
I'm using Linux Lite and it does not work for me either.
@ericpenrose3649
@ericpenrose3649 Жыл бұрын
I guess I don't know why shell command language might not be considered a programming language. Its a Turing complete script interpreter. What condition is not met to be worthy of the title? EDIT: I do fully understand the value of drawing that line in the sand for the purposes of this tutorial, but looking into it I can see there is a larger internet argument going on and that is what confused me.
@StvPaterek
@StvPaterek 2 ай бұрын
Had a problem on this one. Forgot the last command and so it didn't start. Went back and put it in, then ran it again expecting it to find it installed and run it. Instead it didn't find it, installed it again, and when it tried to run it it came up with an error. I had already run it the first time manually when it didn't run, and I ran it manually after it installed again, and came up with the error when trying to run again. Not sure why. I did verify it actually did install in the correct place, but for some reason the script seems to be looking somewhere else I guess? EDIT: Must remember to spell the directory usr not user... lol
@guilherme5094
@guilherme5094 Жыл бұрын
👍
@mr_trex9106
@mr_trex9106 Жыл бұрын
Shouldn't you use another variable name instead of $command? Might be confusing to most people.
@georgesomaru8973
@georgesomaru8973 5 ай бұрын
Yes, showing 'command' twice is confusing.
@wokevns
@wokevns 10 ай бұрын
15:27
@tao4mike
@tao4mike Жыл бұрын
I've only used Linux a bit, so this is probably a newb question. How does the script run the sudo install command without requiring you to enter a password to authenticate? Am I missing something?
@devashishchakraborty
@devashishchakraborty Жыл бұрын
The shell doesn't ask for password if you installed a package before in the same session using sudo. Once you close the terminal and reopen, it will ask for password again.
@Alpha_Sadigh
@Alpha_Sadigh Жыл бұрын
☯🙏
@steph291
@steph291 3 ай бұрын
world chaos, he output 4 space instead of 3 when indenting, how rude !
@tophat593
@tophat593 Ай бұрын
Ok, this language is horrible. Teaching if great but my god, it's like something from the dark ages. I'll use it to run python scripts. Why suffer?
@Hardfuckers
@Hardfuckers 7 ай бұрын
If you are running a sudo command in a script, does it not ask you to insert your password? Or was this prompt also skipped by adding the -y?
Bash Scripting on Linux (The Complete Guide) Class 04 - Basic Math
6:07
Bash Scripting on Linux (The Complete Guide) Class 06 - Exit Codes
27:14
Glow Stick Secret (part 2) 😱 #shorts
00:33
Mr DegrEE
Рет қаралды 50 МЛН
Glow Stick Secret 😱 #shorts
00:37
Mr DegrEE
Рет қаралды 133 МЛН
New Linux User: 10 Things I Wish I Knew When I First Started
23:31
Learn Linux TV
Рет қаралды 372 М.
why are switch statements so HECKIN fast?
11:03
Low Level Learning
Рет қаралды 365 М.
Functions and Arrays in Linux | bash shell scripting
21:39
nikkiinit
Рет қаралды 4,8 М.
5 Common Mistakes New Linux Users Often Make
11:49
Learn Linux TV
Рет қаралды 103 М.
Which Linux Mint is Better? Comparing Debian Edition & Standard
10:46
Learn Linux TV
Рет қаралды 84 М.
Beginner's Guide to the Bash Terminal
1:14:37
Joe Collins
Рет қаралды 2,3 МЛН
Bash Scripting Tutorial for Beginners
47:57
freeCodeCamp.org
Рет қаралды 403 М.
10 Linux Terminal Tips and Tricks to Enhance Your Workflow
44:16
Learn Linux TV
Рет қаралды 90 М.