Linux Programming Bible(1)

来源:互联网 发布:js双日历农历插件 编辑:程序博客网 时间:2024/05/18 12:41

/*codestart*/

 

#!/bin/bash
function CountMatches {
        echo -n "Number of matchs for $1:"
        ls $1 2>/dev/null |wc -l
}

CountMatches /dev/sda*
CountMatches /proc/*

 

/*codeover*/

 

输出:

 

Number of matchs for /dev/sda:1
Number of matchs for /proc/1:36

 

*function函数的使用

*输出的结果并不是我们所需的,这涉及到shell编程中的引用,在 echo -n "Number of matchs for $1:" 中$1直接引用 /dev/sda* 中的第一个 即/dev/sda,同样 /proc/*的第一个 /proc/1

*修改要得到结果,在/proc/* 加单引号

*双引号与单引号的区别:双引号屏蔽大部分的meta,保留部分$等等的含义,单引号屏蔽一切meta (meta:具有特殊意义的保留字)

 

CountMatches ‘/dev/sda*’
CountMatches ’/proc/*‘

 

 

/*codestart*/

 

#!/bin/bash
function CountMatches {
        echo -n "Number of matchs for $1:"
        ls $1 2>/dev/null |wc -l
}

CountMatches ‘/dev/sda*’
CountMatches ’/proc/*‘

 

/*codeover*/

 

输出:

 

Number of matchs for /dev/sda*:7
Number of matchs for /proc/*:5938

 

用ls命令查看

 

[root@huangfeng sandbox]# ls /dev/sda* 2>/dev/null |wc -l
7

[root@huangfeng sandbox]# ls /proc/* 2>/dev/null |wc -l
5938

一些基础知识


标准输入 stdin    0

标准输出 stdout  1

标准错误 stderr   2


$$  当前bash的linux进程ID

$0  保存当前进程的脚本名

$PPID  父进程ID

$RANDOM  产生随机数

$SECONDS  当前shell调用起以秒计算的时间

原创粉丝点击