Bash Scripting for Beginners: Complete Guide to Getting Started - Functions (Part 12)

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

КОМЕНТАРІ • 24

  • @AlbusRegis
    @AlbusRegis 2 роки тому +14

    Bash functions are very powerful while creating scripts but have some limitations:
    They can return an exit code with the keyword "return", if you have been following the series you may gess why this is useful.
    They will never return a anything that is not an exit code, this means that if you want a function to populate a variable you will have to get creative with data stream redirections.
    your_var=$(your_function) achieves most things, but it is not even close to being foolproof.

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

      Good to know, thanks!

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

    Just a comment, to promote this great author!

  • @albertjj5980
    @albertjj5980 9 місяців тому +1

    Thanks Jay, for your wonderful vids.

  • @rubberduckey5630
    @rubberduckey5630 2 роки тому +2

    When I'm developing comething that is lengthy I will tend to use functions to break it into smaller pieces based on... well function. That way I can find the part that needs attention quicker. Then I just call call each of them at the end while I'm testing, or quickly disable an entire section that is working fine to cut down of the runtime when I go to test a change I made as long as the part I'm working isn't contingent on it.

  • @MichaelBrown-w1n
    @MichaelBrown-w1n Рік тому +1

    Enjoying your classes !

  • @mad8ry
    @mad8ry 4 місяці тому

    you can update this code further by letting the function does standard output and standard error as well, and pass the command to it as arrgument like this:
    check_exit_status() {
    "$@" >> $logfile 2>> $errorlog
    if [ $? -ne 0 ]
    then
    echo "An error occurred, please check the $errorlog file."
    fi
    }
    in the code you could pass the command to the function like this:
    check_exit_status sudo apt update

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

    how about passing args to function or getting a return ? Is that explained later ?

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

    Thanks for the course.

  • @vinamrajha1571
    @vinamrajha1571 9 місяців тому +1

    so far in this course i've got to know that this course is bare minimum, U guys still need to explore a lot on your own......!!!!!!!!!!!!!!

  • @kangsankim3753
    @kangsankim3753 2 роки тому +2

    Thanks for a great tutorial. I have a question.
    Is there return value or parameters in bash function?
    It seems quite different from function in C language.

  • @Eilay_Yosfan
    @Eilay_Yosfan 5 місяців тому +1

    Thank you!

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

    why my script is not running, even i copied each and every command properly. it shows updater_error.log: permission denied

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

    Hi Jay, thanks for the great tutorial-I'm learning a lot so far; question: in bash does a function always have be declared before its called? Thanks

    • @emoutraspalavras-marloncou4459
      @emoutraspalavras-marloncou4459 Рік тому +2

      I am not Jay, but I can answer: yes. The execution sequence is always from top to bottom. For functions you can either use function keyword or leave it out. You can use () after its name or leave it out. Very versatile syntax.

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

    cool stuff, thanx

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

    👍Nice!

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

    Best

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

    ☯🙏
    awesome!

  • @Leverquin
    @Leverquin 3 місяці тому

    is bash oop :C

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

    hello sir! it is a nice course. my updater function shows permission denied message for the logfile and errorlog variables.
    /usr/local/bin/update: line 16: /var/log/updater.log: Permission denied
    An error occurred, please check the /var/log/updater_errors.log file.
    /usr/local/bin/update: line 18: /var/log/updater.log: Permission denied
    An error occurred, please check the /var/log/updater_errors.log file.
    here is my code.
    #!/bin/bash
    release_file=/etc/os-release
    logfile=/var/log/updater.log
    errorlog=/var/log/updater_errors.log
    check_exit_status() {
    if [ $? -ne 0 ]
    then
    echo "An error occurred, please check the $errorlog file."
    fi
    }
    if grep -q "Ubuntu" $release_file || grep -q "Debian" $release_file
    then
    sudo apt-get update 1>>$logfile 2>>$errorlog
    check_exit_status
    sudo apt-get dist-upgrade -y 1>>$logfile 2>>$errorlog
    check_exit_status
    fi

    • @helloimatapir
      @helloimatapir 2 роки тому +3

      It's because your user does not have access to the files in /var/log. You can either run the script with sudo, change to root, or chown the 2 log files you created (I don't recommend this method).

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

      @@helloimatapir 😎 Cool, Your hired as Tech _ De_Bugger (comments)! We've learned important 1st step syntax that would puzzle most noobs! Thanks 🙏

  • @guy_is_2_shy_to_talk_to_girl
    @guy_is_2_shy_to_talk_to_girl 2 місяці тому +1

    sorry your course is great and i was binge watching it ,but this moto rola add is too much, i dont know ho yt work but iff the add were different i would still be ok