guides:software:redirection
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
guides:software:redirection [2020/08/16 20:08] – wikiadmin | guides: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. | I/O redirection is part of most shell systems. | ||
+ | |||
+ | ===== Quick Reference ===== | ||
+ | |||
+ | To redirect the standard input to the program hello from the file queenName: | ||
+ | < | ||
+ | $ hello < queenName | ||
+ | </ | ||
+ | |||
+ | To redirect the standard output of the program hello to the file outfile | ||
+ | < | ||
+ | $ hello > outfile | ||
+ | </ | ||
+ | |||
===== 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. | ||
+ | < | ||
+ | $ 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, | ||
+ | </ | ||
+ | |||
+ | Now the program can be run using file redirection behaving as if the user entered the Queen' | ||
+ | < | ||
+ | $ 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, | ||
+ | </ | ||
+ | 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. | ||
+ | < | ||
+ | $ hello > outfile | ||
+ | Dan | ||
+ | |||
+ | $ cat outfile | ||
+ | Enter your name : | ||
+ | Hello Dan! | ||
+ | </ | ||
+ | |||
+ | 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. | ||
+ | |||
+ | < | ||
+ | $ hello < queenName | ||
+ | [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, | ||
+ | </ |
guides/software/redirection.1597608513.txt.gz · Last modified: 2024/07/25 15:01 (external edit)