Created
July 15, 2020 01:29
-
-
Save mohan43u/4ae51ce5026d5e6745dd5737d1293a18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2020-07-09 16:58:39 +mohan43u-we #ilugc channel is now in restricted mode | |
2020-07-09 16:59:51 +mohan43u-we type '/msg mohan43u-we message' if you want to say something important | |
2020-07-09 17:00:46 +mohan43u-we good evening to all | |
2020-07-09 17:01:06 +mohan43u-we welcome to Day 4 of sh Posix shell session | |
2020-07-09 17:01:46 +mohan43u-we we will continue from where we left yesterday | |
2020-07-09 17:02:09 +mohan43u-we just a rewind, yesterday we looked into redirection, pipes, normal and environment variables | |
2020-07-09 17:03:10 +mohan43u-we our next topic is control structures, like 'if' elif etc., | |
2020-07-09 17:04:08 +mohan43u-we before going into the topic, just want to let you guys how to disable that joined/left messages in the irc chat | |
2020-07-09 17:05:00 +mohan43u-we if you are joining #ilugc through webchat.freenode.net, follow https://pasteboard.co/JgGXsrf.gif this link to disable join/part messages | |
2020-07-09 17:05:09 +mohan43u-we now, coming to the topic | |
2020-07-09 17:05:31 +mohan43u-we control structures | |
2020-07-09 17:06:17 +mohan43u-we remember when I said that every command in linux returns a exitcode | |
2020-07-09 17:06:47 +mohan43u-we we can take steps in our commandline based on this returncode | |
2020-07-09 17:07:34 +mohan43u-we see, in presenter terminal, I executed two commands in single line by seperating them using ';' | |
2020-07-09 17:07:45 +mohan43u-we the first command is a simple 'hi' | |
2020-07-09 17:07:57 +mohan43u-we the second command uses '$?' | |
2020-07-09 17:08:34 +mohan43u-we this $? is a special shell variable (will take about these kind of variables briefly later) which returns the last executed command's exitcode | |
2020-07-09 17:09:47 +mohan43u-we so the two commands executed and returned 'hi' and '0' respectively | |
2020-07-09 17:10:14 +mohan43u-we let see what happens when grep command cannot able to find our given string in a file | |
2020-07-09 17:10:29 +mohan43u-we we already have a file falled output.txt filled with text | |
2020-07-09 17:10:39 +mohan43u-we now, I'm going to use this file for our purpose | |
2020-07-09 17:11:08 +mohan43u-we just executed grep command to search for string hi | |
2020-07-09 17:11:39 +mohan43u-we lets see what exitcode the previous grep command returned | |
2020-07-09 17:12:08 +mohan43u-we the last grep command returned 0 which means success or true in shell language | |
2020-07-09 17:12:32 +mohan43u-we again, I executed another grep command, this time I asked it to search for string 'stupid' | |
2020-07-09 17:12:36 +mohan43u-we in output.txt file | |
2020-07-09 17:13:07 +mohan43u-we this time, grep didn't returned anything as output, because there is no matching line in output.txt which contains string stupid | |
2020-07-09 17:13:22 +mohan43u-we now see the return code of the last executed grep command | |
2020-07-09 17:14:04 +mohan43u-we see, instread of returning success (or true), it returned 1 | |
2020-07-09 17:14:13 +mohan43u-we which means, the last grep command failed | |
2020-07-09 17:14:39 +mohan43u-we now we are going to use this returncode to decide what to do next using 'if' | |
2020-07-09 17:16:08 +mohan43u-we see, we just used if command to check the returncod of 'grep stupid output.txt', if the grep command returned success, we asked if command to execute 'echo success' or else we asked it to execute 'echo failure' | |
2020-07-09 17:17:08 +mohan43u-we this is how we control execution of commands using 'if'-'fi', 'if'-'else'-'fi' and 'if'-'elif'-'fi' commands | |
2020-07-09 17:17:49 +mohan43u-we this time I only used 'if'-'fi' | |
2020-07-09 17:17:57 +mohan43u-we and chcked for only success | |
2020-07-09 17:18:24 +mohan43u-we you can learn the syntex of this 'if' command through 'man sh' | |
2020-07-09 17:19:25 +mohan43u-we usually it is like this | |
2020-07-09 17:20:48 +mohan43u-we if <check-condition-command>; then <true-execution-command>; [elif <another-check-command>] [; else <false-command>; fi | |
2020-07-09 17:22:24 +mohan43u-we see, I gave you example of 'if'-elif-else-fi | |
2020-07-09 17:23:03 +mohan43u-we you can also use switch command to use multiple branching | |
2020-07-09 17:24:30 +mohan43u-we apologies it is called case command | |
2020-07-09 17:26:52 +mohan43u-we ok, just trying to show the case-esac through command line itself | |
2020-07-09 17:27:19 +mohan43u-we but forgot the exact syntex, but you can get those kind of SYNOPSIS through manual pages | |
2020-07-09 17:27:33 +mohan43u-we here is one good manual page for 'sh' posix shell | |
2020-07-09 17:27:36 +mohan43u-we https://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=FreeBSD+12.1-RELEASE+and+Ports&arch=default&format=html | |
2020-07-09 17:27:55 +mohan43u-we you can search for 'esac' key to find the syntext of case-esac control structure | |
2020-07-09 17:28:46 +mohan43u-we now, coming to loops | |
2020-07-09 17:29:09 +mohan43u-we there are two commands to do looping | |
2020-07-09 17:29:13 +mohan43u-we one is the 'for' command | |
2020-07-09 17:30:22 +mohan43u-we the for command in shell script is not like you see in programing languages like c/c++ java etc., this for loop is different | |
2020-07-09 17:31:10 +mohan43u-we the for command which I executed iterates over values 0 1 2 etc and assigns each value to variable 'n' in each iteration of the loop | |
2020-07-09 17:31:50 +mohan43u-we so I asked the for loop to just execute 'echo ${n}", means print the value of $n in each iteration | |
2020-07-09 17:32:07 +mohan43u-we thus, the for loop command did print number 0 to 5 | |
2020-07-09 17:33:01 +mohan43u-we ok, now instead of using numbers why dont we use something else | |
2020-07-09 17:34:01 +mohan43u-we see, its just assigning some variable to some value for each iteration | |
2020-07-09 17:34:34 +mohan43u-we now, I'm going to introduce you to something called command substitution which will going to help you tremendesly in shell scripting | |
2020-07-09 17:34:50 +mohan43u-we I feel this is the right time to introduce command substitution | |
2020-07-09 17:35:24 +mohan43u-we instead of just using static values in the for loop, I'm doing to dynamically generate the values for each iteration | |
2020-07-09 17:35:31 +mohan43u-we using command substitution | |
2020-07-09 17:35:36 +mohan43u-we watch this | |
2020-07-09 17:35:58 +mohan43u-we see the 'ls /' command provides all the directory names form '/' directory | |
2020-07-09 17:37:21 +mohan43u-we see instead of using static values for each iteration, I just generated the values for each iteration dynamically | |
2020-07-09 17:38:13 +mohan43u-we same command, just modified little but to show it in the screen properly | |
2020-07-09 17:38:37 +mohan43u-we see, in the for loop, I used $() command substitution method | |
2020-07-09 17:39:22 +mohan43u-we this $() command substitution replaces the output of the inner command in the command line | |
2020-07-09 17:40:13 +mohan43u-we so when shell executed 'for dir in $(ls /); do echo "${dir}"; done, it actually replaced '$(ls /)' with the actual output of 'ls /' command | |
2020-07-09 17:40:45 +mohan43u-we so it become 'for dir in bin dev home lib32' etc.. | |
2020-07-09 17:41:02 +mohan43u-we so this is called command substitution | |
2020-07-09 17:41:26 +mohan43u-we you can use this substitution to assign dynamic values to variables | |
2020-07-09 17:42:14 +mohan43u-we see, I first assigned $(ls /) output to variable 'alldirs' | |
2020-07-09 17:42:22 +mohan43u-we then I used that variable in the for loop | |
2020-07-09 17:43:04 +mohan43u-we see, the output of '$(ls /)' is still available inside 'alldirs' variable, I can use this variable anywhere I want in the shell script | |
2020-07-09 17:43:22 +mohan43u-we command substitution is one important aspect in shell scripting | |
2020-07-09 17:44:14 +mohan43u-we the other loop command is 'while' command | |
2020-07-09 17:44:55 +mohan43u-we the command which I typed in presenter terminal is a sleep forever loop | |
2020-07-09 17:45:52 +mohan43u-we the while command checks for the return code of 'true' command, if the 'true' command returns '0', then it executes 'sleep 1'; otherwise it exits from the loop | |
2020-07-09 17:46:30 +mohan43u-we as I already told, 'true' command always returns '0' every time it executes, so the while loop will never exit from looping | |
2020-07-09 17:47:13 +mohan43u-we so, we have 'for' and 'while' commands for loop | |
2020-07-09 17:47:29 +mohan43u-we we have 'if' and 'case' commands for branching | |
2020-07-09 17:47:59 +mohan43u-we command substitution is very important way to generate dynamic values | |
2020-07-09 17:48:14 +mohan43u-we now, moving to next topic | |
2020-07-09 17:48:54 +mohan43u-we variable expansion | |
2020-07-09 17:50:13 +mohan43u-we when we execute 'echo $variablename'command | |
2020-07-09 17:50:45 +mohan43u-we shell substitutes the value of variable 'variablename' in the commandline, so the actual command befome 'echo value' and then it execute the commandline | |
2020-07-09 17:51:53 +mohan43u-we so, whenevery shell see '$' followed by a string it first tries to substiture the variable then it executes, | |
2020-07-09 17:52:00 +mohan43u-we it is called variable expansion | |
2020-07-09 17:52:44 +mohan43u-we there are various rules shell follows when expanding variables when it sees '$' | |
2020-07-09 17:53:03 +mohan43u-we the man page link I shared with you have all the details | |
2020-07-09 17:53:13 +mohan43u-we I'm giving one simple example | |
2020-07-09 17:54:16 +mohan43u-we I used $() substitution to generate value for mydirs variable | |
2020-07-09 17:54:30 +mohan43u-we so mydirs contains all the directory names from / directory | |
2020-07-09 17:54:46 +mohan43u-we second I executed 'echo ${mydirs}' command | |
2020-07-09 17:54:53 +mohan43u-we here I used ${mydirs} | |
2020-07-09 17:55:59 +mohan43u-we so shell replaced 'echo ${mydirs}' commandline to 'echo bin boot dev etc' then it executed, we already know that echo just outputs whatever we give as its parameters | |
2020-07-09 17:56:08 +mohan43u-we now, see this echo command | |
2020-07-09 17:56:22 +mohan43u-we see, the difference | |
2020-07-09 17:57:08 +mohan43u-we you see, there are different in how shell expands "${mydirs}" and ${mydirs} | |
2020-07-09 17:59:16 +mohan43u-we when shell expands ${mydirs}, each line in mydirs variable become different parameters for 'echo' command | |
2020-07-09 17:59:55 +mohan43u-we but when shell expands "${mydirs}", the show content of mydirs including '\n' end of line become an single parameter for echo command | |
2020-07-09 18:00:10 +mohan43u-we thus the execution provides different output | |
2020-07-09 18:00:27 +mohan43u-we ok. already 1 hour over :) | |
2020-07-09 18:00:51 +mohan43u-we today also we will extend for another 30 mins | |
2020-07-09 18:01:09 +mohan43u-we take a break for 5 mins and do join again | |
2020-07-09 18:01:16 * mohan43u-we taking 5 min break | |
2020-07-09 18:03:43 +shrini if you are joining #ilugc through webchat.freenode.net, follow https://pasteboard.co/JgGXsrf.gif | |
2020-07-09 18:03:45 +shrini this link to disable join/part messages | |
2020-07-09 18:04:13 +shrini by doing this, you can avoid all the join/quit messages | |
2020-07-09 18:04:30 +shrini and see only what we type in real | |
2020-07-09 18:04:47 +shrini it will be very easy to follow mohan43u-we if you disable these join/quit messages | |
2020-07-09 18:04:59 +shrini Hope you all disabled it already | |
2020-07-09 18:05:25 * mohan43u-we back from 5 min break | |
2020-07-09 18:05:44 +shrini Another request | |
2020-07-09 18:05:53 +shrini change all nicknames to your real names | |
2020-07-09 18:06:14 +shrini no need to have your roll number/exam numbers as nick names in IRC | |
2020-07-09 18:06:28 +mohan43u-we thanks shrini | |
2020-07-09 18:06:30 +shrini it will be good to have real names or any other nick name you link | |
2020-07-09 18:06:37 +shrini mohan43u-we: you continue | |
2020-07-09 18:06:44 +mohan43u-we back to variable expansion | |
2020-07-09 18:07:41 +mohan43u-we there are few more different things you can do when doing variable expansion | |
2020-07-09 18:10:00 +mohan43u-we see, I used one expansion method called ':+' | |
2020-07-09 18:10:59 +mohan43u-we the second line which assigns value to 'var2' first checks if var1 variable value is availabel or not, if var1 have value, then assign 'value2', thats what the second line is | |
2020-07-09 18:12:21 +mohan43u-we see I used ':-' method, the line which assigns value to var3 basically check for variable 'var4', if var4 value not available, then assign 'value3' to 'var3' | |
2020-07-09 18:12:28 +mohan43u-we thats why var3 have value3 | |
2020-07-09 18:13:54 +mohan43u-we see, I used ':-' again, this time, I check for 'var3' if var3 not have value, then assign 'value4', since 'var3' already available and have value, shell would have assigned 'value3' to 'var4' | |
2020-07-09 18:14:20 +mohan43u-we this is how you can also do variable expansion | |
2020-07-09 18:15:13 +mohan43u-we there are few more method available like ':+', ':-', one more is ':?' which throws error message when the variable is not assigned any value | |
2020-07-09 18:16:15 +mohan43u-we see, it throws the error message which I provided, shell first check if there is any variable called 'var6', if it is not available, I asked shell to throw this error message | |
2020-07-09 18:16:28 +mohan43u-we since var6 is not there, shell throwed error message | |
2020-07-09 18:16:59 +mohan43u-we you can read the man page of 'sh' to get all these special way of expanding shell variables | |
2020-07-09 18:17:20 +mohan43u-we now the next topic is arithmetic expansion | |
2020-07-09 18:18:38 +mohan43u-we see you can do integer arithmetic using $(()) expansion | |
2020-07-09 18:19:00 +mohan43u-we $(()) cannot do floathing arithmetic, for that you need to use 'bc' or 'dc' commands | |
2020-07-09 18:19:39 +mohan43u-we I dont have these commands installed, but you can use them for floating point arithmetic | |
2020-07-09 18:19:57 +mohan43u-we for integer arithmetic, you can simply use $(()) | |
2020-07-09 18:20:37 +mohan43u-we see, this is the byte expansion of 5MB | |
2020-07-09 18:21:27 +mohan43u-we thus the arithmetic expansion | |
2020-07-09 18:21:49 +mohan43u-we now next topic | |
2020-07-09 18:21:52 +mohan43u-we special variables | |
2020-07-09 18:22:22 +mohan43u-we apart from normal vairables, environment variables, shell provides some built-in special variables | |
2020-07-09 18:23:03 +mohan43u-we '$$' is a special variable, it provide the process id of current shell | |
2020-07-09 18:23:50 +mohan43u-we '${?}' is also a special variable which shell provides, this variable provides the exitcode (or return code) of last executed command | |
2020-07-09 18:24:49 +mohan43u-we "${!}" is also a special variable which provides pid of last executed background command | |
2020-07-09 18:25:22 +mohan43u-we you may think that why we execute commands in background, there are time when you need to execute commands in paralled | |
2020-07-09 18:25:27 +mohan43u-we *parrallel* | |
2020-07-09 18:25:46 +mohan43u-we at that time, '&' and '${!}' comes handy | |
2020-07-09 18:26:27 +mohan43u-we see, jobs command will also provide process id of background jobs apart from the jobid of background jobs | |
2020-07-09 18:27:12 +mohan43u-we the forever while which I put in the background has '%1' as its job id, but its process id is 853133 which is the same we got from ${!} | |
2020-07-09 18:27:33 +mohan43u-we 'kill' command is used to kill processes | |
2020-07-09 18:29:28 +mohan43u-we I'm stopping here for today. we will see 'functions', 'positional parameters', 'parameter expansion', 'shell patterns' and rest tomorrow | |
2020-07-09 18:30:00 -- Mode #ilugc [-m] by mohan43u | |
2020-07-09 18:30:00 _181CS262 tq sir | |
2020-07-09 18:30:07 suba thank you | |
2020-07-09 18:30:07 LENOVO__ tq sir | |
2020-07-09 18:30:14 +mohan43u-we #ilugc channel is open for questions | |
2020-07-09 18:30:16 MOHAN thank you sir | |
2020-07-09 18:30:45 -- Mode #ilugc [-v mohan43u-we] by mohan43u | |
2020-07-09 18:30:51 -- Mode #ilugc [-v shrini] by mohan43u | |
2020-07-09 18:30:56 -- Mode #ilugc [-v mbuf] by mohan43u | |
2020-07-09 18:31:46 aaryan Thank you !!! | |
2020-07-09 18:32:15 mohan43u-we aaryan: useful today? | |
2020-07-09 18:32:29 aaryan Yes learnt a lot! | |
2020-07-09 18:32:44 _172IT146 thank you sir | |
2020-07-09 18:32:53 aaryan looking forward for tommorows session | |
2020-07-09 18:33:40 prit thank you | |
2020-07-09 18:35:19 Ishwarya Thank you mohan43u-we | |
2020-07-09 18:35:23 mohan43u-we aaryan: day 5 was planned to do example shell scripting, but as I expected, the topics to cover itself pushed into day five. so I'm leaving the writing shell script part to users as excersise. | |
2020-07-09 18:35:37 ramswamy123 thank you, learned a lot today | |
2020-07-09 18:36:24 shrini mohan43u-we: you can cover shell scripting in ilugc meet this saturtday, if possible | |
2020-07-09 18:37:01 shrini For all with exam/roll numbers, you can join with your real names itself | |
2020-07-09 18:37:05 mohan43u-we all, do read the manual page of 'sh' available here https://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=FreeBSD+12.1-RELEASE+and+Ports&arch=default&format=html, it is the official and definitive manual of 'sh' shell | |
2020-07-09 18:37:07 shrini we dont take any attendance | |
2020-07-09 18:38:09 shrini if you want to practise more, check here - https://www.webminal.org/ | |
2020-07-09 18:38:24 shrini register and check a web based terminal and more tutorials there | |
2020-07-09 18:38:39 shrini learning commandline will give you super powers | |
2020-07-09 18:39:15 shrini The floor is open | |
2020-07-09 18:39:22 shrini any one can any questions | |
2020-07-09 18:39:32 shrini looking for feebnack from you all | |
2020-07-09 18:39:35 mohan43u-we if you want to practice **most**, install Linux :) and dont go back to windows if you got stuck :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment