Bash Scripting for Beginners: Complete Guide to Getting Started - Exit Codes (Part 6)

Поділитися
Вставка
  • Опубліковано 4 лис 2024

КОМЕНТАРІ • 63

  • @Ekte_Sjakie-tr2qb
    @Ekte_Sjakie-tr2qb Рік тому +28

    These are just the best tutorials of anything Linux available on UA-cam.
    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!!

  • @ВикторВоеводов
    @ВикторВоеводов Місяць тому

    Greeting from Russia. I really enjoyed it.

  • @Starlight-vs2ot
    @Starlight-vs2ot Рік тому +13

    Excellent series! I am a newbie but your tutorials are easy to understand. Thank you Sir!

  • @PmK-i7h
    @PmK-i7h 28 днів тому

    You sir are an excellent teacher! This series is great, thank you for making it. :)

  • @AyalaStudios
    @AyalaStudios 6 місяців тому +1

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

  • @JorgeEscobarMX
    @JorgeEscobarMX 5 місяців тому

    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.

  • @AdriaanBlom-fn5dk
    @AdriaanBlom-fn5dk Рік тому +10

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

  • @kellingc
    @kellingc 4 місяці тому +1

    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.

  • @geekytutorials5745
    @geekytutorials5745 Рік тому +2

    Thanks man I am learning this tutorial on android in termux but your videos are helpful 👍

  • @kazitanim007
    @kazitanim007 Рік тому +2

    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.

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

    Amazing vids! Thanks for breaking things down so simply for the little guy!

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

    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.

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

    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.

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

    Your cadence/pacing matches "Amon Tobin - Nova" really nicely.

  • @stephontreadwell1664
    @stephontreadwell1664 8 місяців тому +2

    Sheesh! This was great. I appreciate you and your teaching style!

  • @Yukitocyborg
    @Yukitocyborg Рік тому +2

    Just commenting so youtube knows this is important!

  • @3vil8unny
    @3vil8unny Місяць тому

    love this series thank oyu

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

    Great teaching style. It feels like I am in the classroom :)

  • @brave.circassian77
    @brave.circassian77 3 місяці тому

    best coach. love from circassia!

  • @AlbusRegis
    @AlbusRegis Рік тому +11

    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"

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

    Very lucid and great lesson

  • @AnimeKing-m2t
    @AnimeKing-m2t 3 місяці тому

    Yo make AMAZING videos Jay !!

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

    Best linux tuts i;ve seen thanks!

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

    Super handy stuff here. I'll be using this for an upcoming script. Thanks!

  • @NEXUSSEC
    @NEXUSSEC 6 місяців тому +1

    Tnx captain❤

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

    Excellent, I wanted to take over the world but I will just take over my computer using this

  • @albertovelazquez-ns7um
    @albertovelazquez-ns7um 4 місяці тому

    Just great!! Thanks!!!

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

    Learned exit code and if statements

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

    Dziękujemy.

  • @anubhavgautam6586
    @anubhavgautam6586 8 місяців тому

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

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

    These were great

  • @ger1991mess
    @ger1991mess 8 місяців тому

    Awesome!!! Thanks

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

      it is faster to say atsee then etcetera, this is why people do this

  • @OinSonOfGloin
    @OinSonOfGloin 10 місяців тому +5

    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.

  • @barkhadibraahim1023
    @barkhadibraahim1023 6 місяців тому

    thanks so much

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

    You gotta show us how to make some beats on FL Studio too :P
    Jokes aside great series!!

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

    Thanks

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

    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 11 місяців тому

    Wonderful

  • @danielkotzer636
    @danielkotzer636 7 місяців тому

    did you get the hardware acceleration working on proot ubuntu?

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

    great video :)

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

    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 Рік тому +2

      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 Рік тому +1

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

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

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

      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 Рік тому +2

      @@cognoscibilis Don't forget:
      '255': Exit status out of range

  • @kooners6961
    @kooners6961 6 місяців тому

    Mine keeps saying htop failed to install, despite installing sucessfully.

  • @MaheshwarReddy-h2h
    @MaheshwarReddy-h2h Рік тому

    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 .

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

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

    ❤❤❤❤❤

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

    👍

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

    ☯🙏

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

    what is the difference between "which" and "command -v"? You used later one in the previous video, here you're using "which"

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

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

  • @wheezybackports6444
    @wheezybackports6444 Рік тому +2

    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.