一些常用的shell模式
在shell中有一些模式经常使用,暂时总解了几点:
就可以根据cpu(或核)的多少,将进程启动到后台,然后,检测进程数上限,
自适应地启动新进程或睡眠等待任务。 这种模式在多核或多路CPU上
充分利用计算资源/加快程序执行速度时尤其有用。
for f in `cat $1`
do
gzip -d $f & # 这里要注意后台启动进程
n=`ps | wc -l`
while [ $n -gt 35 ] #保持35-5个gzip 进程
do
sleep 1
n=`ps | wc -l`
done
done
done
2. tr 合并与断行
适用性:有些文本处理中,同一条数据处于多行中,不同条数据之间用空行分开
下边的脚本将/n替换成| 然后使用||标记分开各条数据,处理完之后,将数据恢复
tr '/n' '|' | tr ';' '/n' | grep CREATE | sed 's//|/|//
/g'/
| grep -v -i error | sed -n '13,$p'| sed 's/$/;/' | tr '|' '/n'
使用tr 可以避免使用sed 的复杂语句,且tr 不存在每行最大字符数限制
3.对进程的输入流(stdin)的编辑
使用shell的输入流定向,可以实现对输入流的编辑,
比如:下边的 <<EOF的意思就是:以下的 shell命令全部定向为ftp的输入命令
直到遇到行首为EOF的行
下边的脚本是使用ftp 上传指定目录下的所有指定文件
ftp -i -v -n $target_host <<EOF >>$log_file
user $ftp_usr $ftp_passwd
cd $host_dir
pwd
lcd $local_dir
mput $up_files
bye
EOF
再有就是使用sqlplus 直接导出Oracle数据库的数据,变成一定格式
(可以直接被程序加载和使用)的文件,这个就不详述了。
在脚本编辑进程的stdin 可以省去临时文件,并且可以直接使用shell变量代入。
下边就是一个演示了,充篇幅用的
--------------------------------
[of@lbaby q.table.idx]$ cat ftpk
ftp -i -n -v ftp.kernel.org <<EOF
user anonymous lbaby@lbaby
cd pub
ls
bye
EOF
[of@lbaby q.table.idx]$ sh ftpk
Trying 130.239.17.4...
Connected to ftp.kernel.org (130.239.17.4).
220 Welcome to ftp.kernel.org.
331 Please specify the password.
230- Welcome to the
230-
230- LINUX KERNEL ARCHIVES
230- ftp.kernel.org
230-
230- "Much more than just kernels"
230-
230- IF YOU'RE ACCESSING THIS SITE VIA A WEB BROWSER
230- PLEASE USE THE HTTP URL BELOW INSTEAD!
230-
230-----> If you are looking for mirror sites, please go <----
230-----> to mirrors.kernel.org instead <----
230-
230-This site is provided as a public service by the Linux Kernel
230-Organization, a California nonprofit corporation. Bandwidth is
230-provided by The Internet Software Consortium, Inc. Our servers are
230-located in San Francisco and Palo Alto, California; Corvallis, Oregon;
230-Amsterdam, Netherlands and Ume氓, Sweden; use in violation of any
230-applicable laws strictly prohibited.
230-
230-Due to U.S. Exports Regulations, all cryptographic software on this
230-site is subject to the following legal notice:
230-
230- This site includes publicly available encryption source code
230- which, together with object code resulting from the compiling of
230- publicly available source code, may be exported from the United
230- States under License Exception "TSU" pursuant to 15 C.F.R. Section
230- 740.13(e).
230-
230-This legal notice applies to cryptographic software only. Please see
230-the Bureau of Industry and Security (http://www.bis.doc.gov/) for more
230-information about current U.S. regulations.
230-
230-Neither the Linux Kernel Organization, nor its sponsors make any
230-guarantees, explicit or implicit, about the contents of this site.
230-Use at your own risk.
230-
230-This site is accessible via the following mechanisms:
230-
230- FTP ftp://ftp.kernel.org/pub/
230- HTTP http://www.kernel.org/pub/
230- RSYNC rsync://rsync.kernel.org/pub/
230-
230-NFS and SMB/CIFS are no longer available.
230-
230-For comments on this site, please contact <ftpadmin@kernel.org>.
230-Please do not use this address for questions that are not related to
230-the operation of this site. Please see our homepage at
230-http://www.kernel.org/ for links to Linux documentation resources.
230-
230 Login successful.
250 Directory successfully changed.
227 Entering Passive Mode (130,239,17,4,180,103).
150 Here comes the directory listing.
drwxrws--- 2 536 536 71 Sep 23 23:53 RCS
-r--r--r-- 1 536 536 1912 Aug 05 2007 README
-r--r--r-- 1 536 536 578 Mar 18 2003 README_ABOUT_BZ2_FILES
drwxrwsr-x 7 536 536 93 Jul 22 2005 dist
-r--r--r-- 1 536 536 2322 Sep 23 23:53 index.html
drwxrwsr-x 9 536 536 106 Jun 25 00:37 linux
drwxr-x--- 2 536 536 6 Oct 27 1998 lost+found
drwxrwsr-x 3 536 536 18 Sep 23 23:35 media
drwxrwsr-x 19 536 536 4096 Jun 02 18:27 scm
drwxrwsr-x 3 536 536 59 Nov 05 2003 site
drwxrwsr-x 12 536 536 143 Sep 26 23:43 software
drwxr-sr-x 3 536 536 22 Apr 30 22:31 tools
226 Directory send OK.
221 Goodbye.
[of@lbaby q.table.idx]$
- 并行
就可以根据cpu(或核)的多少,将进程启动到后台,然后,检测进程数上限,
自适应地启动新进程或睡眠等待任务。 这种模式在多核或多路CPU上
充分利用计算资源/加快程序执行速度时尤其有用。
for f in `cat $1`
do
gzip -d $f & # 这里要注意后台启动进程
n=`ps | wc -l`
while [ $n -gt 35 ] #保持35-5个gzip 进程
do
sleep 1
n=`ps | wc -l`
done
done
done
2. tr 合并与断行
适用性:有些文本处理中,同一条数据处于多行中,不同条数据之间用空行分开
下边的脚本将/n替换成| 然后使用||标记分开各条数据,处理完之后,将数据恢复
tr '/n' '|' | tr ';' '/n' | grep CREATE | sed 's//|/|//
/g'/
| grep -v -i error | sed -n '13,$p'| sed 's/$/;/' | tr '|' '/n'
使用tr 可以避免使用sed 的复杂语句,且tr 不存在每行最大字符数限制
3.对进程的输入流(stdin)的编辑
使用shell的输入流定向,可以实现对输入流的编辑,
比如:下边的 <<EOF的意思就是:以下的 shell命令全部定向为ftp的输入命令
直到遇到行首为EOF的行
下边的脚本是使用ftp 上传指定目录下的所有指定文件
ftp -i -v -n $target_host <<EOF >>$log_file
user $ftp_usr $ftp_passwd
cd $host_dir
pwd
lcd $local_dir
mput $up_files
bye
EOF
再有就是使用sqlplus 直接导出Oracle数据库的数据,变成一定格式
(可以直接被程序加载和使用)的文件,这个就不详述了。
在脚本编辑进程的stdin 可以省去临时文件,并且可以直接使用shell变量代入。
下边就是一个演示了,充篇幅用的
--------------------------------
[of@lbaby q.table.idx]$ cat ftpk
ftp -i -n -v ftp.kernel.org <<EOF
user anonymous lbaby@lbaby
cd pub
ls
bye
EOF
[of@lbaby q.table.idx]$ sh ftpk
Trying 130.239.17.4...
Connected to ftp.kernel.org (130.239.17.4).
220 Welcome to ftp.kernel.org.
331 Please specify the password.
230- Welcome to the
230-
230- LINUX KERNEL ARCHIVES
230- ftp.kernel.org
230-
230- "Much more than just kernels"
230-
230- IF YOU'RE ACCESSING THIS SITE VIA A WEB BROWSER
230- PLEASE USE THE HTTP URL BELOW INSTEAD!
230-
230-----> If you are looking for mirror sites, please go <----
230-----> to mirrors.kernel.org instead <----
230-
230-This site is provided as a public service by the Linux Kernel
230-Organization, a California nonprofit corporation. Bandwidth is
230-provided by The Internet Software Consortium, Inc. Our servers are
230-located in San Francisco and Palo Alto, California; Corvallis, Oregon;
230-Amsterdam, Netherlands and Ume氓, Sweden; use in violation of any
230-applicable laws strictly prohibited.
230-
230-Due to U.S. Exports Regulations, all cryptographic software on this
230-site is subject to the following legal notice:
230-
230- This site includes publicly available encryption source code
230- which, together with object code resulting from the compiling of
230- publicly available source code, may be exported from the United
230- States under License Exception "TSU" pursuant to 15 C.F.R. Section
230- 740.13(e).
230-
230-This legal notice applies to cryptographic software only. Please see
230-the Bureau of Industry and Security (http://www.bis.doc.gov/) for more
230-information about current U.S. regulations.
230-
230-Neither the Linux Kernel Organization, nor its sponsors make any
230-guarantees, explicit or implicit, about the contents of this site.
230-Use at your own risk.
230-
230-This site is accessible via the following mechanisms:
230-
230- FTP ftp://ftp.kernel.org/pub/
230- HTTP http://www.kernel.org/pub/
230- RSYNC rsync://rsync.kernel.org/pub/
230-
230-NFS and SMB/CIFS are no longer available.
230-
230-For comments on this site, please contact <ftpadmin@kernel.org>.
230-Please do not use this address for questions that are not related to
230-the operation of this site. Please see our homepage at
230-http://www.kernel.org/ for links to Linux documentation resources.
230-
230 Login successful.
250 Directory successfully changed.
227 Entering Passive Mode (130,239,17,4,180,103).
150 Here comes the directory listing.
drwxrws--- 2 536 536 71 Sep 23 23:53 RCS
-r--r--r-- 1 536 536 1912 Aug 05 2007 README
-r--r--r-- 1 536 536 578 Mar 18 2003 README_ABOUT_BZ2_FILES
drwxrwsr-x 7 536 536 93 Jul 22 2005 dist
-r--r--r-- 1 536 536 2322 Sep 23 23:53 index.html
drwxrwsr-x 9 536 536 106 Jun 25 00:37 linux
drwxr-x--- 2 536 536 6 Oct 27 1998 lost+found
drwxrwsr-x 3 536 536 18 Sep 23 23:35 media
drwxrwsr-x 19 536 536 4096 Jun 02 18:27 scm
drwxrwsr-x 3 536 536 59 Nov 05 2003 site
drwxrwsr-x 12 536 536 143 Sep 26 23:43 software
drwxr-sr-x 3 536 536 22 Apr 30 22:31 tools
226 Directory send OK.
221 Goodbye.
[of@lbaby q.table.idx]$