例5:打印机器信息的函数
此例定义了一个能够显示机器所有需要信息的函数。用户可以在启动文件中定义并调用该函数,以便于在启动时得到这些信息。
$ function mach()
{
echo -e "/nMachine information:" ; uname -a
echo -e "/nUsers logged on:" ; w -h
echo -e "/nCurrent date :" ; date
echo -e "/nMachine status :" ; uptime
echo -e "/nMemory status :" ; free
echo -e "/nFilesystem status :"; df -h
}
$ mach
Machine information:
Linux dev-db 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 EST 2008 x86_64 GNU/Linux
Users logged on:
root pts/2 ptal.mot Wed10 0.00s 1.35s 0.01s w -h
Current date :
Thu Mar 18 11:59:36 CET 2010
Machine status :
11:59:36 up 7 days, 3 min, 1 user, load average: 0.01, 0.15, 0.15
Memory status :
total used free shared buffers cached
Mem: 2059768 2033212 26556 0 81912 797560
-/+ buffers/cache: 1153740 906028
Swap: 4192956 48164 4144792
Filesystem status :
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 12G 12G 0 100% /
tmpfs 1006M 377M 629M 38% /dev/shm
/dev/sdc5 9.9G 409M 9.0G 5% /mydisk
例6:格式化ls命令输出
函数首先清屏,然后将光标置于屏幕顶端,接着执行ls命令,最后再把光标置于屏幕底部。
$ function ll ()
{
clear;
tput cup 0 0;
ls --color=auto -F --color=always -lhFrt;
tput cup 40 0;
}
$ ll
使用type命令返回函数类别
Type是一个内建函数,可以返回函数类别。
Syntax:
type function-name
$ type ll
ll is a function
ll ()
{
clear;
tput cup 0 0;
ls --color=auto -F --color=always -lhFrt;
tput cup 40 0;
alias ls="ls --color=auto -F"
}