codecademy-command line-inputoutput
2015-08-20 16:56
621 查看
What happens when you type this command?
$ echo "Hello" HelloThe
echocommand accepts the string "Hello" asstandard input, and echoes the string "Hello" back to the terminal as standard output.Let's learn more about standard input, standard output, and standard error:standard input, abbreviated as
stdin, is information inputted into the terminal through the keyboard or input device.standard output, abbreviated as
stdout, is the information outputted after a process is run.standard error, abbreviated as
stderr, is an error message outputted by a failed process.Redirection reroutes standard input, standard output, and standard error to or from a different location.How does redirection work?
$ echo "Hello" > hello.txtThe
>command redirects the standard output to a file. Here,
"Hello"is entered as the standard input. The standard output
"Hello"is redirected by
>to the filehello.txt.
$ cat hello.txtThe
catcommand outputs the contents of a file to the terminal. When you type
cat hello.txt, the contents of hello.txt are displayed.
$ cat oceans.txt > continents.txt
>takes the standard output of the command on the left, and redirects it to the file on the right. Here the standard output of
cat oceans.txtis redirected tocontinents.txt.Note that
>overwrites all original content incontinents.txt. When you view the output data by typing
caton continents.txt, you will see only the contents of oceans.txt.
$ cat glaciers.txt >> rivers.txt
>>takes the standard output of the command on the left and appends (adds) it to the file on the right. You can view the output data of the file with
catand the filename.Here, the the output data of rivers.txt will contain
$ cat < lakes.txt
<takes the standard input from the file on the right and inputs it into the program on the left. Here,lakes.txt is the standard input for the
catcommand. The standard output appears in the terminal.the original contents of rivers.txt with the content ofglaciers.txt appended to it.
$ cat volcanoes.txt | wc
|is a "pipe". The
|takes the standard output of the command on the left, and pipes it as standard input to the command on the right. You can think of this as "command to command" redirection.Here the output of
cat volcanoes.txtis the standard input of
wc. in turn, the
wccommand outputs the number of lines, words, and characters in
volcanoes.txt, respectively.
$ cat volcanoes.txt | wc | cat > islands.txtMultiple
|s can be chained together. Here the standard output of
cat volcanoes.txtis "piped" to the
wccommand. The standard output of
wcis then "piped" to
cat. Finally, the standard output of
catis redirected to
islands.txt.You can view the output data of this chain by typing
cat islands.txt.
$ sort lakes.txt
sorttakes the standard input and orders it alphabetically for the standard output. Here, the lakes in
sort lakes.txtare listed in alphabetical order.
$ cat lakes.txt | sort > sorted-lakes.txtHere, the command takes the standard output from
cat lakes.txtand "pipes" it to
sort. The standard output of
sortis redirected to sorted-lakes.txt.You can view the output data by typing
caton the filesorted-lakes.txt.
$ uniq deserts.txt
uniqstands for "unique" and filters out adjacent, duplicate lines in a file. Here
uniq deserts.txtfilters out duplicates of "Sahara Desert", because the duplicate of 'Sahara Desert' directly follows the previous instance. The "Kalahari Desert" duplicates are not adjacent, and thus remain.
$ sort deserts.txt | uniqA more effective way to call
uniqis to call
sortto alphabetize a file, and "pipe" the standard output to
uniq. Here by piping
sort deserts.txtto
uniq, all duplicate lines are alphabetized (and thereby made adjacent) and filtered out.
sort deserts.txt | uniq > uniq-deserts.txtHere we simply send filtered contents to uniq-deserts.txt, which you can view with the
catcommand.
$ grep Mount mountains.txt
grepstands for "global regular expression print". It searches files for lines that match a pattern and returns the results. It is also case sensitive. Here,
grepsearches for "Mount" in mountains.txt.
$ grep -i Mount mountains.txt
grep -ienables the command to be case insensitive. Here,
grepsearches for capital or lowercase strings that match
Mountinmountains.txt.The above commands are a great way to get started with
grep. If you are familiar with regular expressions, you can use regular expressions to search for patterns in files.
$ grep -R Arctic /home/ccuser/workspace/geography
grep -Rsearches all files in a directory and outputs filenames and lines containing matched results.
-Rstands for "recursive". Here
grep -Rsearches the /home/ccuser/workspace/geographydirectory for the string "Arctic" and outputs filenames and lines with matched results.
$ grep -Rl Arctic /home/ccuser/workspace/geography
grep -Rlsearches all files in a directory and outputs only filenames with matched results.
-Rstands for "recursive" and
lstands for "files with matches". Here
grep -Rlsearches the/home/ccuser/workspace/geography directory for the string "Arctic" and outputs filenames with matched results.
$ sed 's/snow/rain/' forests.txt
sedstands for "stream editor". It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to "find and replace".Let's look at the expression
's/snow/rain/':
s: stands for "substitution". it is always used when using
sedfor substitution.
snow: the search string, the text to find.
rain: the replacement string, the text to add in place.In this case,
sedsearches forests.txt for the word "snow" and replaces it with "rain." Importantly, the above command will only replace the first instance of "snow" on a line.
$ sed 's/snow/rain/g' forests.txtThe above command uses the
gexpression, meaning "global". Here
sedsearches forests.txt for the word "snow" and replaces it with "rain", globally. All instances of "snow" on a line will be turned to "rain".Congratulations! You learned how to use the command line to redirect standard input and standard output. What can we generalize so far?Redirection reroutes standard input, standard output, and standard error.The common redirection commands are:
>redirects standard output of a command to a file, overwriting previous content.
>>redirects standard output of a command to a file, appending new content to old content.
<redirects standard input to a command.
|redirects standard output of a command to another command.A number of other commands are powerful when combined with redirection commands:
sort: sorts lines of text alphabetically.
uniq: filters duplicate, adjacent lines of text.
grep: searches for a text pattern and outputs it.
sed: searches for a text pattern, modifies it, and outputs it.nano is a command line text editor. It works just like a desktop text editor like TextEdit or Notepad, except that it is accessible from the command line and only accepts keyboard input.The command
nano hello.txtopens a new text file named hello.txt in the nano text editor.
"Hello, I am nano"is a text string entered in nano through the cursor.The menu of keyboard commands at the bottom of the window allow us to save changes to hello.txt and exit nano. The
^stands for the
Ctrlkey.
Ctrl+
Osaves a file. 'O' stands for output.
Ctrl+
Xexits the nano program. 'X' stands for exit.
Ctrl+
Gopens a help menu.
clearclears the terminal window, moving the command prompt to the top of the screen.In this lesson, we'll use nano to implement
相关文章推荐
- Web开发之PHP框架(一)-Laravel环境搭建
- ajax-php总结
- 虚拟化--002 powcli(图文)利用PowerCLI为ESXi_Cluster配置NTP服务器
- php cms 通用后台下载
- php加入xdebug后打开页面特别慢,不管是哪个页面
- thinkPHP上到服务器出现的一点问题
- php display_errors和error_reporting的赋值方法
- php socket
- php 16进制压缩
- ThinkPHP字符截取函数msubstr()
- sublime text配置php运行环境
- sublime text配置php运行环境
- PHP框架 CodeIgniter学习
- Atitit.ati dwr的原理and设计 attilax 总结 java php 版本
- Atitit.ati dwr的原理and设计 attilax 总结 java php 版本
- Atitit.ati dwr的原理and设计 attilax 总结 java php 版本
- jtextpanel中获取图片
- array_multisort — 对多个数组或多维数组进行排序
- 两段 PHP 代码比较优劣
- yii中将log存入数据库是修改原有风格