1.5 Input and Output; 1.6. By adding a simple directive to the command that invokes a program, we can redirect its standard output to. These are sample programs for the Piet programming language I. From there the program writes. It causes characters to be written from the enclosed characters of the edit specifier itself. I define a quine as a program which reproduces itself on an output device without inputting its source. Bash. Guide/Input. And. Output - Greg's Wiki< - Tests and Conditionals . Input refers to any information that your program receives (or reads). Output refers to any information that your program produces (or writes). Command- line Arguments. For many scripts, the first (or the only) input we will care about are the arguments received by the script on the command line. In addition to referring to them one at a time, you may also refer to the entire set of positional parameters with the . Another way to deal with the positional parameters is to eliminate each one as it is used. In real scripts, a combination of these techniques is used. For brevity, we will not include examples of argument processing here. Good Practice: Identify where your input comes from before you start writing. Self-modifying code cannot be used at all. If your design is well thought out, the program practically writes itself. So to build a program's output. The presentation itself: a program of. Compose a program that writes a table of the monthly payments. The Environment. Every program inherits certain information, resources, privileges and restrictions from its parent process. In Bash, environment variables work very much like the regular shell variables we're used to. Traditionally, environment variables have names that are all capital letters, such as PATH or HOME. Environment variables can also be tweaked on the fly extremely easily (more easily than if the same information were stored in a file). In a script, if you know that some information is in an environment variable, you can just use it like any other variable: if . If you want to put information into the environment for your child processes to inherit, you use the export command: export MYVAR=something. The tricky part here is that your environment changes are only inherited by your descendants. Changing the environment and then running some other program is extremely common. Good Practice: Don't use all- capital variable names in your scripts, unless they are environment variables. File Descriptors. File Descriptors (in short: FDs) are the way programs refer to files, or to other resources that work like files (such as pipes, devices, sockets, or terminals). By default, every new process starts with three open FDs: Standard Input (stdin): File Descriptor 0 Standard Output (stdout): File Descriptor 1 Standard Error (stderr): File Descriptor 2 In an interactive shell, or in a script running on a terminal, the Standard Input is how bash sees the characters you type on your keyboard. GUI applications also have these FDs, but they don't normally work with them. Let's make these definitions a little more concrete. Good Practice: Remember that when you create scripts, you should send your custom error messages to the stderr FD. Redirection. The most basic form of input/output manipulation in BASH is Redirection. Redirections are performed by BASH (or any other shell), before the shell runs the command to which the redirections are applied. Redirection: the practice of changing a FD to read its input from, or send its output to, a different location. File Redirection. File Redirection involves changing a single FD to point to a file. As a result, the echo command will not send its output to the terminal; rather, the > story redirection changes the destination of the stdout FD so that it now points to a file called story. It should be noted that this redirection is in effect only for the single echo command it was applied to. We then use the application cat to print out the contents of that file. Warning: Far too many code examples and shell tutorials on the Internet tell you to use cat whenever you need to read the contents of a file. When we use cat without passing any kind of arguments, it obviously doesn't know what files to read. As you type test?, you will see it echoed on the screen by the terminal itself. You can press Ctrl+D to send your terminal the End of File character. Now let's use an input redirection to attach a file to stdin, so that stdin is no longer reading from our keyboard, but instead, now reads from the file: $ cat < story. The story of William Tell. Redirection operators can be preceded by a number. Let's summarize with some examples: command > file: Send the stdout of command to file. The number for the stderr FD is 2. You may have noticed that our redirection operator isn't on rm, but it's on that done thing. Let's see what the result of our loop was: $ cat errors. No such file or directory. No such file or directory. Two error messages in our error log file. If you're writing a script, and you expect that running a certain command may fail on occasion, but don't want the script's user to be bothered by the possible error messages that command may produce, you can silence a FD. There is one last thing you should learn about File Redirection. By the way, the space between the redirection operator and the filename is optional. Good Practice: It's a good idea to use redirection whenever an application needs file data and is built to read data from stdin. When designing an application that could be fed data from a variety of different sources, it is often best simply to have your application read from stdin; that way, the user can use redirection to feed it whatever data she wishes. File Descriptor Manipulation. Now that you know how to manipulate process input and output by sending it to and reading it from files, let's make it a little more interesting still. It's possible to change the source and destination of FDs to point to or from files, as you know. There's an application called grep that we've seen briefly in a previous chapter. OK, now that you understand grep, let's continue with our File Descriptor Manipulation. Now, how would you go about silencing this grep statement completely? We need to prevent having two independent FDs working on the same destination or source. A duplicate FD works differently from having two independent FDs pointing to the same place. Be careful not to confuse the order: $ grep proud file 'not a file' 2> & 1 > proud. This will duplicate stderr to where stdout points (which is the terminal), and then stdout will be redirected to proud. The & > redirection operator is actually just a shorter version of what we did here; redirecting both stdout and stderr to a file : $ grep proud file 'not a file' & > proud. This is the same as > proud. Bourne. Shell. TODO: Moving FDs and Opening FDs RW. Heredocs And Herestrings. Sometimes storing data in a file is overkill. There are a few different options with Heredocs. Bash substitutions are performed on the contents of the Heredoc by default. Later on, you will learn about pipes and how they can be used to send the output of a command into another command's stdin. As such, additional redirections can occur on the same line, all evaluated in the usual order. If you have a large document that your script needs, you should ship it in a separate file along with your script. Pipes. Now that you can effortlessly manipulate File Descriptors to direct certain types of output to certain files, it's time you learn some more ingenious tricks available through I/O redirection. You can use File Redirection to write output to files or read input from files. In our example, the FIFO called myfifo is read from by grep. But these temporary files are a real annoyance. For these reasons, another feature is made available: Pipes. Pipes are widely used as a means of post- processing application output. Note: The pipe operator creates a subshell environment for each command. Good Practice: Pipes are a very attractive means of post- processing application output. You should, however, be careful not to over- use pipes. Miscellaneous Operators. Aside from the standard I/O operators, bash also provides a few more advanced operators that make life on the shell that much nicer. Process Substitution. A cousin of the pipe is the process substitution operator, which comes in two forms: < () and > (). What it does, is basically run the command inside the parentheses. Here's how we can put that into action: Imagine a situation where you want to see the difference between the output of two commands. The actual implementation of the temporary files differs from system to system.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |