Chapter2 Standard Output

1.1 Writing Output to the Terminal/Window

Use the echo built-in command.All the parameters on the command line are printed to the screen.

The echo command is one of the most simple of all bash commands .

keep in mind:

   (1) the shell is parsing the arguments on the echo command line.

   (2) the spacing between arguments is ignored

[maxwell@MaxwellDBA shelllearning]$ echo Please wait
Please wait
[maxwell@MaxwellDBA shelllearning]$ echo this     was     very      widely        spaced
this was very widely spaced
[maxwell@MaxwellDBA shelllearning]$ 

1.2 Writing Output but Preserving Spacing

Enclose the string in quotes.

[maxwell@MaxwellDBA shelllearning]$ echo "this    was    very   widely   spaced"
this    was    very   widely   spaced
[maxwell@MaxwellDBA shelllearning]$ echo 'this    was    very   widely   spaced'
this    was    very   widely   spaced
[maxwell@MaxwellDBA shelllearning]$ 

1.3 Writing Output with More Formatting Control

The numbers between the % and the format type (s or f) provide additional formatting details.

For a string , the first digit is the maximum field width, and the second is the minimum field width.

[maxwell@MaxwellDBA shelllearning]$ printf '%s = %d\n' Lines $LINES
Lines = 49
[maxwell@MaxwellDBA shelllearning]$ printf '%-10.10s = %4.2f\n' 'GigaHerz' 1.92735
GigaHerz   = 1.93
[maxwell@MaxwellDBA shelllearning]$

1.4 Writing Output Without the Newline

Using printf ——just leave of the ending \n in your format string. with echo use the -n option.

[maxwell@MaxwellDBA shelllearning]$ printf "%s %s" next promt
next promt[maxwell@MaxwellDBA shelllearning]$ 
[maxwell@MaxwellDBA shelllearning]$ echo -n prompt
prompt[maxwell@MaxwellDBA shelllearning]$ 
[maxwell@MaxwellDBA shelllearning]$ 

1.5 Saving Output from a Command

Use the > symbol to tell the shell to redirect the output into a file.

The cat command get its name from the longer word concatenation. The cat command concatenates  the output from the several files listed on its command line.

[maxwell@MaxwellDBA shelllearning]$ echo fill it up
fill it up
[maxwell@MaxwellDBA shelllearning]$ echo fill it up > file.txt | cat file.txt
fill it up
[maxwell@MaxwellDBA shelllearning]$ echo fill it up one  > first.half
[maxwell@MaxwellDBA shelllearning]$ echo fill it up two  > second.half
[maxwell@MaxwellDBA shelllearning]$ cat first.half second.half > whole.file | cat whole.file
fill it up one
fill it up two
[maxwell@MaxwellDBA shelllearning]$ 

1.6 Saving Output to Other Files

Use more of  a pathname when you redirect the output.

[maxwell@MaxwellDBA test]$ echo some more data > /tmp/echo.out
[maxwell@MaxwellDBA test]$ # or
[maxwell@MaxwellDBA test]$ echo some more data > ../../over.here
[maxwell@MaxwellDBA test]$ 

1.7 Saving Output from the ls Command

Use the -C option on ls when you redirect the output.

[maxwell@MaxwellDBA test]$ echo you are a  > a.out
[maxwell@MaxwellDBA test]$ echo he is a congfigure > cong.txt
[maxwell@MaxwellDBA test]$ echo my name is def > def.conf
[maxwell@MaxwellDBA test]$ echo that is a good file > file.txt
[maxwell@MaxwellDBA test]$ echo there are more foods > more.txt
[maxwell@MaxwellDBA test]$ echo I like the zebra which is a animal > zebra.list
[maxwell@MaxwellDBA test]$ ls
a.out  cong.txt  def.conf  file.txt  more.txt  zebra.list
[maxwell@MaxwellDBA test]$ ls > /tmp/save.out | cat /tmp/save.out
[maxwell@MaxwellDBA test]$ cat /tmp/save.out
a.out
cong.txt
def.conf
file.txt
more.txt
zebra.list
[maxwell@MaxwellDBA test]$ ls -C > /tmp/save.out
[maxwell@MaxwellDBA test]$ cat /tmp/save.out
a.out  cong.txt  def.conf  file.txt  more.txt  zebra.list
[maxwell@MaxwellDBA test]$ ls -l
total 24
-rw-rw-r-- 1 maxwell maxwell 10 Jul 21 15:39 a.out
-rw-rw-r-- 1 maxwell maxwell 19 Jul 21 15:40 cong.txt
-rw-rw-r-- 1 maxwell maxwell 15 Jul 21 15:40 def.conf
-rw-rw-r-- 1 maxwell maxwell 20 Jul 21 15:40 file.txt
-rw-rw-r-- 1 maxwell maxwell 21 Jul 21 15:41 more.txt
-rw-rw-r-- 1 maxwell maxwell 35 Jul 21 15:42 zebra.list
[maxwell@MaxwellDBA test]$ 
[maxwell@MaxwellDBA test]$ 

1.8 Sending Both Output and Error Messages to Different Files.

Redirect output and error messages to different files

[maxwell@MaxwellDBA test]$ myprogram 1> messages.out 2> message.err

or more commonly

[maxwell@MaxwellDBA test]$ myprogram > messages.out 2> message.err

1.9 Sending Both Output and Error Messages to the Same File

Use the shell syntax to redirect standard error messages to the same place as standard output.

[maxwell@MaxwellDBA test]$ both >& outfile

# or

[maxwell@MaxwellDBA test]$ both &> outfile

&> or >& is a shortcut that simply sends both STDOUT and STDERR to the same place --exactly what we want to do.

1.10 Appending Rather Than Clobbering Output

[maxwell@MaxwellDBA test]$ ls
a.out  cong.txt  def.conf  file.txt  more.txt  zebra.list
[maxwell@MaxwellDBA test]$ ls > /tmp/ls.out
[maxwell@MaxwellDBA test]$ cd ../elsewhere
[maxwell@MaxwellDBA elsewhere]$ ls >> /tmp/ls.out
[maxwell@MaxwellDBA elsewhere]$ cd ../anotherdir
[maxwell@MaxwellDBA anotherdir]$ ls >> /tmp.ls.out
-bash: /tmp.ls.out: Permission denied
[maxwell@MaxwellDBA anotherdir]$ ls >> /tmp/ls.out
[maxwell@MaxwellDBA anotherdir]$ cat /tmp/ls.out
a.out
cong.txt
def.conf
file.txt
more.txt
zebra.list
a1.out
cong1.txt
def1.conf
file1.txt
more1.txt
zebra1.list
TestA.txt
TestB.txt
TestC.txt
[maxwell@MaxwellDBA anotherdir]$

1.11 Using Just the Beginning or End of a File

use the head or tail commands.head will output the first 10 lines and tail will output the last 10 lines of the given file.

1.12 Throwing Output Away

 Redirect the output to /dev/null as shown in these examples:

[maxwell@MaxwellDBA test]$ find / -name myfile -print 2> /dev/null
[maxwell@MaxwellDBA test]$ noisy >/dev/null 2>&1

1.13 Saving or Grouping Output from Several Commands

Use braces { } to group these commands together, then redirection applies to the output from all commands in the group.

[maxwell@MaxwellDBA test]$ { pwd; ls; cd ../elsewhere; pwd; ls; } > /tmp/all.out
[maxwell@MaxwellDBA elsewhere]$ (pwd; ls; cd ../elsewhere; pwd; ls;) > /tmp/alla.out
[maxwell@MaxwellDBA elsewhere]$ cat /tmp/all.out
/home/maxwell/shelllearning/test
a.out
asdfwednnrw2rs2esff.txt
cong.txt
def.conf
file.txt
lines
more.txt
zebra.list
/home/maxwell/shelllearning/elsewhere
a1.out
cong1.txt
def1.conf
file1.txt
more1.txt
zebra1.list
[maxwell@MaxwellDBA elsewhere]$ cat /tmp/alla.out
/home/maxwell/shelllearning/elsewhere
a1.out
cong1.txt
def1.conf
file1.txt
more1.txt
zebra1.list
/home/maxwell/shelllearning/elsewhere
a1.out
cong1.txt
def1.conf
file1.txt
more1.txt
zebra1.list
[maxwell@MaxwellDBA elsewhere]$ 

1.14 Connecting Two Programs by Using Output As Input

[maxwell@MaxwellDBA test]$ cat one.file another.file > /tmp/cat.out
[maxwell@MaxwellDBA test]$ sort < /tmp/cat.out
1
23
345
4567
56789
678910
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
adjajnsojqo
dfjajdjado
sadjajdfc
sdjdasfa
[maxwell@MaxwellDBA test]$ cat one.file another.file | sort 
1
23
345
4567
56789
678910
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
adjajnsojqo
dfjajdjado
sadjajdfc
sdjdasfa
[maxwell@MaxwellDBA test]$ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值