Unix script 12 - extrnal programs 外部程序
来源:互联网 发布:c指针编程之道 编辑:程序博客网 时间:2024/06/05 10:30
External programs are often used within shell scripts; there are a fewbuiltin commands (echo
,which
, andtest
are commonly builtin), but many useful commands are actually Unix utilities,such astr
,grep
, expr
and cut
.
The backtick (`)is also often associated with external commands. Because of this, we will discuss the backtick first.
The backtick is used to indicate that the enclosed text is to be executedas a command. This is quite simple to understand. First, use aninteractive shell to read your full name from/etc/passwd
:
$ grep "^${USER}:" /etc/passwd | cut -d: -f5Steve Parker
Now we will grab this output into a variable which we can manipulate more easily:
$ MYNAME=`grep "^${USER}:" /etc/passwd | cut -d: -f5`$ echo $MYNAMESteve Parker
So we see that the backtick simply catches the standard output from anycommand or set of commands we choose to run. It can also improve performanceif you want to run a slow command or set of commands and parse various bits of its output:
#!/bin/shfind / -name "*.html" -print | grep "/index.html$"find / -name "*.html" -print | grep "/contents.html$"
This code could take a long time to run, and we are doing it twice!
A better solution is:
#!/bin/shHTML_FILES=`find / -name "*.html" -print`echo "$HTML_FILES" | grep "/index.html$"echo "$HTML_FILES" | grep "/contents.html$"Note: the quotes around
$HTML_FILES
are essential to preserve the newlines between each file listed. Otherwise,grep
will see one huge long line of text, and not one line per file.This way, we are only running the slow find
once, roughlyhalving the execution time of the script.
We discuss specific examples further in the Hints and Tips section of this tutorial.
- Unix script 12 - extrnal programs 外部程序
- TortoiseSVN -设置 - External Programs: Diff Viewer外部程序:差异查看器 - WinMerge
- [Script]List of Scheduled Concurrent Programs
- 【Unix/Linux.Shell Script】Shell Script程序的流程控制语句
- external script外部脚本
- Unix script - 7 Loop
- Unix script 9 - case
- Unix script 17 - examples
- 【Unix/Linux.Shell Script】Shell & Shell Script
- UNIX的批处理 Shell Script
- Unix script 5 - wildcards 星号
- Unix script - 6 escape chracter
- Unix script 15 - quick reference
- Unix script 16 - interactive shell
- 【Unix/Linux.Shell Script】函数
- 1.6.程序和进程(Programs and Processes)
- Python Simple Programs 简单小程序
- Unix command-line interface programs and shell builtins
- 中国茶叶的影响
- BOP - 计算32位整型数中的1的个数
- 第12周报告3
- 学习jQuery的注意事项
- VC Studio6.0 使用技巧大全
- Unix script 12 - extrnal programs 外部程序
- SQL对Xml字段的操作
- 记录页面的cookie值
- flex中flash.events和mx.events的事件的区别
- VB图形绘制
- Tomcat ClassLoad加载机制
- PHP.ini配置文件中文详解
- 通过Spring Mail Api发送邮件
- IME管理基本函数