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

preview_player
Показать описание
You've been learning how to become more functional in Bash throughout this series so far, but your scripts will function much better if you learn how to write functions in Bash. And that's exactly what you'll learn if you check out this video. Functions are super useful in scripts, so don't take this lightly. In fact, check out this video on functions to add additional skills to your toolkit.

*Brand New Udemy Courses Available!*
Check out my new courses on Udemy and take your learning even further!

*Check out the LLTV Shop!*

*Support the Channel*
Show your support for Learn Linux TV and get access to exclusive perks!

*Official Stores and Merchandise*
_Note: Royalties and/or commission is earned from each of the above links_

*Time Codes*
00:00 - Intro
00:43 - Introducing Functions into our "Updater" script
05:39 - Further explanation of functions and why they're useful in Bash
07:25 - An example run of a script that utilizes functions

OTHER BASH SCRIPTING SERIES EPISODES

*Full Courses from Learn Linux TV*

*Linux-related Podcasts*

*Learn Linux TV on the Web*

*Disclaimer*
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.

#ubuntu #devops #devopstutorialsforbeginners
Рекомендации по теме
Комментарии
Автор

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.

AlbusRegis
Автор

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.

rubberduckey
Автор

Just a comment, to promote this great author!

sssrgo
Автор

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.

kangsankim
Автор

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

mahmoudashraf
Автор

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

xaviermalcolm
Автор

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

muhammadalimughal
Автор

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

vinamrajha
Автор

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


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

ibrahimmorketa