Тёмный

Bash Scripting on Linux (The Complete Guide) Class 04 - Basic Math 

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

Performing math operations in Bash is a bit different than on other platforms, so it may prevent some future confusion if you learn about these things now. And why not? This tutorial is only six minutes long and there's plenty of worse things you can spend your time on, so check out this Bash tutorial and learn how to perform basic math functions!
Brand LPI Linux Essentials Course Available
Now just $44.99 for a limited time! ➜ learnlinux.link/lpi-course
➜ learnlinux.link/lpi-course
Check out the Linux Shop
In the official shop, you'll find Shirts, hats, stickers, bags and more!
➜ merch.learnlinux.tv
5% discount on LPI exam vouchers
After you finish Jay's new course, get 5% off an LPI exam voucher here:
➜ learnlinux.link/lpi-voucher
Become a Patron
Show your support for Learn Linux TV on Patreon and get access to exclusive perks!
➜ learnlinux.link/patron
Become a Channel Member
Show your support for Learn Linux TV here on RU-vid and get access to exclusive perks!
➜ learnlinux.link/member
Mastering Ubuntu Server: 4th Edition
Jay's latest book covers everything you need to know in order to master Ubuntu Server. It's available here:
➜ ubuntuserverbook.com
Linux Gear and Kits
Check out Jay's choice of hardware products, audio/video equipment, and more.
➜ learnlinux.link/amazon
Grab an awesome Pi-powered KVM
Support Learn Linux TV and grab yourself a TinyPilot KVM here:
➜ learnlinux.link/tinypilot
Note: Royalties and/or commission is earned from each of the above links
Individual Sections
00:00 - Intro
01:00 - Math is handled differently in Bash
01:28 - The "evaluate expression" command (expr) in Bash
02:37 - Multiplication in Bash (and how to use an escape symbol)
03:48 - Using variables with math expressions
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
• Enterprise Linux Security Podcast ➜ enterpriselinuxsecurity.show
• The Homelab Show Podcast ➜ thehomelab.show
• Content Ethics ➜ www.learnlinux.tv/content-ethics
• Request Assistance ➜ www.learnlinux.tv/request-ass...
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.
#linux #baschscripting #linuxforbeginners

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

 

6 июн 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 45   
@helloimatapir
@helloimatapir Год назад
"expr" is good to know, but on a modern system, you should be using "$(( ))" for numeric calculations. E.g. $((a + b)). Also, for multiplication you can simply do $((10 * 10)), not needing a double asterisk or escape character.
@nerfherder4284
@nerfherder4284 Год назад
You literally just helped me fix a script I was writing to convert GPU temps from C to F. Thanks! 👍🏻
@gfjffdgjroedsfdkdslmerrelb4357
i am looking for partner who want to learn bash through co opearation
@VitaDani314
@VitaDani314 9 месяцев назад
If I do it this way, it performs that calculation, but it is also treating it as a command
@imnottellingyoumyname3050
@imnottellingyoumyname3050 5 месяцев назад
@@VitaDani314 same. Got to echo it first.
@marianostone2285
@marianostone2285 3 месяца назад
thks bro ​@@imnottellingyoumyname3050
@chiomachukwuma2859
@chiomachukwuma2859 4 месяца назад
I always love your mode of teaching, very simple, clear and straigth forward. Thanks.
@coolmanabc1231
@coolmanabc1231 11 месяцев назад
I hope your business keeps growing. I appreciate you man.
@patrob3885
@patrob3885 Год назад
Thank you for your course, it is absolutely amazing that I can understand you the way you teach thank you !
@seta16
@seta16 9 месяцев назад
10/10 good passed short videos and perfect if someone come from any programming languages to make a reference
@monjurrahman8688
@monjurrahman8688 10 месяцев назад
I am learning many things from your video. wish your good health
@CIDC-4500
@CIDC-4500 9 месяцев назад
learned alot from your previous Vid and this as well. Thanks much
@kawa_kxf4509
@kawa_kxf4509 Месяц назад
Hello from Athens Greece, I watch your channel and I suggest it to all new people that afraid of using Linux OS.
@amitkrmandal9974
@amitkrmandal9974 Месяц назад
Wow Sir Nice . You have Brilliant Knowledge of Linux. I appreciate you Sir . I am Amit From India .
@fabiobianchi3104
@fabiobianchi3104 9 месяцев назад
Great class!!!
@unbekannter_Nutzer
@unbekannter_Nutzer Год назад
Oh, no! `expr` is deprecated - since 199x? Use $((...)) in bash for simple integer calculations: for op in + - \* / % ; do echo $((30 op 10)); done 40 20 300 3 0 Inside the double parens, you needn't mask the asterisk (here it needed to be masked for the for loop). You needn't use $ to dereference value variables (I needed, because it was used for the operator 🐧> v=7 🐧> echo $((v + 4)) 11 You may extend the math portfolio with exponentiation, often seen in shells as a^b, but here it is $((a**b)). You may even perform bitwise OR, AND and XOR: t530:~/dl 🐧> echo $((v | 4)) 7 t530:~/dl 🐧> echo $((v & 4)) 4 t530:~/dl 🐧> echo $((v ^ 4)) 3 For floating point, you need of course, like viesic pointed out, bc (but not for +*/= and reasonable small values). t530:~/dl 🐧> echo $((2**62)) 4611686018427387904 t530:~/dl 🐧> echo "scale=314; 4*a(1);" | bc -l 3.141592653589793238462643383279502884197169399375105820974944592307\ 81640628620899862803482534211706798214808651328230664709384460955058\ 22317253594081284811174502841027019385211055596446229489549303819644\ 28810975665933446128475648233786783165271201909145648566923460348610\ 45432664821339360726024914127372458700660628 Do a performance comparision between `expr FOO` and `echo $((FOO))` in a loop which runs for 1s. On my system, the bash builtin `$((FOO))` is about 200x faster, than the external `expr FOO` call. Performance often doesn't matter, but there is no single advantage in using `expr` - well, maybe it is more portable for shells, which don't have `$((...))` or something similar.
@AlbusRegis
@AlbusRegis Год назад
Old versions of bash are still running around on the most unexpected places, and I agree with you that almost always $(()) is superior
@unbekannter_Nutzer
@unbekannter_Nutzer Год назад
@@AlbusRegis What do you mean by `old`? Do you think an intro to newbies should focus on teaching archaic syntax and styles? In the file /usr/share/doc/bash/NEWS.gz, I find the news for version 2.0, following bash-1.14.7.(my current version is 5.1.16), stating (among other things): t. The $[...] arithmetic expansion syntax is no longer supported, in favor of $((...)). Bash 2.0 seems to be from 2001. So most of its users are already dead. :) Even the good, old `let` command is superior to `expr`, since it can perform ` let b=30**3; echo $b`
@AlbusRegis
@AlbusRegis Год назад
@@unbekannter_Nutzer I have found on the wild servers that sould be on a museum, running DOS era software because the big boss could not be bothered to update the program that "works perfectly fine". Unfortunately you have to be at least aware of deprecated stuff since it is out there and very much half alive.
@unbekannter_Nutzer
@unbekannter_Nutzer Год назад
@@AlbusRegis Well, I don't doubt that some old servers with outdated software is still in use, and for an experienced programmer, it is nice to know, how to handle them. These videos are made in a technical satisfying way, well pronounced, fine equipment with a high, reliable frequency. Often I like to nit-pick on small issues, but here we see a lot of work wasted to the wrong audience. Who ever is already working as admin, supporting outdated machines, is a) very probably already trained b) in a small minority. It's such a pity if beginners learn bad habits about math in the shell in such a beautiful way.
@christianrobert7915
@christianrobert7915 Год назад
Fun things to try: echo {1..30} echo {30..0}
@OraOraOra
@OraOraOra 6 месяцев назад
thanks🙂
@franciscoromogaray3076
@franciscoromogaray3076 4 месяца назад
classic banger ma bro, short but good
@13thravenpurple94
@13thravenpurple94 Год назад
Great video Thank you
@VideosRichy
@VideosRichy 16 дней назад
thank you!!
@leonel_carrizo
@leonel_carrizo Год назад
Thank you!
@kainaatmakhani6550
@kainaatmakhani6550 Год назад
Nice lecture
@barkhadibraahim1023
@barkhadibraahim1023 Месяц назад
thanks jay
@VitaDani314
@VitaDani314 9 месяцев назад
I noticed that if you surround the * with quotation marks, that works as well
@viesic
@viesic Год назад
did not know the "expr" command. For mathematical calculations i`m using "bc" command, like that "echo 30 + 10 | bc"
@zakariabasraoui1580
@zakariabasraoui1580 11 месяцев назад
thnx!
@doomtm5500
@doomtm5500 Месяц назад
!!!!*DOUBT* whenever I create a variable in linux lets say 'myVar' it returns - myVar : command not found..... Can anyone pls tell the solution ??????
@ushousewatch
@ushousewatch 3 месяца назад
Does bash do order of operations
@knoxduder
@knoxduder 2 месяца назад
Great question.
@guilherme5094
@guilherme5094 Год назад
👍!
@doug2794
@doug2794 3 месяца назад
These math operations only seem to work with integers.
@jeffschroeder4805
@jeffschroeder4805 Год назад
Will a division operation that doesn't reduce to an integer generate a float or maybe truncate the result to an integer? Will the expr command work at all with floats?
@AlbusRegis
@AlbusRegis Год назад
It truncates to nearest low integer: 8/3 will output 2 instead of rounding to 3. My advice is that you should not trust bash to handle critical math, it makes good enough aproximations and works well with integers most of the time, but floating point or other number types are finicky at best Edit: expr errors with non integers: expr 8.1 + 1 "expr: non integer argument"
@unbekannter_Nutzer
@unbekannter_Nutzer Год назад
@@AlbusRegis IMHO you can't say, it performs approximations. It's absolute accurate (hope so ;) ) in the range and scope of integer arithmetic, which means, it truncates, yes, predicable. 99/100 is zero and in many mature programming languages, if using int, too (C, Java, C++, ...). expr 7234534365 \* 14734536 + 977 106597507044330617 (in the range up to 2^63-1). But expr 99 / 100 \* 100 will give you a result of 0, since it is evaluated left to right.
@gfjffdgjroedsfdkdslmerrelb4357
i am looking for partner who want to learn bash through co opearation
@jorojo5219
@jorojo5219 Год назад
Follow!
@Alpha_Sadigh
@Alpha_Sadigh Год назад
☯🙏
@murad4622
@murad4622 Год назад
you can also use echo $[100 + 30] # to give you 130
Далее