Beginning Linux Programming 学习-chapter2-Shell programming-Pipes and Redirection
“为了从事创造性工作,人类需要孤独,可是在孤独中,广义的人类仍存在于内心。”——(德国)奥铿
uninx, linux的初衷是不使用图形界面,而是使用commnd line,随着不断发展,command line不断壮大,shell对lilnux来说是一个非常重要的部分,对于自动完成一些任务是非常方便的!
为什么用shell?
首先,shell编程方便,快捷,另外,最基本的linux系统中也可以支持shell,可以用shell 快速实现你的一些想法!
shell与windows中的command prompt类似,但是更加强大。 shell program一般被叫做script,一边运行一遍编译。
什么是Shell:
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.
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 $(...).
系统中可以有多种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.
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呢:
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.
Beginning Linux Programming 学习-chapter2-Shell programming-Pipes and Redirection的更多相关文章
- Beginning Linux Programming 学习--chapter 17 Programming KDE using QT
KDE: KDE,K桌面环境(K Desktop Environment)的缩写.一种著名的运行于 Linux.Unix 以及FreeBSD 等操作系统上的自由图形桌面环境,整个系统采用的都是 Tro ...
- linux视频学习3(shell和网络)
1.shell的学习. shell的种类比较多,主要有三种: /bin/sh, /bin/csh, /bin/ksh. 查看当前使用的是哪种shell : 命令env (显示当前操作系统的环境变量). ...
- Linux 学习 (八) Shell
Linux达人养成计划 I 学习笔记 Shell 是什么: Shell 是一个命令解释器 Shell 还是一个功能相当强大的编程语言,易编写,易调试,灵活性较强 Shell 的分类: Bourne S ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- linux 学习10 shell 基础
10.1 Shell概述 .Shell是什么 Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动.挂起.停止甚至是编写一 ...
- Linux基础学习(10)--Shell基础
第十章——Shell基础 一.Shell概述 1.Shell是什么: (1)Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来 ...
- 别出心裁的Linux系统调用学习法
别出心裁的Linux系统调用学习法 操作系统与系统调用 操作系统(Operating System,简称OS)是计算机中最重要的系统软件,是这样的一组系统程序的集成:这些系统程序在用户对计算机的使用中 ...
- Linux驱动学习步骤(转载)
1. 学会写简单的makefile 2. 编一应用程序,可以用makefile跑起来 3. 学会写驱动的makefile 4. 写一简单char驱动,makefile编译通过,可以insmod, ls ...
- 关于Linux内核学习的误区以及相关书籍介绍
http://www.hzlitai.com.cn/article/ARM9-article/system/1605.html 写给Linux内核新手-关于Linux内核学习的误区 先说句正经的:其实 ...
随机推荐
- P2736 “破锣摇滚”乐队 Raucous Rockers
题目描述 你刚刚继承了流行的“破锣摇滚”乐队录制的尚未发表的N(1 <= N <= 20)首歌的版权.你打算从中精选一些歌曲,发行M(1 <= M <= 20)张CD.每一张C ...
- TensorFlow(九):卷积神经网络
一:传统神经网络存在的问题 权值太多,计算量太大 权值太多,需要大量样本进行训练 二:卷积神经网络(CNN) CNN通过感受野和权值共享减少了神经网络需要训练的参数个数. 三:池化 四:卷积操作 五: ...
- [bzoj 2653][国家集训队]middle
传送门 Description 一个长度为\(n\)的序列\(a\),设其排过序之后为\(b\),其中位数定义为\(b[n/2]\),其中\(a,b\)从\(0\)开始标号,除法取下整. 给你一个长度 ...
- Educational Codeforces Round 49 (Rated for Div. 2)
题目链接 还缺F和G,至少上橙之后把F补了吧. A - Palindromic Twist 题意:每个字母恰好操作一次,变成其之前或者其之后的一个字母,注意'a'和'z'不互通,求是否可以变成回文串. ...
- tomcat设置gzip
使用tomcat发布3dtiles或terrain数据遇到的gzip问题 问题一 对大于1KB的json请求进行gzip压缩,json为原文件 1.创建原始文件 2.设置 在apache-tomcat ...
- docker容器里面执行top报“TERM environment variable not set.”
解决: [hadoop@master ~]$ docker exec -ti 6eca7d27a988 /bin/bashroot@6eca7d27a988:/# topTERM environmen ...
- Layui + 微信小程序 + PC端 + GatewayWorker + 移动端即时聊天系统
- Linux CentOS 使用Yum源安装MySQL 5.7
在CentOS(Fedora.RedHat)系统中,可以使用yum install mysql命令来安装MySQL,但所安装的MySql版本一般都较旧,所以更推荐通过源码编译安装或下载最新rpm安装包 ...
- html 与 xml 的区别与联系
[引言] 前一阵子刚刚学习了html(HyperText Markup Language),最近又接触了xml(Extensible Markup Language),它们之间有什么联系和区别呢?现在 ...
- Cesium获取经度 ,纬度,高度
实例代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...