1.Writing Output to the Terminal/Window
Problem: you want some simple output from your shell commands.
Solution: Use the echo built-in command.
[maxwell@DBAMAXWELL learning_bash_shell]$ echo Please wait.
Please wait.
[maxwell@DBAMAXWELL learning_bash_shell]$
[maxwell@DBAMAXWELL learning_bash_shell]$ help echo
echo: echo [-neE] [arg ...]
Write arguments to the standard output.
Display the ARGs, separated by a single space character and followed by a
newline, on the standard output.
Options:
-n do not append a newline
-e enable interpretation of the following backslash escapes
-E explicitly suppress interpretation of backslash escapes
`echo' interprets the following backslash-escaped characters:
\a alert (bell)
\b backspace
\c suppress further output
\e escape character
\E escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\0nnn the character whose ASCII code is NNN (octal). NNN can be
0 to 3 octal digits
\xHH the eight-bit character whose value is HH (hexadecimal). HH
can be one or two hex digits
Exit Status:
Returns success unless a write error occurs.
[maxwell@DBAMAXWELL learning_bash_shell]$ help printf
printf: printf [-v var] format [arguments]
Formats and prints ARGUMENTS under control of the FORMAT.
Options:
-v var assign the output to shell variable VAR rather than
display it on the standard output
FORMAT is a character string which contains three types of objects: plain
characters, which are simply copied to standard output; character escape
sequences, which are converted and copied to the standard output; and
format specifications, each of which causes printing of the next successive
argument.
In addition to the standard format specifications described in printf(1),
printf interprets:
%b expand backslash escape sequences in the corresponding argument
%q quote the argument in a way that can be reused as shell input
%(fmt)T output the date-time string resulting from using FMT as a format
string for strftime(3)
The format is re-used as necessary to consume all of the arguments. If
there are fewer arguments than the format requires, extra format
specifications behave as if a zero value or null string, as appropriate,
had been supplied.
Exit Status:
Returns success unless an invalid option is given or a write or assignment
error occurs.
[maxwell@DBAMAXWELL learning_bash_shell]$
2.Writing Output but Preserving Spacing Problem
Problem : you want the output to preserve your spacing
Solution: Enclose the string in quotes
[maxwell@DBAMAXWELL learning_bash_shell]$ echo "this was very widely spaced"
this was very widely spaced
[maxwell@DBAMAXWELL learning_bash_shell]$ echo 'this was very widely spaced'
this was very widely spaced
[maxwell@DBAMAXWELL learning_bash_shell]$
3.Writing Output with More Formatting Control Problem
Problem: You want more control over the formatting and placement of output.
Solution: Use the prinf built-in command.
ep:
[maxwell@DBAMAXWELL learning_bash_shell]$ printf '%s = %d\n' Lines $LINES
Lines = 33
[maxwell@DBAMAXWELL learning_bash_shell]$ printf '%-10.10s = %4.2f\n' 'GigaHerz' 1.92735
GigaHerz = 1.93
[maxwell@DBAMAXWELL learning_bash_shell]$
4.Writing Output Without the Newline
Problem: You want to produce some output without the default newline that echo provides.
Solution: Using printf it''s easy——just leave off the ending \n in your format string.With echo ,use the -n option.
[maxwell@DBAMAXWELL learning_bash_shell]$ printf "%s %s" next prompt
next prompt[maxwell@DBAMAXWELL learning_bash_shell]$
[maxwell@DBAMAXWELL learning_bash_shell]$
[maxwell@DBAMAXWELL learning_bash_shell]$ echo -n prompt
prompt[maxwell@DBAMAXWELL learning_bash_shell]$
5.Saving Output from a Command
Problem:You want to keep the output from a command by putting it in a file.
Solution: Use the > symbol to tell the shell to redirect the output into a file.
ep:
6.Saving Output to Other Files
Problem:You want to save the output with a redirect to elsewhere in the filesystem,not in the current directory.
Solution:Use more of a pathname when you redirect the output.
ep:
7.Saving Output from the ls Command
Problem:You tried to save output from the ls command with a redirect,but when you look at the resulting file, the format is not what you expected.
Solution:Use -C option on ls when you redirect the output.
ep:
8.Sending Both Output and Error Messages to Different Files
Problem:You are expecting output from a program but you don't want it to get littered with error messages.You'd like to save your error messages,but it's harder to find them mixed among the expected output.
Solution:Redirct output and error message to different files.
9.Sending Both Output and Error Messages to The Same File
Problem:Using redirection ,you can redirect output or error messages to separate files,but how do you capture all the output and error messages to a single file?
Solution:Use the shell syntax to redirect standard error messages to the same place as standard output.
&> or >& is a shortcut that simply send both STDOUT and STDERR to the same place——exactly what we want to do.
10.Appending Rather Than Clobbering Output
Problem:Each time you redirect your output ,it creates that output file anew.What if you want to redirect output a second time(or third,or ...),and don't want to clobber the previous output?
Solution:The double greater-than sign(>>) is a bash redirect that mean append the output.
11.Using Just the Beginning or End of a File
Problem:You need to display or use just the beginning or end of a file.
Solution:Use the head or tail commands.By default,head will output the first 10 lines and tail will output the last 10 lines of the given file.If more than one file is given ,the appropriate lines from each of them are output.Use the -number switch to change the number of lines .tail also has the -f and -F and -F switches,which follow the end of the file as it is written to.
12.Skipping a Header in a File
Problem:You have a file with one or more header lines and you need to process just the data, and skip the header.
Solution:Use the tail command with a special argument.
An argument to tail,which is a number starting dash(-),will specify a line offset relative to the end of the file.tail +1 file gives you the entire file, the same as cat.+2 skips the first line.
13.Throwing Output Away
Problem:Sometimes you don't want to save the output into a file
Solution:Redirect the output to /dev/null as shown
14.Saving or Grouping Output from Serveral Commands
Problem:You want to capture the output with a redirect,but you're typing several commands on one line.
$ pwd; ls; cd ../elsewhere; pwd; ls > /tmp/all.out
Solution: you could use parentheses () to tell bash to run the commands in a subshell,then redirect the output of the entire subshell's execution.
15. Connecting Two Programs by Using Output As Input
Problem:You want to take the output from one program and use it as the input of another program.
Solution: you could redirect the output from the first program into a temporary file,then use that file as input to the second program.
16. Saving a Copy of Output Even While Using it as Input
Problem:You want to debug a long sequence of piped I/O.
Solution: The solution to these problems is to use what plumbers call a T-joint pipes.
[maxwell@DBAMAXWELL cp2]$ find / -name '*.c' -print > /tmp/all.my.sources
[maxwell@DBAMAXWELL cp2]$ find / -name '*.c' -print | tee /tmp/all.my.sources
17.Connecting Two Programs by Using Output As Arguments
Problem:what if one of the programs to which you would like to connect with a pipe doesn't work that way?
Solution: Use the command substitution feature of bash
The $() encloses a command that is run in a subshell.
18.Saving Output When Redirect Doesn't Seem to Work
Problem:You tried using > but some (or all) of the output still appears on the screen.
Solution: Redirect the error output
19.Swapping STDERR and STDOUT
Problem:You need to swap STDERR and STDOUT so you can send STDOUT to a logfile,but then send STDERR to the screen and to a file using the tee command.But pipes only work with STDOUT.
Solution: Swap STDERR and STDOUT before the pipe redirection using a third file descriptor
[maxwell@DBAMAXWELL cp2]$ ./myscript 3>&1 1>stdout.logfile 2>&3- | tee -a stderr.logfile
20.Keeping Files Safe from Accidental Overwriting
Problem:You don't want to delete the contents of a file by mistake.It can be too easy to mistype a filename and find that you've redirected output into a file that you meant to save.
The noclobber option tells bash not to overwrite any existing files when yoou redirect output.
21.Clobbering a File on Purpose
Problem:You like to have noclobber set,but every once in a while you do want to clobber a file when you redirect output.Can you override bash's good intentions,just once?
Solution: Use >| to redirect your output.Even if noclobber is set,bash ingore its setting and overwrites the file.