[size=xx-large]Linux使用技巧大拼盘(四)[/size]
[size=large]grep显示行号[/size]
使用grep的`-n`选项可以显示行号:
[img]http://dl2.iteye.com/upload/attachment/0116/1931/3fbbfcd2-ce5b-354c-9524-38a812f1778e.png[/img]
[size=large]`/`的inode号是2[/size]
[size=large]`ls -F`[/size]
ls的manpage对`-F`选项的说明如下:
其中比较常见的是 `/`(目录),`*`(可执行文件)和`@`(符号链接)。下面是例子:
[img]http://dl2.iteye.com/upload/attachment/0116/1935/6c3b698f-ce0d-3f8f-80c0-3e7b352162a3.png[/img]
[size=large]getcwd[/size]
getcwd可以获取进程所在工作目录,类似`pwd`命令的效果:
[img]http://dl2.iteye.com/upload/attachment/0116/1941/a7686c05-9786-3288-8b06-f08d96d12184.png[/img]
[size=large]bash中查看数组中数据的索引号[/size]
[size=large]bash中制定数组中数据的标号[/size]
[size=large]bash中查看数组中某一元素的字串长度[/size]
[size=large]bash中查看数组包含的元素个数[/size]
[size=large]bash中的参数展开[/size]
通过使用`!`,我们可以让bash将传入函数的字串转变为变量名:
以下是执行结果:
[img]http://dl2.iteye.com/upload/attachment/0116/1937/0f0d8d8b-d213-3858-9835-889d342dc168.png[/img]
[size=large]bash中传入数组[/size]
[img]http://dl2.iteye.com/upload/attachment/0116/1939/f875804e-6ddf-3a25-96f1-9ea60c9095ae.png[/img]
更为可靠的方法是这样声明`foo`和`bar`:
其中`declare -a`就是把foo和bar强制声明为数组类型,然后`()`本身就是数组的声明格式。
[size=large]grep显示行号[/size]
使用grep的`-n`选项可以显示行号:
[img]http://dl2.iteye.com/upload/attachment/0116/1931/3fbbfcd2-ce5b-354c-9524-38a812f1778e.png[/img]
[size=large]`/`的inode号是2[/size]
power:/ weinanli$ ls -ldi /
2 drwxr-xr-x 40 root wheel 1428 Oct 9 08:28 /
[size=large]`ls -F`[/size]
ls的manpage对`-F`选项的说明如下:
-F Display a slash (`/') immediately after each pathname that is a
directory, an asterisk (`*') after each that is executable, an at
sign (`@') after each symbolic link, an equals sign (`=') after
each socket, a percent sign (`%') after each whiteout, and a ver-
tical bar (`|') after each that is a FIFO.
其中比较常见的是 `/`(目录),`*`(可执行文件)和`@`(符号链接)。下面是例子:
[img]http://dl2.iteye.com/upload/attachment/0116/1935/6c3b698f-ce0d-3f8f-80c0-3e7b352162a3.png[/img]
[size=large]getcwd[/size]
getcwd可以获取进程所在工作目录,类似`pwd`命令的效果:
[img]http://dl2.iteye.com/upload/attachment/0116/1941/a7686c05-9786-3288-8b06-f08d96d12184.png[/img]
[size=large]bash中查看数组中数据的索引号[/size]
power:~ weinanli$ foo=(a b c)
power:~ weinanli$ echo ${!foo[@]}
0 1 2
[size=large]bash中制定数组中数据的标号[/size]
power:~ weinanli$ foo=(a [5]=b c)
power:~ weinanli$ echo ${!foo[@]}
0 5 6
[size=large]bash中查看数组中某一元素的字串长度[/size]
power:~ weinanli$ foo=(a b c d e f g)
power:~ weinanli$ echo ${#foo[@]}
7
power:~ weinanli$ foo=(x xy xyz)
# 第一个元素`x`,长度为1
power:~ weinanli$ echo ${#foo[0]}
1
# 第二个元素`xy`,长度为2
power:~ weinanli$ echo ${#foo[1]}
2
# 第三个元素`xyz`,长度为2
power:~ weinanli$ echo ${#foo[2]}
3
[size=large]bash中查看数组包含的元素个数[/size]
power:~ weinanli$ foo=(a b c)
# 查看数组本身的元素个数用`[@]`引用数组本身即可
power:~ weinanli$ echo ${#foo[@]}
3
power:~ weinanli$ foo=(a b c d e f g)
power:~ weinanli$ echo ${#foo[@]}
7
[size=large]bash中的参数展开[/size]
通过使用`!`,我们可以让bash将传入函数的字串转变为变量名:
function not_expand() {
echo ${1}
}
function expand() {
echo ${!1}
}
foo=(a b c)
echo "NOT_EXPAND"
not_expand foo
not_expand foo[@]
echo "EXPAND"
expand foo
expand foo[@]
以下是执行结果:
[img]http://dl2.iteye.com/upload/attachment/0116/1937/0f0d8d8b-d213-3858-9835-889d342dc168.png[/img]
[size=large]bash中传入数组[/size]
power:Desktop weinanli$ ccat in_array.sh
function in_array() {
foo=${!1}
bar=${!2}
echo ${foo[@]}
echo ${bar[@]}
}
arr1=(a b c)
arr2=(x y z)
in_array arr1[@] arr2[@]
power:Desktop weinanli$ sh in_array.sh
a b c
x y z
power:Desktop weinanli$
[img]http://dl2.iteye.com/upload/attachment/0116/1939/f875804e-6ddf-3a25-96f1-9ea60c9095ae.png[/img]
更为可靠的方法是这样声明`foo`和`bar`:
declare -a foo=("${!1}")
declare -a bar=("${!2}")
其中`declare -a`就是把foo和bar强制声明为数组类型,然后`()`本身就是数组的声明格式。