So now we are going to learn about standard input output and pipes for RTL. So let's start the input output could be channelized into programs, there are three input output channels is standard input stdio h that is provided by keyword a standard output or STD out that is provided on terminal window by default, the standard error STD er, which is directed to a terminal window by default, however, we can generalize the input and output to devices or programs or to text files. So, we are going to learn about how we can do so. We can redirect output to a file output of a standard output or a standard error which can be redirected to files. So the syntax for writing this is provided a command the command which you want to execute the operator for standard output or standard error and the filename where the output or error has to be transferred.
So the supported operators include a redirection of the standard output to a file provided by arrow symbol on the right hand side. Then there is a redirection option for a standard error to a file, which is provided by writing to and directed symbol on the right hand side. Then we can redirect all the outputs including errors and output program output to the file by providing ampersand symbol and right hand side arrow symbol. So let's take an example. Suppose we want to execute the find command on a certain path. I like changing the password we can do stored the output of this command is standard output of this command in the find output file, find out out or PhD or see any file you can say.
So, it will be executed. Similarly, we want we can redirect error of this command is chose to the path and we can also output everything else. The pipe command is generally used to execute two commands simultaneously, along with another functionality where output of command one written on the left hand side of first command is provided as input to the second command. We can use less command there are three more commands we learned here, the list command which is used to display input one page at a time. We can view any on one page at a time. So, in the in this command last line on the left hand side, the ls command, which is used to list shell, or show them files and directories within a certain folder is used to show those files display those files.
But there are a huge number of files possibly that can reside inside any directory or folder. But we want to see only few on the screen. So just provide right this command ls the part of the directory and the argument for the LS Sep right after that five symbol then right less Come on le SS. This will execute our functionality. Similarly, we can send output via email by using the mail command. Suppose we want to make Something mail I string, email a string via the terminal, we can do so by just writing echo command, provide the string, you want to mail within the quotes, then write a pipe symbol, then write the mail command, provide the argument and the email id, it will email.
Similarly, we can simply simply print any string on a piece of paper using the printer when it is connected to your computer by using LPR command. So, the way if we want to print any string directly from the terminal, just simply write echo command, provide the string, place the pipe symbol and write LPR it will be sent to printer and printer will simply print the string on a piece of paper. Then you can use output and error combination wherever Or put an error could be combined with ampersand and directed as a symbol. Similarly as with earlier examples there is another option where we can redirect a standard error to standard output by just writing two directed arrow ampersand symbol and one. It is useful for sending all the offers through a pipe. And with a parenthesis, you can combine standard outputs of multiple programs like here we did for the calendar of 2007 and 2008.
You can also redirect to multiple targets using the T command. Where some operators could be could affect both a standard output. It is towards the command here. It is still a standard output of command one in filename, then pipe to command to a pipe company. too good to be five again to command three. So you can use multiple times the pipe operator to execute multiple commands simultaneously in a single line.
The format would be the input of the first command, sorry output out the first command is provided as input to the second, output of second command is provided as input to second, and third and so on. You can read edit a standard input from a file with a left hand side arrow symbol. Some commands can accept data redirection to standard input from a file. Simply write the syntax write the command for example we were here we are writing tr command providing the range A to Zed capital and is fall redirecting it to a bash profile. It will translate the uppercase characters in dot bash profile to lowercase the equivalent condition of writing the same could be which cat Come on provide the file name earlier and tr command later on after the file. We another powerful thing that we can do with shell scripting is loop shell scripts are very useful thing that could be used for Server Administration and working over the network.
So, let us understand with an example. Suppose you have multiple files. For example, you have to execute a C code on every computer kept in a lab. The lab contains consists of 50 computers, and you have the sole responsibility of installing 10 c scripts. The C programming language so how you can execute a script You have to manually copy the scripts on each computer via pendrive or over the network, and then manually execute a script on each computer, it would take a very long time and it will get tedious or you can keep it short by writing a shell script is storing it on the pendrive or any network distribution environment. provided all the C scripts, the C script part would be saved inside that pendrive.
And the shell script is executed remotely from for each computer. Or you can execute simply just double click on each script for each PC, and it will execute all the commands simultaneously. Your work is very reduced. But just you need to write a shell script that executes the GCC compiler. For a sale a script and so, is C script. And so, similarly you can perform my huge range of tasks.
Shell scripts are basically designed to provide multi line out execution of the same commands that are been executed on Linux terminal with system variables and other functionalities. So, here there is an example for for loop loops are used for reputation of tasks, which can be used in the example I provided you. The for loop is a widely used in programming languages like Python, C, Java and other languages. Here we are using the for loop for three strings. Here we have three strings Joe, Jane and Julie. And we have a user defined variable call the name it will serve In these three strings when the condition is true, the execution of the three lines followed by two are done.
So, what is done in the first line of after do an email address is stored in a variable called address and another string is stored in a message. And the third line executes the execute a string within with pipe where the message variable is echoed or displayed, the output is transferred to the email address and the email address is provided by the address very well. So it is a simple way of writing the email to Jane, Joe and Julie. The three persons with a day may have an email id like Jane at the rate example.com. Here you can write a gmail.com yahoo.com or Yahoo mix.com and any other email ID that you have. So this is what we learned today.
We will learn more about later. The pipes, a standard input and output. Keep practicing those concepts. Just simply write multiple commands. They allow you a greater functionalities. So keep learning and keep moving ahead.
We will learn more