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
    

      

  • 相关阅读:
    linux如何查看端口或服务被占用情况
    linux网络查看及配置相关命令
    linux查看程序运行相关命令
    shell脚本编写一个用真实用户去访问的vsftpd服务器
    shell脚本监控CPU和内存利用率
    小白的个人技能树(基于自动化软件测试开发实习和软件开发实习)
    MySQL 8.0.12 基于Windows 安装教程(超级详细)
    C语言 0x7fffffff是多少(也就是INT_MAX,首位是 0,其余都是1,f代表1111)
    数通知识点
    数据结构之算法基础
  • 原文地址:https://www.cnblogs.com/winditsway/p/14475973.html
Copyright © 2011-2022 走看看