zoukankan      html  css  js  c++  java
  • Learning_the_bash_Shell_Third_Edition 8/n

    Command Substitution

    The command inside the parentheses is run, and anything the command writes to standard output is returned as the value of the expression. These constructs can be nested, i.e., the UNIX command can contain command substitutions.

     Here are some simple examples:

    • The value of $(pwd) is the current directory (same as the environment variable $PWD).

    • The value of $(ls $HOME) is the names of all files in your home directory.

    • The value of $(ls $(pwd)) is the names of all files in the current directory.

    • The value of $(< alice) is the contents of the file alice with any trailing newlines removed. 

    • To find out detailed information about a command if you don’t know where its file resides, type ls -l $(type -path -all command-name). The -all option forces type to do a pathname look-up and -path causes it to ignore keywords, built-ins, etc.

    • If you want to edit (with vi) every chapter of your book on bash that has the phrase “command substitution,” assuming that your chapter files all begin with ch, you could type:

    vi $(grep -l 'command substitution' ch*)
    

      The -l option to grep prints only the names of files that contain matches.

    “When in doubt, use single quotes, unless the string contains variables or command substitutions, in which case use double quotes.”

    Task 4-5

    The file used in Task 4-1 is actually a report derived from a bigger table of data about albums. This table consists of several columns, or fields, to which a user refers by names like “artist,” “title,” “year,” etc. The columns are separated by vertical bars (|, the same as the UNIX pipe character). To deal with individual columns in the table, field names need to be converted to field numbers.

     

    Suppose there is a shell function called getfield that takes the field name as argument and writes the corresponding field (or column) number on the standard output. Use this routine to help extract a column from the data table.

     cut -f4 -d| albums
    1981
    1984
    1989
    1990
    1993
    
    (lqdjango0601) cor@debian:~/shell$ cat albums 
    Depeche Mode|Speak and Spell|Mute Records|1981
    Depeche Mode|Some Great Reward|Mute Records|1984
    Depeche Mode|101|Mute Records|1989
    Depeche Mode|Violator|Mute Records|1990
    Depeche Mode|Songs of Faith and Devotion|Mute Records|1993
    

      

  • 相关阅读:
    HTML5 WebSocket 技术介绍
    腾迅平台接入笔记
    Windows 2008 R2 64位上安装wamp失败的原因
    海伦公式
    ANE接入平台心得记录(安卓)
    ANE原生代码的调试(安卓)
    一行代码远离Google浏览器兼容问题的困扰
    U3D的飞船太空射击例子中,使用coroutine
    这几天在搞UNITY3D,感觉回到了AS2
    网页动物园2.0发布,经过几个月的努力,采用JAVA编写!
  • 原文地址:https://www.cnblogs.com/winditsway/p/14475973.html
Copyright © 2011-2022 走看看