Shell脚本编程

来源:互联网 发布:指环王和哈利波特 知乎 编辑:程序博客网 时间:2024/05/23 14:14
if语句
 if condition1 then command1 elif condition2 then command2 else commandN fi
for语句
 for var in item1 item2 ... itemN do command1 command2 ... commandN done
while 语句
 while condition do command done
 
case语句
 case  in 模式1) command1 command2 ... commandN ;; 模式2 command1 command2 ... commandN ;; esac
参数处理
说明
$#传递到脚本的参数个数$*以一个单字符串显示所有向脚本传递的参数。
如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。$$脚本运行的当前进程ID号$!后台运行的最后一个进程的ID号$@与$*相同,但是使用时加引号,并在引号中返回每个参数。
如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。$-显示Shell使用的当前选项,与set命令功能相同。$?显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。n > file将文件描述符为 n 的文件重定向到 file。n >> file将文件描述符为 n 的文件以追加的方式重定向到 file。

$* 与 $@ 区别:

  • 相同点:都是引用所有参数。
  • 不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3,,则 " * " 等价于 "1 2 3"(传递了一个参数),而 "@" 等价于 "1" "2" "3"(传递了三个参数)

  • 命令
    功能
    其他
    ps -ef

     -A      Display information about other users' processes, including those without controlling terminals.

         -a      Display information about other users' processes as well as your own.  This will skip any processes which do not

                 have a controlling terminal, unless the -x option is also specified.

         -C      Change the way the CPU percentage is calculated by using a ``raw'' CPU calculation that ignores ``resident'' time

                 (this normally has no effect).

         -c      Change the ``command'' column output to just contain the executable name, rather than the full command line.

         -d      Like -A, but excludes session leaders.

         -E      Display the environment as well.  This does not reflect changes in the environment after process launch.

         -e      Identical to -A. 

         -f      Display the uid, pid, parent pid, recent CPU usage, process start time, controlling tty, elapsed CPU usage, and the

                 associated command.  If the -u option is also used, display the user name rather then the numeric uid.  When -o or

                 -O is used to add to the display following -f, the command field is not truncated as severely as it is in other

                 formats. 

         -G      Display information about processes which are running with the specified real group IDs.

         -g      Display information about processes with the specified process group leaders.

         -h      Repeat the information header as often as necessary to guarantee one header per page of information.

         -j      Print information associated with the following keywords: userpidppidpgidsessjobcstatetttime, and

     ps -- process status

    lsof -i

    lsof filename 显示打开指定文件的所有进程
    lsof -a 表示两个参数都必须满足时才显示结果
    lsof -c string 显示COMMAND列中包含指定字符的进程所有打开的文件
    lsof -u username 显示所属user进程打开的文件
    lsof -g gid 显示归属gid的进程情况
    lsof +d /DIR/ 显示目录下被进程打开的文件
    lsof +D /DIR/ 同上,但是会搜索目录下的所有目录,时间相对较长
    lsof -d FD 显示指定文件描述符的进程
    lsof -n 不将IP转换为hostname,缺省是不加上-n参数
    lsof -i 用以显示符合条件的进程情况
    lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
    46 --> IPv4 or IPv6
    protocol --> TCP or UDP
    hostname --> Internet host name
    hostaddr --> IPv4地址
    service --> /etc/service中的 service name (可以不只一个)
    port --> 端口号 (可以不只一个)
    例如: 查看22端口现在运行的情况 
    # lsof -i :22
    COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
    sshd 1409 root 3u IPv6 5678 TCP *:ssh (LISTEN)

    lsof - list open files

    1、grep

    grep(General Regular Expression Parser,通用规则表达式分析程序)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

    它的使用语法为:

    grep [选项] pattern [文件名]

    . 匹配任意一个字符
    * 匹配0 个或多个*前的字符
    ^ 匹配行开头
    $ 匹配行结尾
    [] 匹配[ ]中的任意一个字符,[]中可用 - 表示范围,
    例如[a-z]表示字母a 至z 中的任意一个
    \ 转意字符
    命令中的选项为:
    -b 显示块号
    -c 仅显示各指定文件中包含模式的总行数
    -i 模式中字母不区分大小
    -h 不将包含模式的文件名显示在该行上
    -l 仅显示包含模式的文件名
    -n 显示模式所在行的行号
    -s 指定文件若不存在或不可读,不提示错误信息
    -v 显示所有不包含模式的行

    其中,pattern为所要匹配的字符串。如在/etc/passwd文件下查找包含“carey”字符的行:

    grep carey /etc/passwd

    要用好grep这个工具,其实就是要写好正则表达式,所以这里不对grep的所有功能进行实例讲解,只列几个例子,讲解一个正则表达式的写法。

    grep 'test' d*

    显示所有以d开头的文件中包含test的行。

    ls -l | grep '^public'

    通过管道过滤ls -l输出的内容,只显示以public开头的行。

    后面的^字符强制grep命令只在每行的开头找public。整个搜索模式(pattern)用单引号括起来,使shell不理会它们。shell只将单引号去掉,将搜索模式送给grep命令。

    grep -i 'hello world' menu.h main.c

    显示在menu.h和main.c文件中匹配“hello world”的行,忽略大小写。

    例如:

    grep 'sample' -R * 

    2、find

    find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件。

    find的使用格式如下:

      find <指定目录> <指定条件> <指定动作>

      - <指定目录>: 所要搜索的目录及其所有子目录。默认为当前目录。

      - <指定条件>: 所要搜索的文件的特征。

      - <指定动作>: 对搜索结果进行特定的处理。

    如果什么参数也不加,find默认搜索当前目录及其子目录,并且不过滤任何结果(也就是返回所有文件),将它们全都显示在屏幕上。

    find的使用实例:

    find . -name 'my*' –ls

    搜索当前目录(含子目录,下同)中所有文件名以my开头的文件,并显示它们的详细信息。

    find /home -user rtos     

    搜索/home下拥有者为rtos的文件

    find . -type f -mmin -10

    搜索当前目录中,在过去10分钟中更新过的所有的普通文件。如果不加-type f参数,则搜索普通文件+特殊文件+目录。

    find / -type f -size +100M

    查找系统中所有大于100M的文件

    说明: 如果你要寻找一个档案的话,那么使用 find 会是一个不错的主意。不过,由于 find 在寻找数据的时候相当的耗硬盘,所以没事情不要使用 find 啦!有更棒的指令可以取代呦,那就是 whereis 与 locate 咯~

    3、locate

    locate命令其实是“find -name”的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库(/var/lib/locatedb),这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。

    locate命令的使用实例:

    locate /etc/sh

    搜索etc目录下所有以sh开头的文件。

    locate -i ~/m

    搜索用户主目录下,所有以m开头的文件,并且忽略大小写。

    4、whereis

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

    whereis命令的使用实例:

    whereis grep

    grep: /bin/grep /usr/share/man/man1p/grep.1p.gz /usr/share/man/man1/grep.1.gz

    5、which

    which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

    which命令的使用实例:

    which grep

    /bin/grep

原创粉丝点击