惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
P
Proofpoint News Feed
H
Hacker News: Front Page
P
Privacy & Cybersecurity Law Blog
月光博客
月光博客
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
Forbes - Security
Forbes - Security
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
博客园 - 叶小钗
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
O
OpenAI News
G
Google Developers Blog
Martin Fowler
Martin Fowler
罗磊的独立博客
S
SegmentFault 最新的问题
T
Tor Project blog
量子位

博客园 - xiaoyixy

WeakHashMap相关 Java常见问题[转] MONO,原来你是水中月 Lucene 搜索引擎倒排索引原理 PXE Network Boot and Install Linux over NFS server 让进程在后台运行方法汇总 用 Spreadsheet::ParseExcel处理中文excel文件 IBM terminology abstraction Perl Note(2) 亲爱的,我想念你 Perl命令行应用 深入PAM Perl Note(1) GRUB awk学习 高手好习惯 Shell脚本 用户管理 re notes
Shell技巧
xiaoyixy · 2008-08-22 · via 博客园 - xiaoyixy

AIX Bourne Shell 的常用选项

标志描述
-a 导出所有已分配值的变量。
-c Variable 执行从变量 中读取的命令。
-e 当命令满足以下条件之一时立即退出:命令退出时返回比 0 大的值;命令不是 whileuntilif 结构的一部分;命令不经过 ANDOR 检测;或者命令不是管道前加感叹号。
-f 禁用所有文件名替换。
-h 定义函数时,定位和记住函数内部调用的所有命令。
-i 指定交互式 Shell。
-k 将所有关键字 都放入命令的环境。
-n 读取命令,但是不执行它们。
-r 调用受限制的 Shell。
-s 从标准输入读取命令,然后将输出写入标准错误(不包括 Shell 内置命令的输出)。
-t 读取并执行单个命令,然后退出。
-u 在脚本中,将所有未定义 变量视为错误。当尝试变量替换时退出。
-v 当读取输入行时将其显示出来。
-x 在执行命令之前显示其完整命令(包括所有的参数和选项)。

 使用 Shell 运算进行进制转换

$ echo $((013))
$ echo $((0xA4))

还可以使用以下格式指定 2 到 64 之间的任意进制:
$((BASE#NUMBER))
使用 here document 提供 Shell 脚本使用信息

                    
#!/bin/sh
cat << EOF
baseconv is a program to convert a number from one base to another.

Usage: baseconv [options]

Options:

-i BASE input base
-o BASE output base
-h display this message

For more information, consult the baseconv man page.
EOF

带前导缩进的 Shell 脚本 here document
                    
#!/bin/sh

cat <<- EOF
baseconv is a program to convert a number from one base to another.

Usage: baseconv [options]

Options:

-i BASE input base
-o BASE output base
-h display this message

For more information, consult the baseconv man page.
EOF

将内联输入发送到交互式程序

                    
$ bc << EOF
> ibase=16
> A
> EOF
10
$

可以在一个名为 subshell 的新 Shell 中执行一个或一组命令,当前 Shell 是 SubShell 的父 Shell。Subshell 继承父亲的环境。I/O 重定向可以出现在子 Shell 和父 Shell 之间,但是 Subshell 永远不能修改父环境。当您为了执行这些命令(比如设置变量)要更改 Shell 的环境,并且不想更改脚本自身运行所在的环境时,这就是您所期望的技术。当您想要同时在后台启动多个长时间运行的进程时也最好使用 Subshell。一个 Shell 可以生成多个 Subshell,而 Subshell 又可以循环生成属于它们自身的任意数量的 Subshell。

使用 read 读取一个变量

                    
$ read VAR
23
$ echo $VAR
23
$

在变量读取时使用提示

                    
$ read -p "Instead of $VAR, what number would you like? " VAR
Instead of 23, what number would you like? 17
$ echo $VAR
17
$

从管道读取

                    
$ ls | while read file; do ls $file; done
10
11
12
$

有用的单命令行程序

以下示例是执行有用功能的 Shell 单命令行程序样本。它们全部由本教程中描述的各种结构组成。

  • 从当前目录中获取一组文件名恰好为两个字符长的文件,并使用 .ppm 扩展名为其重新命名:

    for i in ??; { mv $i $i.ppm; }

  • 使用 tar 和 Subshell 复制整个目录树,同时保持相同的文件权限:

    ( cd source ; tar pcf - * ) | ( cd target ; tar pxvf - )

  • 读取二进制数并以十进制输出值:

    read BINLOC;echo $((2#$BINLOC))

  • 在 /usr/local 目录树中找到所有带 .mp3 扩展名的文件(这些文件的名称中可能包含空格字符),然后使用 bzip2 实用程序压缩这些文件:

    find /usr/local -name "*.mp3" | while read name ; do bzip2 $name; done

  • 将给定文件中所有十进制数的值以十六进制输出:

    cat file | while read number ; do echo $((0x$number)); done

  • 将给定文件中所有十进制数转换为十六进制的值,并将值输出到带有 .hex 扩展名的新文件中:

    cat file | while read number ; do echo $((0x$number)) >> file.hex; done

  • 构造重复十次的循环,以数字(从 0 到 90 以 10 递增)作为传递的参数运行 command

    i=0; while [ $i -ne 100 ]; do command $i; i=$(($i+10)); done

转换进制的简单脚本

                    
#!/bin/sh
# baseconv, convert numbers from one base to another.
#

NUMBER=1
while [ $NUMBER ]; do
read -p "Input base: " IN
read -p "Output base: " OUT
read -p "Number: " NUMBER
bc -ql <<- EOF
obase=$OUT
ibase=$IN
$NUMBER
EOF
done