`

Shell input and output

阅读更多
  • echo 命令可以显示文本行或变量 或者把字符串输入到文件
  •    echo [option] string
       -- -e 解析转义字符
        -- -n 回车不换行 linux系统默认回车换行
        -- 转义符(\c, \f, \t, \n)
       echo "Theo log files have all been done" > mylogfile.txt
       该命令可用于在脚本打印一些错误信息。
  • read 可以从键盘或文件的某一行文本中读入信息 并将其赋给一个变量
  •     read variable1 varible2
        -- 如果只指定了一个变量,那么read将会把所有的输入赋给该变量,真到遇到第一个
            文件结束符或回车;如果给出了多个变量,它们按顺序分别被赋对不同的变量。
            shell将用空格作为变量之间的分隔符。
  • cat 是一个简单而通用的命令,可以用它来显示文件内容,创建文件,还可以用它来
  •         显示控制字符。
        cat [options] filename1 ... filename2 ..
         -- -v 显示控制字符(例如dos中的某些字符)
          -- 使用该命令时要注意,它不会在文件分页符处停下来;它会一下显示完整个文件
              。如果希望每次显示一页,可以使用more命令或把cat命令的输出通过管道
              传递到另外一个具有分页功能的命令(more,less)中。
          -- cat myfile1 myfile2 myfile3 > myfile
  • 管道(|) 一个命令的输出做为别外一个命令的输入
  •    -- df -k|awk '{print $1}'|grep -v "Filesystem"
  • tee命令把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中。
  •    -- tee -a files
       --如果希望在看到输出的同时,也将其存入一个文件,这个命令再适合不过。
        --一般用于管道之后。
  • exec命令可以用来替代当前shell;换句话说,并没有启动子shell。使用这一命令
  •     时任何现有环境都会被清除,并重新启动一个shell.


    标准输入,输出和错误
  1. 在shell中执行命令时,每个进程都和三个打开的文件相联系,并使用文件描述符来
  2.    引用这些文件。由于文件描述符不容易记忆,shell同时也给出了相应的文件名。
            文件  ----------------- --------------文件描述符
       输入文件 -- 标准输入      |      0(缺省是键盘,也可以是文件或其它命令的输出)
       输出文件 -- 标准输出      |      1(缺省是屏幕,也可以是文件)
       错误文件 -- 标准错误      |      2(缺省是屏幕,也可以是文件)
  3. 系统中实际上有12个文件描述符,可以任意使用文件描述符3到9。
  4. 改变程序运动的输入和输出
  5.    command > filename  把标准输出重定向到一个新文件中
      command >> filename  同上(追加)
      command 1 > filename 把标准输出重定向到一个文件中
      command > filename 2>&1 把标准输出和错误一起重定向到一个文件
      command 2 >filename 把标准错误重定向到一个文件
      command 2 >> filename 把标准错误重定向到一个文件(追加)
      command >> filename 2>&1 把标准输出和错误一起重定向到一个文件(追加)
      command < filename > filename2 filename标准输入 f..2为标准输出
      command < filename 以filename为文件作为标准输入
      command << delimiter 从标准输入中读入直到遇到delimiter分界符
      command < &m 把文件描述符m做为标准输入
      command > &m 把文件描述符m做为标准输出
      command <&- 关闭标准输入

分享到:
评论

相关推荐

    shell-input-and-output.rar_hell

    &#8226 使用标准输入、标准输出及标准错误。 &#8226 重定向标准输入和标准输出。 本章全面讨论了s hell对数据和信息的标准输入、标准输出,对重定向也做了一定的介绍。

    Linux Shell Scripting Essentials 无水印pdf 0分

    Redirect input, output, and errors of a command or script execution to other streams Debug code with different shell debugging techniques to make your scripts bug-free Manage processes, along with the...

    《Learning the bash Shell,3rd Edition,by Cameron Newham》.pdf

    Input/Output and Command-Line Processing Chapter 8. Process Handling Chapter 9. Debugging Shell Programs Chapter 10. bash Administration Chapter 11. Shell Scripting Chapter 12. bash for Your System ...

    Pro.Bash.Programming.Scripting.the.GNULinux.Shell.2nd.Edition

    Chapter Two: Input, Output, and Throughput Chapter Three: Looping and Branching Chapter Four: Command-Line Parsing and Expansion Chapter Five: Parameters and Variables Chapter Six: Shell Functions ...

    concourse_input_output

    concourse_input_output

    bash Cookbook: Solutions and Examples for bash Users, 2nd Edition

    Shell scripting is a way to harness and customize the power of any Unix system, and an essential skill for Unix users, system administrators, and even professional Mac OS X developers. But beneath ...

    Linux Shell Scripting Cookbook

    Reading the output of a sequence of commands 38 Reading "n" characters without pressing Return 40 Field separators and iterators 41 Comparisons and tests 44 Chapter 2: Have a Good Command 49 ...

    How Linux Works: What Every Superuser Should Know, 2nd Edition

    You'll also explore the kernel and examine key system tasks inside user space, including system calls, input and output, and filesystems. With its combination of background, theory, real-world ...

    Mastering Linux Shell Scripting 2nd Edition

    shell. Then, you'll learn how to write a simple bash script and how to edit your bash script using Linux editors. Following this, you will learn how to define a variable and the visibility of a ...

    Pro Bash Programming

    Your First Shell Program * Input, Output, and Throughput * Looping and Branching * Command-Line Parsing and Expansion * Parameters and Variables * Shell Functions * String Manipulation * File ...

    Pro Bash Programming 2015

    Your First Shell Program * Input, Output, and Throughput * Looping and Branching * Command-Line Parsing and Expansion * Parameters and Variables * Shell Functions * String Manipulation * File ...

    数据结构 希尔(shell)排序

    用函数实现希尔(shell)排序,并输出每趟排序的结果,初始增量d=n/2,其后d=d/2 Input 第一行:键盘输入待排序关键的个数n 第二行:输入n个待排序关键字,用空格分隔数据 Output 每行输出一趟排序结果,数据之间用一个...

    Linux_slides.rar

    Shell Basics Module 7 — Shell Advanced Features Module 8 — File Name Generation Module 9— Quoting Module 10 — Input and Output Redirection Module 11 — Pipes Module 12 — Using ...

    BeagleBone.Cookbook.Software.and.Hardware.Problems.and.Solutions

    Manage real-time input/output (I/O) Work at the Linux I/O level with shell commands, Python, and C Compile and install Linux kernels Work at a high level with JavaScript and the BoneScript library ...

    The Linux Command Line: A Complete Introduction, 2nd Edition(Linux命令行大全,第二版)

    Use standard input and output, redirection, and pipelines Edit files with Vi, the world's most popular text editor Write shell scripts to automate common or boring tasks Slice and dice text files with...

    the linux command line

    and process management * Use standard input and output, redirection, and pipelines * Edit files with Vi, the world's most popular text editor * Write shell scripts to automate common or boring tasks ...

    The Linux Command Line

    and process management * Use standard input and output, redirection, and pipelines * Edit files with Vi, the world's most popular text editor * Write shell scripts to automate common or boring tasks ...

    Bash.Cookbook.2nd.Edition.14919

    Shell scripting is a way to harness and customize the power of any Unix system, and an essential skill for Unix users, system administrators, and even professional Mac OS X developers. But beneath ...

    how_linux_works_2nd_edition

    You’ll also explore the kernel and examine key system tasks inside user space, including system calls, input and output, and filesystems. With its combination of background, theory, real-world ...

    Linux for Beginners: An Introduction to the Linux Operating System

    How and why to redirect input and output from applications. How to customize your shell prompt. How to be efficient at the command line by using aliases, tab completion, and your shell history. How...

Global site tag (gtag.js) - Google Analytics