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

  

 

Learning_the_bash_Shell_Third_Edition 8/n

上一篇:如果你不懂.net逆向,又对它感兴趣


下一篇:ubuntu16.04 + vscode + cmake + hi3519 编译sample示例