UNIX Tutorial Three

3.1 Redirection

Most processes initiated by UNIX commands write to the standard output (that is, they write to the terminal screen), and many take their input from the standard input (that is, they read it from the keyboard). There is also the standard error, where processes write their error messages, by default, to the terminal screen.

We have already seen one use of the cat command to write the contents of a file to the screen.

Now type cat without specifing a file to read

% cat

Then type a few words on the keyboard and press the [Return] key.

Finally hold the [Ctrl] key down and press [d] (written as ^D for short) to end the input.

What has happened?

If you run the cat command without specifing a file to read, it reads the standard input (the keyboard), and on receiving the ‘end of file‘ (^D), copies it to the standard output (the screen).

In UNIX, we can redirect both the input and the output of commands.

3.2 Redirecting the Output

We use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of fruit, type

% cat > list1

Then type in the names of some fruit. Press [Return] after each one.

pear
banana
apple
^D {this means press [Ctrl] and [d] to stop}

What happens is the cat command reads the standard input (the
keyboard) and the > redirects the output, which normally goes to the
screen, into a file called list1

To read the contents of the file, type

% cat list1

Exercise 3a

Using the above method, create another file called list2 containing
the following fruit: orange, plum, mango, grapefruit. Read the contents of list2

3.2.1 Appending to a file

The form >> appends standard output to a file. So to add more items to
the file list1, type

% cat >> list1

Then type in the names of more fruit

peach
grape
orange
^D (Control D to stop)

To read the contents of the file, type

% cat list1

You should now have two files. One contains six fruit, the other contains four
fruit.

We will now use the cat command to join (concatenate) list1 and list2 into a new file called biglist.
Type

% cat list1 list2 > biglist

What this is doing is reading the contents of list1 and list2
in turn, then outputing the text to the file biglist

To read the contents of the new file, type

% cat biglist

3.3 Redirecting the Input

We use the < symbol to redirect the input of a command.

The command sort alphabetically or numerically sorts a list.
Type

% sort

Then type in the names of some animals. Press [Return] after
each one.

dog
cat
bird
ape
^D (control d to stop)

The output will be

ape
bird
cat
dog

Using < you can redirect the input to come from a file rather than the
keyboard. For example, to sort the list of fruit, type

% sort < biglist

and the sorted list will be output to the screen.

To output the sorted list to a file, type,

% sort < biglist > slist

Use cat to read the contents of the file slist

3.4 Pipes

To see who is on the system with you, type

% who

One method to get a sorted list of names is to type,

% who > names.txt
% sort < names.txt

This is a bit slow and you have to remember to remove the temporary file called
names when you have finished. What you really want to do is connect the output
of the who command directly to the input of the sort
command. This is exactly what pipes do. The symbol for a pipe is the vertical
bar |

For example, typing

% who | sort

will give the same result as above, but quicker and cleaner.

To find out how many users are logged on, type

% who | wc -l

Exercise 3b

Using pipes, display all lines of list1 and list2
containing the letter ‘p‘, and sort the result.

Answer available here

Summary

Command Meaning
command > file redirect standard output to a file
command >> file append standard output to a file
command < file redirect standard input from a file
command1 | command2 pipe the output of command1 to the input of command2
cat file1 file2 > file0 concatenate file1 and file2 to file0
sort sort data
who list users currently logged in
时间: 2024-08-02 15:13:13

UNIX Tutorial Three的相关文章

Unix Tutorial Eight

1.UNIX 变量 变量是在运行时将信息从shell传递到程序的一种方式.程序在特定的变量中查找"在环境中",如果发现它们将使用存储的值.有些是由系统设置的,另一些是由你设置的,还有一些是由shell设置的,或者是任何加载另一个程序的程序. 标准UNIX变量分为两类,环境变量和shell变量.笼统地说,shell变量仅适用于shell的当前实例,并用于设置短期工作条件:环境变量具有更深远的意义,登录时设置的会话在会话期间是有效的.按照惯例,环境变量有UPPER CASE,shell变量

UNIX Tutorial Four

4.1 Wildcards The * wildcard The character * is called a wildcard, and will match against none or more character(s) in a file (or directory) name. For example, in your unixstuff directory, type % ls list* This will list all files in the current direc

Linux/Unix 新手和专家教程

你正在找一些高质量的Linux 和 UNIX 的教程吗?如果是,这篇文章会告诉你到哪去找到这些教程.这里我们将给出超过30个相当的不错的 Linux 和 UNIX 在线的教程. 需要大家注意的是,他们都是英文的,也许有一些也经被翻译到了中文社区,你可以搜索一下.但不管怎么样,我的建议是应该尽可能的去阅读英文. Linux 和UNIX 的新手培训教程 免费的新手Linux教程 Introduction to Linux – 这是来自IBM的教程,用于给那些想学习Linux的人. Linux Des

Github 的一个免费编程书籍列表

Index Ada Agda Alef Android APL Arduino ASP.NET MVC Assembly Language Non-X86 AutoHotkey Autotools Awk Bash Basic BETA C C# C++ Chapel Cilk Clojure COBOL CoffeeScript ColdFusion Cool Coq D Dart DB2 Delphi / Pascal DTrace Elasticsearch Emacs Erlang F#

TensorFlow学习路径【转】

作者:黄璞链接:https://www.zhihu.com/question/41667903/answer/109611087来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 前言:其实TensorFlow本身仅仅是一个分布式的高性能计算框架,想要用TF做深度学习,仅仅学习这个框架本身是没有太大意义的.因此应该将TF看作技术路线中的一个核心点,去掌握整个开发所需要的必要技术,知识.尤其是深度学习的基本原理,这对日后搭建模型,模型调参以至提出新的模型都是极其有用的.

Linux学习,在线版

考虑加入Linux教派,最初被Linux吸引是看了<Unix编程艺术>,虽然里面的个别非常教条,极端.但是里面大部分的设计思想我还是认同的. 下面是我整理的一些Linux资料,其实我打算折腾ubuntu或者Arch的. 来自于IBM在线技术社区的: Linux 编程和系统管理新手入门 http://www.ibm.com/developerworks/cn/linux/newto/index.html 鸟哥的 Linux 私房菜 -- 基础学习篇目录 http://vbird.dic.ksu.

计算机类免费电子书共享

列表最早来自stackoverflow上的一个问题:List of freely available programming books 现在在github上进行维护:free-programming-books List of Free Programming Books This list initially was a clone of stackoverflow - List of freely available programming books by George Stocker.

Github上的1000多本免费电子书重磅来袭!

这个GIthub库的免费电子书资源绝对值得你拥有,赶紧收藏吧! 以前 StackOverFlow 也给出了一个免费电子书列表,现在在Github上可以看到时刻保持更新的列表了. 瞥一眼下面的书籍分类目录,你就能知道这个免费电子书库的含金量了吧.记得一定要看几本,千万别下载了大量书籍而束之高阁! 行动重于空想! Github地址:     https://github.com/vhf/free-programming-books/blob/master/free-programming-books

akka---Getting Started Tutorial (Java): First Chapter

原文地址:http://doc.akka.io/docs/akka/2.0.2/intro/getting-started-first-java.html Introduction Welcome to the first tutorial on how to get started with Akka and Java. We assume that you already know what Akka and Java are and will now focus on the steps