Bash Shell Scripting|File descriptors|If-then-else
Video Title: Bash Shell Scripting|File descriptors|If-then-else
Hey guys welcome back to the channel and we’ll be continuing with our shell scripting course and in this video i wanted to cover file descriptors and some comparison operators like if then else uh file descriptors so we’ll be covering basically three file descriptors which is zero one and two
Uh standard in standard out and standard error and these are basically called redirection file descriptors or redirection redirection descriptors and you would mostly use them for redirecting input output and error to basically different devices files and things like that so let’s first see example of file descriptors and
Then we’ll go into the comparison operators so yeah let’s get started so like i told you there are three file descriptors which we’ll be looking at zero one and two so zero is standard in one is standard out and 2 is standard error so suppose if i do something like cad file
Does not exist so i’m getting this ugly error but what if i don’t want to get this ugly error right so suppose if you’re running some commands in your shell script and your shell script throws out this ugly error so you don’t want your end
User to see this right so what i can do is i can redirect all the errors to say a device called dev null which is like a black hole device in linux so now if i do that you can see i didn’t get any ugly errors on the
Screen so what i’ve done is basically redirected my staff all the errors or standard error to a null device right when you i suppose i mean i have a couple of files over here file one and file two and file name one i don’t know why
It’s there so let’s get rid of it all right so let’s get out this file file one so this has hello and file two has world all right so what exactly i mean when you do a cat right so cad file one so what actually i’m doing is
Basically i mean or the linux basically behind the scene what it’s doing it’s doing something like ampersand one and file one but this is i mean either you can do it this way or simply cad command takes care of all the uh basically file descriptions right all right so
Another thing is you can also power parse i mean pass a file as an input to cat right and then you can say output that particular thing to another file so let me show you what i mean so what i can do is i want to basically take input
From say etc pass wd file and i want to put that in a file name called file 3. let’s run this and let’s get out file three if it has created and you can see so what i actually did so i passed this file etc pass wd as input to cat command
And then i sent out all my standard output to a file called file three so that is why my file three has all the information which are there in my atc passwd file all right one more thing i wanted to show you so sometimes you would actually need
Or you’ll be in a situation where you want to print something on the standard out which is your say my terminal and you would also want to redirect it to a file right so in that case what you can use is a command called t so suppose i want say etc passwd
File i want to get this out as well as i want to send it to another file say file 4 in my current directory so let’s run this so you can see this has actually printed out all the stuff and if we have done everything right we
Would have a file 4 over here and if we do a cat on file 4 you can see it has the same content as my tc passwd file right so this is all about file descriptors and this is the only use and the i mean the most file descriptor which you’re going to
Use is either standard out or standard error so because standard in is mostly coming from your keyboard so you would and sometimes from a file but that’s very rare and you would actually not need that but standard out and standard error is something you are going to use on a
Very regular basis in your shell script because your shell script might throw some error which you don’t want your end users to see and then you probably have to redirect those errors to say uh null device right or some or some other file where you want
Suppose you have a log file and you want to send all those errors to a log file so you can basically use your file descriptors all right moving on so now we are going to talk about comparisons so comparison operator basically if then else so basically depending upon your if
Conditions you make some decisions and then you put them in your shell script right so i mean if then else or comparison operator in general have evolved a lot in linux i mean there are multiple ways to do it like i’ll show you some say you can do like if say one
Greater than two right and then you can just simply press enter and you’ll get this prompt and now you can start writing your script say then suppose i say echo hello one else echo hello 2 and then i want to close this so 5 so you see i get this ugly ugly error
Which is basically related to some bash so what we can do is basically we can just redirect this ugly error to say devnull and now run this script again so since this condition is false because 1 is not greater than 2 so this is i mean i have put it in code
But this is actually evaluating the exact numbers right another way if you don’t want to put these in quote is basically to do something like this sorry about that guys is my someone sending me a text all right so this is another way you go ahead and do it and
In that case you actually don’t even need this and this will run without error so you can see uh this is basically numerical comparison what we did earlier was also numerical comparison but that was an old way in fact even this is also not being done today so i’ll talk about the latest
Trend which is there in bash currently and for that we are going to actually uh know for that we are not going to write a bash script i will write a bash script for the next topic which i want to cover which is basically uh some file operations or file comparisons right
So the new ways basically you give these two square brackets and you define your condition within this square bracket right there was an inbuilt command also called test which was used earlier i mean i also used to use earlier say somewhere around three four years back but i think this is also
Depicted i mean it works but mostly people you won’t see using this because i mean this is actually very convenient and there are a lot of operations which are not supported in test or single square brackets so single square bracket was also there but there were a lot of operation like
I think multiple comparisons within the bracket were not supported and something like that that is why they were i mean deprecated and now the new trend is actually to use double square brackets so that is what we are going to follow all right so now let’s talk about some file comparisons
So by file comparisons i mean uh or rather file operations i mean so suppose you have a file so in a script if you want to check whether the file exists and whether it’s a regular file right so let’s do that so we are going to create a script say
Check file dot sh and let’s quickly define our shebang bin bash and now what i’m going to do is uh if double bracket hyphen e and i would say etc pass wd bracket closed then echo password file exists else echo or echo file not found and fi
So you don’t forget to close your if with the file alright so let’s save this let’s give it execution permission and now let’s run this so you can see it says file exists because etc passed wd file exist now if we modify this And say put something like file not found save this and run this again you can see this is saying file not found all right so pretty simple right and we did a simple file check that file exist uh we can also check within the square bracket itself whether this is a regular file
So because i mean it can be a directory it can be a character file right things like that so we are checking whether it’s a regular file or not rswd and it still says file access because edc pass wd is a regular file all right now we can also check for directory and
Let’s do it inside this particular condition itself and to check for directory basically what you need to do is so and an operator basically it will evaluate all the conditions and if any condition fails it will fail the script right so for to check for a particular file
To be a directory we do hyphen d and slash atc let’s just check slash edc right and don’t forget these spaces these i mean bash is very sensitive to spaces let’s run this again and password file exists because that is a directory but what if we do this instead of pass wd
What do you think it will run let’s check you can say it failed because one condition failed so file not found simple as that now we’ll create a script basically for command line parameters so suppose or suppose there’s i mean there is some script that takes command line parameter
And you don’t provide command line parameters so that script should technically fail right so that is what we are going to do so we will create a file test command dot sh let’s quickly define the shebang dash and now what we are going to do is basically
If so to test if i mean the user has provided a command line parameter or not the switch is basically hyphen z which checks whether the string is null or not so this is basically checking for the string to be null or not so if hyphen z dollar one
So if dollar one is null and in that case what we are going to do is echo please provide uh parameter and exit else what we are going to do is echo or else echo let’s just say parameter meter provided and let’s just echo the parameter dollar one
And five all right so let’s save this don’t forget to give it execution permission test command and now let’s run this without a parameter so without the parameter it failed telling me that i need to provide some parameter and let’s provide it my name and parameter provided
And the parameter is static all right so this is working fine right so this is what you basically i mean this is the way you actually check for any particular variable any particular input whether it’s null or not or whether user has provided it or not
So yeah so i think we covered everything we covered file descriptors and we covered if then else comparisons and we made decision based on those comparisons cool so yeah this is all for this video guys this is all i wanted to cover please feel free to comment if you have
Any questions and also let me know if there’s any specific thing which you wanted me to cover and i didn’t i’ll be happy to cover that as well all right so please do subscribe to the channel before leaving and thank you for watching
-
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
The if "1>2" line is just complete nonsense and together with your explanation, it indicates that you lack the basic knowledge of shell command line parsing and syntax rules.
Scary that you teach before learning.
Very very useful. Thanks brother