bash学习之二:解释一个再简单不过的脚本

来源:互联网 发布:mac os x 10.10 dmg 编辑:程序博客网 时间:2024/05/16 11:03

其实主要是解释几个常用的命令。

脚本如下:

wangjk@wangjiankun:~$ cat -n mysystem.sh                                       
     1  #!/bin/bash
     2  clear
     3  echo "This is information provided by mysystem.sh. Program starts now."
     4
     5  echo "Hello, $USER"
     6  echo
     7
     8  echo "Today's date is `date`, this is week `date +"%V"`."
     9  echo
    10
    11  echo "These users are currently connected:"
    12  w | cut -d " " -f 1 | grep -v USER | sort -u
    13  echo
    14
    15  echo "This is `uname -s` running on a `uname -m` processor."
    16  echo
    17
    18  echo "This is the uptime information:"
    19  uptime
    20  echo
    21
    22  echo "That's all folks."
wangjk@wangjiankun:~$

1、date命令

第8行用到了date命令:

NAME
       date - print or set the system date and time

SYNOPSIS
       date [OPTION]... [+FORMAT]
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

单独的date命令会打印出年月日时分秒以及星期和时区的完整信息,date +"%V是要显示本周是全年中的第几周。

2、w命令

第12行有几个常用的命令。

NAME
       w - Show who is logged on and what they are doing.

SYNOPSIS
       w [-husfVo] [user]

w命令在首行显示系统运行的信息,从第二行开始将显示登陆用户的信息,如下所示:

wangjiankun:/home/wangjk# w
07:33:40 up  2:06,  3 users,  load average: 0.03, 0.01, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
wangjk   pts/0    192.168.1.100    05:29    3:14m  0.31s  0.31s -bash
wangjk   pts/1    192.168.1.100    07:32   51.00s  0.18s  0.18s -bash
wangjk   pts/2    192.168.1.100    07:33    0.00s  0.18s  0.02s sshd: wangjk [priv]
wangjiankun:/home/wangjk# whoami
root
wangjiankun:/home/wangjk#

上面的信息显示用户wangjk在三个不同的终端上登录了系统,其中第三个的红色部分显示用户运行在特权模式下,也就是说,用户通过su命令运行在root模式下,通过命令whoami的显示root也说明了这一点。

3、cut命令

NAME
       cut - remove sections from each line of files

SYNOPSIS
       cut [OPTION]... [FILE]...

-d, --delimiter=DELIM
       use DELIM instead of TAB for field delimiter

-f, --fields=LIST
       select  only  these  fields;  also print any line that contains no delimiter
       character, unless the -s option is specified

cut命令用来提取域,在此,-d选项将域分隔符指定为空格:“ ”,-f选项指定提取第一个域,如下所示:

wangjk@wangjiankun:~$ w|cut -d " " -f 1                                       

USER
wangjk
wangjk
wangjk
wangjk@wangjiankun:~$

4、grep命令

关于grep命令在此只介绍一个选项:-v,

-v, --invert-match
       Invert the sense of matching, to select non-matching lines.

输出非匹配行。

5、sort命令

NAME
       sort - sort lines of text files

SYNOPSIS
       sort [OPTION]... [FILE]...

-u, --unique
       with  -c, check for strict ordering; without -c, output only the
       first of an equal run

这里的sort -u的作用是对于相同的条目只打印一次,例如:

wangjk@wangjiankun:~$ w|cut -d " " -f 1|grep -v USER

wangjk
wangjk
wangjk
wangjk@wangjiankun:~$ w|cut -d " " -f 1|grep -v USER|sort -u

wangjk
wangjk@wangjiankun:~$

第一次没有运行sort -u命令,打印出了三个相同的用户名wangjk,而第二次运行了sort -u命令,只打印了一个wangjk。

6、uname命令

第15行使用uname命令打印出了系统的信息。

NAME
       uname - print system information

SYNOPSIS
       uname [OPTION]...

-s, --kernel-name
       print the kernel name

-m, --machine
       print the machine hardware name

-s选项打印出了操作系统内核的名称;-m选项打印出了硬件cup的名称。

7、uptime命令

NAME
       uptime - Tell how long the system has been running.

SYNOPSIS
       uptime
       uptime [-V]

运行脚本的完整输出信息如下:

This is information provided by mysystem.sh. Program starts now.
Hello, wangjk

Today's date is Sun Jul  5 06:52:23 EDT 2009, this is week 27.

These users are currently connected:

wangjk

This is Linux running on a i686 processor.

This is the uptime information:
06:52:23 up  1:24,  1 user,  load average: 0.00, 0.00, 0.00

That's all folks.
wangjk@wangjiankun:~$

原创粉丝点击