Тёмный

Bash Scripting on Linux (The Complete Guide) Class 06 - Exit Codes 

Learn Linux TV
Подписаться 663 тыс.
Просмотров 72 тыс.
50% 1

How do you tell whether or not a task completed successfully? Usually, an error message is a sure sign that something went wrong. But when it comes to Linux, you won't always have that to rely on. In this video, you'll learn all about exit codes, which is how you determine whether or not a task in Linux is successful or if it failed.
Support Learn Linux TV
Note: Commission may be earned for any and all links presented here.
- Get your own cloud server with Akamai Connected Cloud ➜ learnlinux.link/akamai
- Become a channel member here on RU-vid ➜ learnlinux.link/join
- Become a Patron on Patreon ➜ learnlinux.link/patron
- Check out my latest book, Mastering Ubuntu Server 4th Edition ➜ ubuntuserverbook.com
- Affiliate store for Linux compatible hardware/accessories ➜ learnlinux.link/amazon
- Check out the Tiny Pilot KVM for your Homelab ➜ learnlinux.link/tinypilot
Time Codes
00:00 - Intro
00:54 - What are "Exit Codes" in Linux?
01:31 - How does Bash perceive success and failure?
02:16 - Checking the $? variable to determine a command's Exit Code
03:35 - How to interpret the value of Exit Codes in Linux
04:15 - Why it can benefit us to utilize Exit Codes in our Bash Scripts
05:22 - An example Bash Script that utilizes Exit Codes
07:48 - More efficiently utilizing Exit Codes in our Bash Scripts
14:42 - An example script that illustrates why execution order is important in Bash
20:55 - How to manipulate the exit code of a task
23:35 - Another example of task order in Bash being very important
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
Full Courses from Learn Linux TV
• Linux Essentials Certification Workshop ➜ learnlinux.link/lpi-course
• Linux Crash Course series ➜ linux.video/cc
• Learn how to use tmux ➜ linux.video/tmux
• Learn how to use vim ➜ linux.video/vim
• Bash Scripting Series ➜ linux.video/bash
• Proxmox VE Cluster Full Course ➜ linux.video/pve
• Learn Ansible ➜ linux.video/ansible
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.
#linux #linuxtutorial #bash

Опубликовано:

 

4 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 59   
@Ekte_Sjakie-tr2qb
@Ekte_Sjakie-tr2qb Год назад
These are just the best tutorials of anything Linux available on RU-vid. I really like how you let students make mistakes and show what these can lead to. And for anyone that thinks this is tedious, this is the best and really the only way to understand and prevent more serious issues down the line. Thanks for everything Jay!!
@AdriaanBlom-fn5dk
@AdriaanBlom-fn5dk Год назад
(Newby here) For people who also struggle to understand why the exit code isn't being displayed with the "The directory $directory exists" echo output...here is the solution: echo "The directory $directory exists" exit_code=100 else echo "The directory $directory does not exist" exit_code=0 fi echo "Exit code: $exit_code" exit $exit_code In this modified script, the exit code is stored in a variable called exit_code based on the condition that was evaluated. After the appropriate message is displayed, the exit code is displayed using echo, and the script exits with the specified exit code using the exit command. My basic way of understanding it: In order for the exit code to be a part of the echo results...it needs to be defined as a variable within the script and it needs to be echoed along with the script.
@AyalaStudios
@AyalaStudios 2 месяца назад
Phenomenal guidance Jay! I'm sure if you created a structured System Administrator Linux course that is tailored to a specific certification or maybe even just a broad overview of what is covered in many of the certificates available... many people who are looking to become certified to get into a position would be extremely grateful and would EASILY pay for that course. Your guidance is just out of this world when it comes to Linux. You hit every single aspect of what is needed to fully grasp the concept in a very understandable way. THANK YOU!!!!!
@Starlight-vs2ot
@Starlight-vs2ot Год назад
Excellent series! I am a newbie but your tutorials are easy to understand. Thank you Sir!
@JorgeEscobarMX
@JorgeEscobarMX Месяц назад
This is the best video on bash scripting I watched. I love how you teach, very well paced for people like me that don't know about exit codes, and you even got the time to explain how "exit" will not run commands after it and how the exit code is modified by echo commands.
@PaulElander
@PaulElander 25 дней назад
Your videos have been very helpful and are giving me a better understanding and a head start before I delve more into this when I start a Cybersecurity bootcamp next week. Also, the ELS podcast is really cool , interesting and insightful. Of course, I started at episode 0 and now on episode 34. I will be taking your class on LPI after I complete the bootcamp.. Keep up all the great work and producing more content.
@kellingc
@kellingc 15 дней назад
I don't know if you get into this in a later video, but exit codes more broadly defined as the exit condition, not necessarily an error. For example, I wrote a C program to check if the current version of the kernel is the same as the latest one in /boot. I exit the program with a exit code 0 if they are the same, and a 1 if they are not the same. The script checks the exit code to determine if the system needs rebooting or not. Both cases are a normal end, but flag a different condition. This is something I learned back in my MS-DOS days, and I was running a BBS with a mailer front end. The mail would exit with a certain errorlevel to start the BBS at the baud rate the person was connecting with. At 1200 baud, the system would drop with a errorlevel of 12 and the batch file would go to the label that would start the BBS at 1200 baud. 56K would exit at errorlevel 56, etc. The front end had a scheduler, too (like cron) and I'd have the nightly and weekly maintenance set up to exit at like errorlevel 140 and 150 respectively, and have the batch file run the maintenance.. Fun times. Anyway, having a script check the exit code can lead to some really flexible decision automation, and not just flagging errors.
@AlbusRegis
@AlbusRegis Год назад
The fact that exit instantly kills the script can be handy to check that, for example, unless a certain file exists everything else should not run. You may see lines of code like this one on the wild: [[ ! -f /some/file/path ]] && exit 1 Except "&&" everything else is covered in this tutorial series, it means "execute the following command ONLY when the first command returns 0"
@promisenwanno5969
@promisenwanno5969 10 месяцев назад
You are really an excellent teacher, your patience to explain a concept is second to none - the best. Thank you! A kind request however is if you can do a series on ethical hacking. I know it tasking, and tedious but it will help the knowledge easily accessible and grasping.
@androspuddy2
@androspuddy2 Год назад
Amazing vids! Thanks for breaking things down so simply for the little guy!
@geekytutorials5745
@geekytutorials5745 8 месяцев назад
Thanks man I am learning this tutorial on android in termux but your videos are helpful 👍
@jojobobbubble5688
@jojobobbubble5688 Год назад
Super handy stuff here. I'll be using this for an upcoming script. Thanks!
@sumanbhattacharjee8894
@sumanbhattacharjee8894 11 месяцев назад
Great teaching style. It feels like I am in the classroom :)
@kazitanim007
@kazitanim007 Год назад
Hey man, i'v been trying to learn Linux for a long time and tried many videos to follow on youtube. You are the best among all of em. Can you also please make a video of what should we focus to learn minimum stuff to get a job as a linux sys admin ? i believe it will be very helpful to all the viewers of this channel and really appreciate ur work.
@jamesmann7411
@jamesmann7411 6 месяцев назад
Thank you Jay. This course will save me time. I highly recommend the 'Mastering Ubuntu Server' book by Jay laCroix, it is the same easy to follow style as his videos, just easier to refer back to.
@midinerd
@midinerd Год назад
Your cadence/pacing matches "Amon Tobin - Nova" really nicely.
@CoachingSitesThatWork
@CoachingSitesThatWork 10 месяцев назад
Hey good stuff. Thanks for making these videos. They have been helpful for getting me familiar with the syntax and BASH/linux environment. A note - The script at around 20 minutes, with successful exit code of 0 is correct, even though the conditional statement is FALSE. This is because there isn't an error. It's simply a conditional check to direct the flow of commands. And thanks for the handy explanation of forcing exit codes. Good to know. I was wondering if we can put a non-number as the exit code, like a string or text. 🙃
@stephontreadwell1664
@stephontreadwell1664 4 месяца назад
Sheesh! This was great. I appreciate you and your teaching style!
@user-eg6kh2rk3y
@user-eg6kh2rk3y 4 месяца назад
same same
@Yukitocyborg
@Yukitocyborg Год назад
Just commenting so youtube knows this is important!
@skmplanet9591
@skmplanet9591 6 месяцев назад
Very lucid and great lesson
@movealongnothingtosee
@movealongnothingtosee 8 месяцев назад
Best linux tuts i;ve seen thanks!
@albertovelazquez-ns7um
@albertovelazquez-ns7um 16 дней назад
Just great!! Thanks!!!
@OinSonOfGloin
@OinSonOfGloin 6 месяцев назад
I think everybody would benefit from a system where you label a video either beginner, intermediate or expert, this way you do not have to repeat yourself so much. I notice that you may be a little to overly friendly to the user which can use a lot of time.
@MrPoint-tb3kx
@MrPoint-tb3kx 5 месяцев назад
This.
@ger1991mess
@ger1991mess 4 месяца назад
Awesome!!! Thanks
@marcushoutzager6225
@marcushoutzager6225 Год назад
These were great
@marianbieda
@marianbieda 11 месяцев назад
Dziękujemy.
@barkhadibraahim1023
@barkhadibraahim1023 2 месяца назад
thanks so much
@user-hl5bx3wc2l
@user-hl5bx3wc2l 2 месяца назад
Tnx captain❤
@SlideRSB
@SlideRSB Год назад
Checking the exit code directly within the if statement is unnecessary since if statements are driven by exit codes behind the scenes. The test clause of the if statement runs the test command and then decides what to do if the exit code is success or failure.
@batuhanunal9380
@batuhanunal9380 7 месяцев назад
Wonderful
@sussusamogus7831
@sussusamogus7831 Год назад
great video :)
@noweare1
@noweare1 8 месяцев назад
Excellent, I wanted to take over the world but I will just take over my computer using this
@subee128
@subee128 4 месяца назад
Thanks
@stanislasautos3606
@stanislasautos3606 8 месяцев назад
Learned exit code and if statements
@danielkotzer636
@danielkotzer636 3 месяца назад
did you get the hardware acceleration working on proot ubuntu?
@NIMMALAAKHILREDDY
@NIMMALAAKHILREDDY Год назад
Hi @Jay , I was trying to install the package "htop" on mac OS but it throws an error 'Unable to locate a Java runtime'. Is it necessary to install Java? or Am i doing something wrong?
@ShnauzerR
@ShnauzerR 8 месяцев назад
You gotta show us how to make some beats on FL Studio too :P Jokes aside great series!!
@TheClembo
@TheClembo Год назад
Maybe a silly question, but is there a predefined list of 'exit codes' and their meanings available to look at or download? -thanks.
@helloimatapir
@helloimatapir Год назад
You can look them up using the "man pages". E.g., "man ls" and scroll to the bottom and look for "Exit Status".
@cognoscibilis
@cognoscibilis Год назад
For bash: '1': Catch-all for general errors '2': Misuse of shell builtins (according to Bash documentation) '126': Command invoked cannot execute '127': "command not found" '128': Invalid argument to exit '128+n': Fatal error signal "n" '130': Script terminated by Ctrl + C
@ChozoSR388
@ChozoSR388 Год назад
@@cognoscibilis Don't forget: '255': Exit status out of range
@ConChairman
@ConChairman Год назад
Nice. I learned some things watching your video. My only nit is, the /etc directory is pronounced "etcetera", not "et-sea".
@FixItAMIGO
@FixItAMIGO Год назад
it is faster to say atsee then etcetera, this is why people do this
@anubhavgautam6586
@anubhavgautam6586 4 месяца назад
I guess you can use another approach by saving the exit code as a variable and then accessing it at the end of the script like this (correct me if I am wrong): #!/bin/bash directory=/notexist if [ -d $directory ] then exit_code=$? echo "The directory $directory exists." else exit_code=$? echo "The directory $directory does not exist." fi echo "The exit code for this script run is $exit_code."
@lonewolf3069
@lonewolf3069 10 месяцев назад
❤❤❤❤❤
@guilherme5094
@guilherme5094 Год назад
👍
@kooners6961
@kooners6961 2 месяца назад
Mine keeps saying htop failed to install, despite installing sucessfully.
@ivansalloum3924
@ivansalloum3924 Год назад
There is something I don't understand. You declared a variable called package with the htop value but you didn't use these "" or ''. So how will bash interpret a variable with the string without "" or ''. If we put the htop inside "", how will bash interpret the package?
@BolekWu
@BolekWu Год назад
If the string of text doesn't include spaces it's not necessary to use "". Bash will interpret package=htop the same way as package="htop". Try this two commands: echo htop echo "htop" Both will have the same result: htop
@ivansalloum3924
@ivansalloum3924 Год назад
@@BolekWu thanks.
@harinathreddy5365
@harinathreddy5365 Год назад
Hi BolekWu, Actually "(Double Quotes)" and '(Single Quotes)'----> These plays an important role in some scenarios. For Example, If we want to print a name/word-----> In this scenario, Mentioning Single Quotes /Double quotes are not Mandatory. But, If we want to print a simple sentence called (HELLO WORLD)----->In this scenario, Mentioning single Quotes / Double Quotes are Mandatory; because Between Hello and World; Space is available. Here Bash cannot recognise/print the sentence without quotes. ***Here are some simple examples that u can understand easily; . . Example: 1 #Using variable as (name) and value is (bash) name=bash (or) name='bash' (or) name="bash" If we do (echo $name) --------------------------------------------------------------------------------------------------------------------->#Output = bash . . Example: 2 #Using variable as (name) and value is (Hello World) name='Hello World' (or) "Hello World" If we do (echo $name) ------------------------------------------------------------------------------------------------>#Output = Hello World . . #The difference between "(Double Quotes)" and '(Single Quotes)' are: *Single Quotes are used to print the sentence/word. *Double Quotes are used to print the existing variable in the sentence. Example: 3 #Using variable as Script compliment=beautiful script="Hello BolekWu, You have a $compliment beard i've ever seen." If we do (echo $script) ---------------------------------------------------------------->#Output = Hello BolekWu, You have a beautiful beard i've ever seen. Here, In the ex:3 We are using existing variable in the variable. In this case we have to use DOUBLE QUOTES.
@user-gd4tc2ne9n
@user-gd4tc2ne9n Год назад
Very nice session , can someone help me i was trying to use this line in my script but whenever it asks for password it got stuck there and can't continue for other users check . how to get out of it and continue the rest of the commands ? tried -"T" or "timeout" but no luck as timeout is not inbuilt . # su - user1 -c 'sudo -l' We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. Password: thank you .
@Alpha_Sadigh
@Alpha_Sadigh Год назад
☯🙏
@Shanks24696
@Shanks24696 Год назад
10:25
@davidwheeler4329
@davidwheeler4329 Год назад
Hey Jay, you keep doing something which I find annoying. I was watching at the 2:44 mark and the amount of time the command is displayed on the screen is less than a second. I know you type fast, but I read slow. Perhaps you could, in future videos, type out the command so we "old slow farts" can freeze the screen so we can follow along with the command before it's gone again. I've rewound this particular moment in time several times, and on my phone, with its small screen, it's not a fun thing to try because I keep over/under jumping. Please... please... KEEP THE COMMANDS ON THE SCREEN FOR LONGER THAN A SLIVER OF TIME before you press the ENTER key.
@Ca1vema
@Ca1vema 10 месяцев назад
what is the difference between "which" and "command -v"? You used later one in the previous video, here you're using "which"
@wheezybackports6444
@wheezybackports6444 Год назад
If you wanted to here's my way of checking exit codes reliably and easily. check_status() { STATUS=$STATUS If [ $STATUS == 0 ]; then echo "OK" else echo "FAIL $STATUS" fi } sudo apt-get update; STATUS=$? STATUS=$STATUS check_status You can use an argument instead of a variable passed to it if you wanted to.
Далее
EVERYONE Needs to Learn a Little Bit of AWK!
16:29
Просмотров 395 тыс.
Incredible magic 🤯✨
00:53
Просмотров 7 млн
Linux Mint vs... Linux Mint (Debian Edition)
17:43
Просмотров 248 тыс.
Linux Crash Course - awk
16:07
Просмотров 92 тыс.
BASH scripting will change your life
14:18
Просмотров 1 млн
Why Linux is better for (most) developers!
14:59
Просмотров 465 тыс.
Linux Problems
11:39
Просмотров 122 тыс.