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.
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.
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
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.
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.
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
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).
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
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.
Good to know, thanks!
Just a comment, to promote this great author!
Thanks Jay, for your wonderful vids.
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.
Enjoying your classes !
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
how about passing args to function or getting a return ? Is that explained later ?
Thanks for the course.
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......!!!!!!!!!!!!!!
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.
Thank you!
why my script is not running, even i copied each and every command properly. it shows updater_error.log: permission denied
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
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.
cool stuff, thanx
👍Nice!
Best
☯🙏
awesome!
is bash oop :C
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
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).
@@helloimatapir 😎 Cool, Your hired as Tech _ De_Bugger (comments)! We've learned important 1st step syntax that would puzzle most noobs! Thanks 🙏
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