Beginning Linux Programming 學習-chapter2-Shell programming-Pipes and Redirection

「爲了從事創造性工做,人類須要孤獨,但是在孤獨中,廣義的人類仍存在於心裏。」——(德國)奧鏗
                                               
linux

uninx, linux的初衷是不使用圖形界面,而是使用commnd line,隨着不斷髮展,command line不斷壯大,shell對lilnux來講是一個很是重要的部分,對於自動完成一些任務是很是方便的!shell

爲何用shell?編程

首先,shell編程方便,快捷,另外,最基本的linux系統中也能夠支持shell,能夠用shell 快速實現你的一些想法!windows

shell與windows中的command prompt相似,可是更增強大。 shell program通常被叫作script,一邊運行一遍編譯。bash

什麼是Shell:app

A shell is a program that acts as the interface between you and the Linux system, enabling you to enter commands for the operating system to execute. In that respect, it resembles the Windows command prompt, but as mentioned earlier, Linux shells are much more powerful.
ui

For example, input and output can be redirected using < and >, data piped between simultaneously executing programs using |, and output from a subprocess grabbed  by using $(...).this

系統中能夠有多種shell程序On Linux it’s quite feasible to have multiple shells installed, with different users able to pick the one they prefer. The following Figure  shows how the shell (two shells actually, both bash and csh) and other programs sit around the Linux kernel.
spa


bash是最經常使用的一種shell:
On Linux, the standard shell that is always installed as
/bin/sh is called bash (the GNU Bourne-Again SHell), from the GNU suite of tools. On most Linux distributions, the program /bin/sh, the default shell, is actually a link to the program /bin/bash.

You can check the version of bash you have with the following command:
$ /bin/bash --version
GNU bash, version 3.2.9(1)-release (i686-pc-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.


若是系統中默認的shell 不是bash,如何使用bash呢:
code

To change to a different shell — if bash isn’t the default on your system, for example —just execute the desired shell’s program (e.g., /bin/bash) to run the new shell and change the command prompt.


各類shell小結:



Pipes and Redirection 管道和重定向

$ ls -l > lsoutput.txt   saves the output of the ls command into a file called lsoutput.txt.  Redirects the standard output into a file by using the > operator .  By default, if the file already exists, then it will be overwritten. If you want to change the default behavior, you can use the command set -o noclobber (or set -C)(禁止重定向覆蓋已存文件 ), which sets the noclobber option to prevent a file from being overwritten using redirection. You can cancel this option using set +o noclobber(容許重定向覆蓋已存文件 )

File descriptor 0 is the standard input to a program,
file descriptor 1 is the standard output

file descriptor 2 is the standard error output.
You can redirect each of these independently. In fact, you can also redirect other file descriptors, but it’s unusual to want to redirect any other than the standard ones: 0, 1, and 2.

將指令的輸出追加到文件末尾:
ps >> lsoutput.txt 
will append the output of the ps command to the end of the specified file
 

To redirect the standard error output, preface the > operator with the number of the file descriptor you wish to redirect. Because the standard error is on file descriptor 2, use the 2> operator. This is often useful to discard error information and prevent it from appearing on the screen.

將指令的標準輸出和錯誤輸出輸出到同兩個文件中:
Suppose you want to use the
kill command to kill a process from a script. There is always a slight risk that the process will die before the kill command is executed. If this happens, kill will write an error message to the standard error output, which, by default, will appear on the screen. By redirecting both the standard output and the error, you can prevent the kill command from writing any text to the screen.
$ kill -HUP  1234  >killout.txt  2>killerr.txt  will put the output and error information into separate files

將指令的標準輸出和錯誤輸出輸出到同一個文件中:
If you prefer to capture both sets of output into a single file, you can use the
>& operator to combine the two outputs. Therefore,
$ kill -1 1234 >killouterr.txt 2>&1
will put both the output and error outputs into the same file. Notice the order of the operators. This reads as 「redirect standard output to the file killouterr.txt, and then direct standard error to the same place as the standard output.」 If you get the order wrong, the redirect won’t work as you expect.

將指令的標準輸出和錯誤輸出所有discard:

Because you can discover the result of the
kill command using the return code , you don’t often want to save either standard output or standard error. You can use the Linux universal 「bit bucket」 of /dev/null to efficiently discard the entire output, like this:
$ kill -1 1234 >/dev/null 2>&1

Redirecting Input:
Rather like redirecting output, you can also redirect input. For example,
$ more < killout.txt  --------??
Obviously, this is a rather trivial example under Linux; the Linux more command is quite happy to accept filenames as parameters, unlike the Windows command-line equivalent.

Pipes 管道
You can connect processes using the pipe operator ( | ). In Linux, unlike in MS-DOS, processes connected by pipes can run simultaneously and are automatically rescheduled as data flows between them. As a simple example, you could use the sort command to sort the output from ps.
If you don’t use pipes, you must use several steps, like this:
$ ps > psout.txt
$
sort psout.txt > pssort.out   //sort是排序命令
A much more elegant solution is to connect the processes with a pipe:
$ ps | sort > pssort.out    //sort做用於ps的輸出,sort的輸出保存在passort.out中

Because you probably want to see the output paginated on the screen, you could connect a third process, more, all on the same command line:
$ ps | sort | more

There's practically no limit to the permissible number of connected processes. Suppose you want to see all the different process names that are running excluding shells. You could use
$ ps –xo comm | sort | uniq | grep -v sh | more // ps -xo comm的輸出被送給sort指令。uniq用於去掉重複的東西;grep -v sh to remove the process named sh.

注意:If you have a string of commands, the output file is created or written to immediately when the set of commands is created, so never use the same filename twice in a string of commands. If you try to do something like
cat mydata.txt | sort | uniq > mydata.txt
you will end up with an empty file, because you will overwrite the mydata.txt file before you read it.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Shell 編程方式之一: Interactive Programs

Just typing the shell script on the command line is a quick and easy way of trying out small code fragments. 例如,又不少c文件,你想搜索包含POSIX字符串的文件:

$ for file in *   //file是循環變量
> do
> if grep -l POSIX $file //使用一個定義過的變量,只要在變量名前面加美圓符號便可
> then
> more $file  //displays the whole contents of the file to the screen
> fi

> done

說明: grep -l 是 列出文件內容符合指定的範本樣式的文件名稱。
            用到了 基本的if-then-fi 判斷語句
當shell但願你繼續輸入時,提示符會由$變爲>。當你最後輸入done以後,shell會自動判斷你輸入完畢,而後立刻執行你寫的整個東西。

Shell 編程方式之二: Writing Scripts

例子:

#!/bin/sh
# first
# This file looks through all the files in the current
# directory for the string POSIX, and then prints the names of
# those files to the standard output.
for file in *
do
    if grep -q POSIX $file
    then
        echo $file
    fi
done
exit 0
行註釋以#開頭;  #!/bin/sh , is a special form of comment,  the #! characters tell the system that the argument that follows on the line is the program to be used to execute this file. In this case, /bin/sh is the default shell program.

The
exit command ensures that the script returns a sensible exit code (more on this later in the chapter). This is rarely checked when programs are run interactively, but if you want to invoke this script from another script and check whether it succeeded, returning an appropriate exit code is very important. Even if you never intend to allow your script to be invoked from another, you should still exit with a reasonable code. Have faith in the usefulness of your script: Assume it may need to be reused as part of another script someday.  A zero denotes success in shell programming.

linux不在文件的後綴:Linux, and UNIX in general, rarely makes use of the filename extension to determine the type of a file. You could have used .sh or added a different extension, but the shell doesn’t care. 

Making a Script Executable

假設你寫了一個腳本名字爲first,而後你可用下面的方式運行它
1)直接使用shell程序 /bin/sh first  須要first就在當前你的command 窗口的當前目錄中;若是不在
   那就須要你將first的路徑寫全。
2)執行 chmod +x first使得first腳本可執行,而後 ./first,你也能夠在PATH中添加它


你能夠專門創建一個目錄存儲你本身寫的可執行腳本,例如/usr/local/bin,加入PATH路徑,之後能夠方便實用。


Shell Syntax:
之後用到再具體看。The shell is quite an easy programming language to learn, not least because it's easy to test small program fragments interactively before combining them into bigger scripts. You can use the bash shell to write quite large, structured programs.
相關文章
相關標籤/搜索