Writing your First Shell Script for Linux Tutorial
Video Title: Writing your First Shell Script for Linux Tutorial
Well this video is part of a series we’re going over the basics of shell scripts in these first couple of weeks of this series and as we’re going to be going over everything go over in this video today more in more detail on future videos but whenever you learn a
New programming language there are four basic things you need to learn how to do there that should be the first things you learn how to do and it’s going to be the majority of your programming those things are display a message on the screen get user input and save it to a
Variable be able to read and write to files so we’re going to go over those few things and a couple other things real quick today um and and then I’ll go over the more detail in future videos so the first thing is writing a message to
The screen so we use the echo command is one option and I’ll hit enter now you don’t necessarily need in all cases to have the quotation marks but it’s a good habit to get into so echo and then whatever you want to display back and again doing this right
Here in the shell seems kind of silly like why would you do that but putting that into a script it makes more sense which we will do in a moment next will be the read command the read command will get user input so I can say read X
And that will I can enter here it’s going to wait for the user to type something and solve this type in Bob and when I hit enter it has now put the string Bob into the variable X so now I can say echo and in shell scripts dollar
Sign indicates a variable so here we’re saying not to echo this literally but to echo out this variable of X which now equals Bob now you can use read in cooperation with echo or you should be able to use dash P to display a message
First so I can say pick a number I’ll hit enter it says pick a number I can press 8 and then when I press enter the variable X now equals 8 so I can echo out that variable so we’ve learned how to display stuff to the screen whoops
Splayed stuff to the screen read user input and even to read user input with a prompting message next we want to be able to read from a file I can cation or we have a file here called names txt and you can see all I have to do is say cat and
That’s short from captain eight and basically you can display one or more files using this command and this is the contents of this txt file if I want to add a name to that file I would say echo John now if I did one greater than symbol and then the name of
The file names it will overwrite that file and replace the entire contents with just the word John but I want to append to the bottom of it so I’m going to use two of the greater than symbols that means put John into this file or whatever the output of this command it
Doesn’t just have to be the echo command the output of whatever command this is take the standard output because there’s also an error output we’ll go over that in future videos as well but take the standard output and append it so two greater than symbols means append to the
End of this file so I hit enter there now I can say cat names dot txt and you can see it has now added G on the end of that list another command is clear and there’s other ways to do that but as far as scripting you use the word clear now
I’m going to use a text editor to have a look at a script that I’ve written on using vim but you can use whatever text Center you like vim Nano Emacs Kate G edit mousepad whatever as long as it’s a text editor and not like a word document
Or Word document editor or office document editor so here we go this is a script that is created which basically incorporates everything we just went over now the first line of any script on a UNIX or unix-like systems such as Linux should be your shebang line regardless of whether it’s a shell
Script a perl script or a Python script it should be and some people leave this out it should be a shebang line is telling your computer your operating system what interpreter to use now we’ve mentioned there’s different shells this is telling us to use the bash shell
Which is the default on most systems so even if your default shell something else this is saying when you run the script use the bash shell if available because there are some differences which I’ll point out one here in a moment first thing I like to do a lot on my
Scripts is clear the screen so we have nice little clear then we’re going to echo out the message hello and then we’re going to display the message please enter your name and we’re going to put whatever the user enters into the variable name then we’re going
To put that name we’re going to append it to the end of our names txt file well clear the screen again then we’ll echo out a hello and the user’s name and then the backslash n when I use the dash eon echo here indicates a newline so this will be on
The next line I could just use to echo commands but I’m just incorporating it into one here it says your name has been add to the list next we’re going to display the list by cattle that file name then I will say goodbye to whatever the person’s name is
And in the sleep command sleeps for two seconds right here is what that’s doing I just like to add that just for a little little bit of a here’s the information now the program is done running once that’s saved you need to make your programs executable so we’ll
Say change mod X and we’ll say the name of your script so now that script is executable now to run the script since it’s in our current folder run do dot slash and the name of our script I’ll go over that more in the future videos but
That slash just means this is in our current directory rather than somewhere else on the computer without that it’s going to try to look for it in a system path which we’ll also talk about in future videos but right now it’s in our current folder the directory we’re in
And if I hit enter it now runs that script it says hello please enter your name my name is Chris I’ll let enter it says hello Chris your name has been added to the list it gives me the list my name is at the N it said goodbye Chris and then
It waited two seconds before exiting the program now as I mentioned we did the shebang line at the top now if you leave that out it’s going to try to use whatever your default shell is which you may or may not be bash and my default shells I’ve mentioned in the previous
Video is Z shells so now I’m currently in Z shell and I’ll show you one difference ah another difference that I may have not mentioned is that the read command with the dash P gives me an error so the read command works different in Z shell so although I use e
Shell as my default shell on my system most the scripts I write I write using bash because that’s the most common used shell out there for desktop systems on Linux and Mac OS I’m not sure what the default shell on something like one of the BSD operating systems is or some
Other operating systems but bash is most common when it comes to Linux and it’s the default shell on Mac OS um so yeah so make sure you have that shebang line it’s rather annoying when people don’t have that usually people are pretty good with shell scripts but then they go to
Python scripts and forget to put that in there it’s just rather annoying anyway that is the basics again we looked at printing to the screen reading user input displaying files and writing to files that is going to be the majority of what you do in any program you’re
Right a lot of what else from that is just like when you read from a file or you read from user input is just filtering that data cutting it up and sorting through it and formatting it certain ways but a majority what you do I just taught you what you’re going to
Do in most your scripts so I thank you for watching be sure to watch the rest of this series there should be an annotation the playlist or annotation in the description of this video for the full playlist I thank you for watching if you enjoyed this video be sure to
LIKE subscribe share and comment and if you really like my videos and one become a supporter you can go to patreon.com/scishow and there should be a link to that in the description as well where you can support me financially and even like $1 a month is
Is helpful I can a lot of people doing that so thanks for watching and as always I hope that you have a great day
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
My instructor uses this video in her curriculum – Nice job dude!!
Well presentesd, thank you
thanks dude, learned something new today…
Good stuff 👍 Thank you 💜
Some say this guy is still listing Text editors . Ffs
Hello, je suis français et découvre ta chaîne , je débute sur Linux et j'ai appris des choses interessantes , ta video est bien car c'est du concret au moins . Merci Have a good day guy.
Saragosse
Thank you.
Hi
My shell script scenario:-
It check bill_status for previous month from Bill_status table .
Bill_status table contain status column with unique value i.e. ( C for completed and Other than C for not completed).
My expectation:- (my script need to run daily till status=C , if we get Status=C then no need to run for remaining day of this month.
And start again same script with next month 1st day to till status=C)
Example: —
If I found status other than C then this script will need to run daily till we get status C .
And If i found status C today i.e. 23/Apr/22 then it would not check status for remaining day of this month. It should Start again with next month first day to till status=C and so on .
Kindly provide suggestion or logic for above expectation
Thanks for sharing. Very informative.
Check this video https://youtu.be/Z5VHabv5gNQ where I have created a program replicator tool which create copies of an original program with expiry date. Another thing that was used was Shell Script Compiler. I find this an awesome use of Bash.
My personal webportal: drravirawat.wordpress.com
My YouTube Channel: https://www.youtube.com/channel/UCePEtbmWDmumjO-kqxveSbg
This was so helpful, thank you very much! 🙂
is this going to be hack my neightbour wifi password becouse its so easy and designed so you can LOL
password that you not do anything you just get slow internet. everything in today is blocked there is no shares that you can read LOL
hey can i have a textbook or a manual book on all the name of the symbol and its function ploz??or where do i find one and whats its called
This is helpful. Thanks
GREAT
can anyone share the exact steps ko connect oracle DB using shell script
great video, really well explained.
Thank you so very much, this helped me start my assignment. THANKS A LOT
Which app is this ?
yes, I enjoy this video
RIP your return button
damn your soo good you dont even tought how to save the file G E N I U S
Present sir
Present sir, Ankit
Present sir
Thank you man
You go way too fast with minimum explanation
Very helpful – thank you! Do you happen to know if I can write a script that would be triggered by a specific key combination (rather than upon start-up)? And is it possible to use scripts to check and report on the status of other apps that are running? In particular I'm making an external button that I'd like to run a script that will tell me if zoom is open, and if zoom is muted. I know mac os does this, but not sure how to accomplish with linux.
SHELL SCRIPTS – https://www.sakthidbtechnology.com/blog_shell_script.php For more details kindly visit this blogs.
very helpful
how many enter keys have you broken?
how would I create a custom command 'mvt' that automatically moves 'trash' files to directory ~/Trash ?
It works even without the chmod command. What is the use of the chmod command?
Easy, straight to the point… All I needed. Thanks!
how do u save that
It can be helpful : http://www.cemreacar.com/blog/linux-shell-scripting
How to save the file and execute after editing with vim command
Dude, you are a legend. thank you so mucn!
Universities focuses on programs and scripts while the students don't understand the language. This video is a lifesaver