User Tools

Site Tools


guides:software:redirection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
guides:software:redirection [2020/08/16 20:08] wikiadminguides:software:redirection [2024/07/25 15:01] (current) – external edit 127.0.0.1
Line 2: Line 2:
  
 I/O redirection is part of most shell systems.  This process allows the user to enter data into a program from a file instead of the keyboard, or send the output of a program to a file instead of the screen. I/O redirection is part of most shell systems.  This process allows the user to enter data into a program from a file instead of the keyboard, or send the output of a program to a file instead of the screen.
 +
 +===== Quick Reference =====
 +
 +To redirect the standard input to the program hello from the file queenName:
 +<code>
 +$ hello < queenName
 +</code>
 +
 +To redirect the standard output of the program hello to the file outfile
 +<code>
 +$ hello > outfile
 +</code>
 +
  
 ===== An Example =====  ===== An Example ===== 
Line 34: Line 47:
  
 A solution is to place this information in a file.   So create the file // queenName // that contains this information. A solution is to place this information in a file.   So create the file // queenName // that contains this information.
 +<code>
 +$ cat queenName
 +Elizabeth the Second, by the Grace of God of the United Kingdom of Great Britain and Northern Ireland and of Her other Realms and Territories Queen, Head of the Commonwealth, Defender of the Faith
 +</code>
 +
 +Now the program can be run using file redirection behaving as if the user entered the Queen's style.
 +<code>
 +$ hello < queenName 
 +Enter your name : 
 +Hello Elizabeth the Second, by the Grace of God of the United Kingdom of Great Britain and Northern Ireland and of Her other Realms and Territories Queen, Head of the Commonwealth, Defender of the Faith!
 +</code>
 +Notice in this example, the input from the file does not show as if it had been typed.
 +
 +Output redirection can be used to save the standard output of a program to a file.  
 +<code>
 +$ hello > outfile
 +Dan
 +
 +$ cat outfile
 +Enter your name : 
 +Hello Dan!
 +</code>
 +
 +In this example
 +  * The user runs the program, redirecting output to the file // outfile //.
 +    * Because the prompt was written to the output file, it does not appear on the screen.
 +    * The user types in a name ** Dan **.
 +  * The user than displays the contents of the redirected output by typing ** cat outfile **.
 +
 +Output redirection can be done in both ways, so the following is a valid command.
 +
 +<code>
 +$ hello < queenName  > outfile
 +[dbennett@cslab103 tmp]$ cat outfile
 +Enter your name : 
 +Hello Elizabeth the Second, by the Grace of God of the United Kingdom of Great Britain and Northern Ireland and of Her other Realms and Territories Queen, Head of the Commonwealth, Defender of the Faith!
 +</code>
guides/software/redirection.1597608513.txt.gz · Last modified: 2024/07/25 15:01 (external edit)