Jan14

shell输入与输出高阶知识点汇总

Author: leeon  Click: 105   Comments: 0 Category: linux  Tag: bash,shell,stdio,stdout,stderr,io

shell输入输出基本知识点:

 a、I/O重定向通常与 FD有关,shell的FD通常为10个,即 0~9;
b、常用FD有3个,为0(stdin,标准输入)、1(stdout,标准输出)、2(stderr,标准错误输出),默认与keyboard、monitor、monitor有关;
c、用 < 来改变读进的数据信道(stdin),使之从指定的档案读进;
d、用 > 来改变送出的数据信道(stdout, stderr),使之输出到指定的档案;
e、0 是 < 的默认值,因此 < 与 0<是一样的;同理,> 与 1> 是一样的;
f、在IO重定向 中,stdout 与 stderr 的管道会先准备好,才会从 stdin 读进资料;
g、管道“|”(pipe line):上一个命令的 stdout 接到下一个命令的 stdin;
h、tee 命令是在不影响原本 I/O 的情况下,将 stdout 复制一份到档案去;
i、bash(ksh)执行命令的过程:分析命令-变量求值-命令替代(``和$( ))-重定向-通配符展开-确定路径-执行命令;
j、( ) 将 command group 置于 sub-shell 去执行,也称 nested sub-shell,它有一点非常重要的特性是:继承父shell的Standard input, output, and error plus any other open file descriptors。
k、exec 命令:常用来替代当前 shell 并重新启动一个 shell,换句话说,并没有启动子 shell。使用这一命令时任何现有环境都将会被清除,。exec 在对文件描述符进行操作的时候,也只有在这时,exec 不会覆盖你当前的 shell 环境。

 

IO文件描述符:

>&n 使用系统调用 dup (2) 复制文件描述符 n 并把结果用作标准输出
<&n 标准输入复制自文件描述符 n
<&- 关闭标准输入(键盘)
>&- 关闭标准输出
n<&- 表示将 n 号输入关闭
n>&- 表示将 n 号输出关闭
上述所有形式都可以前导一个数字,此时建立的文件描述符由这个数字指定而不是缺省的 0 或 1。如:
... 2>file 运行一个命令并把错误输出(文件描述符 2)定向到 file。
... 2>&1 运行一个命令并把它的标准输出和输出合并。(严格的说是通过复制文件描述符 1 来建立文件描述符 2 ,但效果通常是合并了两个流。)

 

exec命令使用技巧:

exec 0<infilename # 打开文件infilename作为 stdin
exec 1>outfilename # 打开文件outfilename作为stdout
exec 2>errfilename # 打开文件 errfilename作为 stderr
exec 0<&- # 关闭 FD0
exec 1>&- # 关闭 FD1
exec 5>&- # 关闭 FD5

Sep28

rsync使用小记

Author: leeon  Click: 491   Comments: 0 Category: linux  Tag: rsync

1. rsync 修改配置文件后无须重启rsyncd进程即可立即生效。

2. --delete参数在差量更行目录文件的时候切忌远端和目的端均不需要在目录后面加“*”,例如/aaa/bbb/*

Aug27

【原创】关于shell中变量作用域的思考

Author: leeon  Click: 739   Comments: 0 Category: linux  Tag: shell,linux

执行一个shell脚本会在当前shell中派生一个子shell去执行任务。同理,如果一个shell脚本中调用另一个shell,也是会在执行到子shell这一步的时候再次派生一个子shell去处理脚本。例如a.sh脚本有如下信息:

[code="bash"]
#!/bin/bash
echo $$
./b.sh
[/code]

b.sh中有:

[code="bash"]
#!/bin/bash
echo $$
[/code]

执行a.sh会发现输出两行不同的进程ID,验证了每个sh在执行的时候会派生一个子shell去执行任务。

在脚本之间调用我们或许会遇到变量之间的调用关系,这里我们需要注意一下几个场景的应用:

场景1, 当前shell中创建一个变量传递给当前脚本派生的子shell,例如:

a.sh

[code="bash"]
#!/bin/bash
a="TEST"
./b.sh
[/code]

b.sh

[code="bash"]
#!/bin/bash
echo $a
[/code]

这时会发现在b.sh中是无法正常获取到a的变量值的,那么我们可以利用export命令或者set -a命令来使当前shell的变量激活到派生的子shell中。

我们只需要在a.sh中加入

[code="bash"]
#!/bin/bash
export a="TEST"
[/code]

或者

[code="bash"]
#!/bin/bash
set -a
a="TEST"
[/code]

 

场景2,派生的子shell中的变量在当前父shell中使用,例如:

a.sh

[code="bash"]
#!/bin/bash
./b.sh
echo $b
[/code]

b.sh

[code="bash"]
#!/bin/bash
b="TEST"
[/code]

我们来执行a.sh会发现输出的b变量为空。那么我们可以使用source命令或者.(点号)来激活子shell中的变量使其能在父shell中使用。

我们只需要修改a.sh,在调用b.sh前加上特殊命令或字符即可,例如:

[code="bash"]
#!/bin/bash
. ./b.sh
#或者
source ./b.sh
[/code]

Jun8

【原创】利用iftop查看进程产生的网路流量

Author: leeon  Click: 963   Comments: 0 Category: linux  Tag: iftop,linux,network

当机器的出入流量有异常的时候,我们可以利用iftop工具查看每个进程的网络流量情况。iftop工具具有强大的网络流量分析能力。编译安装iftop工具前需要在机器上安装:libpcap ,libcurses。

iftop功能强大,这里我们只需要执行

 

[code="bash"]
iftop -P
[/code]

查看出来的流量是前2秒,10秒,40秒的平均流量,每个连接会显示两行数据,第一行是出流量,第二行是入流量数据,我们只用关心当前2秒的平均流量就能判断此时此刻哪个进程使用的网络IO较大。

不带参数效果:

带参数查看端口号效果:

界面操作命令如图所示

 

命令即可查看每个端口的网络流量情况,这样再通过netstat反查每个端口对应的进程名即可!

附上官方使用说明:

When running, iftop uses the whole screen to display network usage. At the top of the display is a logarithmic scale for the bar graph
which gives a visual indication of traffic.

The main part of the display lists, for each pair of hosts, the rate at which data has been sent and received over the preceding 2, 10
and 40 second intervals. The direction of data flow is indicated by arrows, <= and =>. For instance,

foo.example.com => bar.example.com 1Kb 500b 100b
<= 2Mb 2Mb 2Mb

shows, on the first line, traffic from foo.example.com to bar.example.com; in the preceding 2 seconds, this averaged 1Kbit/s, around
half that amount over the preceding 10s, and a fifth of that over the whole of the last 40s. During each of those intervals, the data
sent in the other direction was about 2Mbit/s. On the actual display, part of each line is inverted to give a visual indication of the
10s average of traffic. You might expect to see something like this where host foo is making repeated HTTP requests to bar, which is
sending data back which saturates a 2Mbit/s link.

By default, the pairs of hosts responsible for the most traffic (10 second average) are displayed at the top of the list.

At the bottom of the display, various totals are shown, including peak traffic over the last 40s, total traffic transferred (after
filtering), and total transfer rates averaged over 2s, 10s and 40s.

分类

标签

归档

最新评论

王爷在21:32:04评论了
【原创】获取jQuery中Ajax函数的返回值的方法
Funny在10:22:51评论了
shell数组使用方法小记
kevinems在11:30:08评论了
【原创】使用gitosis和tortoisegit打造自己的git服务
candy在13:11:40评论了
【原创】beautifulsoup解析中文网页乱码解决
thenbsp.com在16:17:48评论了
中国邮政EMS就是一坨屎

我看过的书

链接

其他

访问本站种子 本站平均热度:1218 c° 本站链接数:55 个 本站标签数:250 个 本站被评论次数:33 次