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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 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 - xiaoyixy Perl Note(1) GRUB awk学习 高手好习惯 Shell脚本 用户管理 - xiaoyixy re notes - xiaoyixy
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