简体   繁体   中英

How to execute Linux nested command line tersely?

Here nested command line means one command's output is another command's input. For example below:

$ CmdA

output1 output2 output3...

Now I want to run CmdB which use the output of CmdA as arguments. So How to run CmdB tersely instead of using

$ CmdB output1 output2 output3...

I have an actual problem now:

$ python-config --cflags --ldflags

-I/usr/include/python2.7 -I/usr/include/python2.7 -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fwrapv
-lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic

As you see, there are many items generated from command python-config. If I compile a .cpp source file, i have to write all the items like

gcc test.cpp -I/usr/include/python2.7 -fno-strict-aliasing -02 -g -pipe........-o test , So i just want to find a simple way to execute the caller command.

Thanks for tips!

gcc test.cpp `python-config --cflags --ldflags`

更多: 命令替换

You can give parameters to another command by using backticks or $() :

$ uname -r
2.6.38-020638rc5-generic
$ ls /lib/modules/`uname -r`/
build              modules.builtin.bin  modules.inputmap   modules.softdep
initrd             modules.ccwmap       modules.isapnpmap  modules.symbols
kernel             modules.dep          modules.ofmap      modules.symbols.bin
modules.alias      modules.dep.bin      modules.order      modules.usbmap
modules.alias.bin  modules.devname      modules.pcimap
modules.builtin    modules.ieee1394map  modules.seriomap
$ ls /lib/modules/$(uname -r)/
build              modules.builtin.bin  modules.inputmap   modules.softdep
initrd             modules.ccwmap       modules.isapnpmap  modules.symbols
kernel             modules.dep          modules.ofmap      modules.symbols.bin
modules.alias      modules.dep.bin      modules.order      modules.usbmap
modules.alias.bin  modules.devname      modules.pcimap
modules.builtin    modules.ieee1394map  modules.seriomap
$ 

Try gcc test.cpp $(python-config --cflags --ldflags) -o test

这就是你在说什么?

$ cmdA | while read line; do cmdB $line; done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM