shell 编程

  1. shell 命令应用练习
    (1)通过帮助信息查看,简述以下文件的用途
    ① /etc/passwd 用户的账户信息

    ② /etc/shadow 可选的加密后的密码档

    ③ /etc/group 一个定义系统中用户组的档配置

    ④ /etc/gshadow用户组etc/group的加密信息档

    (2)依次输入如下命令,观察运行结果。结合 man id 查看到的帮助信息,指出每一条命令功能
    ① id查看当前用户的用户id和组id
    ② id -u只显示用户id
    ③ id -u root显示root用户id
    ④ id -u smw显示smw用户id


    (3)在 shell 命令终端依次输入以下命令,观察执行结果,理解每条命令实现的具体功能
    ① which python
    whereis python
    locate python
    find /usr/bin -name python


    ② grep -n -E 'root|jsj|^user' /etc/passwd
    grep -n -E ' [[:digit:]] ' /etc/passwd
    grep -n -E ' [[:alpha:]] ' /etc/passwd
    grep -n '[0-9]{4,}' /etc/group



    ③ sudo apt install gimp
    which gimp
    sudo apt remove gimp
    which gimp


    ④ ls -dl /root --time-style=long-iso
    ls -dl /root --time-style=long-iso | cut -d' ' -f1,8

    ⑤ ls -l --time-style=long-iso
    ls -l --time-style=long-iso -t
    ls -l --time-style=long-iso -t -r



    ⑥ ls /usr/share/man
    ls /usr/share/man | grep man[1-8]
    ls /usr/share/man/man1
    file /usr/share/man/man1/ls.1.gz
    mkdir ~/temp; cp /usr/share/man/man1/ls.1.gz ~/temp
    cd ~/temp; ls
    sudo gzip -d ls.1.gz; ls



    ⑦ ls –l /home | grep "^d" | wc –l
    ⑧ sudo adduser user7
    ls /home | tee users | wc -l

    (4)根据要求写出相应的 shell 命令
    ① 在目录/usr/include 下搜索文件 signal.h 是否存在

    ② 在/usr/include 目录下的所有文件中查找包含 BUFSIZ 的行,并显示所在行号

    ③ 在用户名密码文件/etc/passwd 中查找登录 shell 为 bash 的用户信息记录,并显示行号
    ④ 从/etc/group 文件中截取第 1 列(组名)和第 3 列(组 id),并按照组 id 号的数值大小由小到大排序。

    (5)体验 awk, sed 用法:依次执行命令,观察执行结果
    ① cp /etc/apt/sources.list t1; less t1

    ② sed -e "s/#.
    //g" t1

    ③ sed -e "s/#.*//g" t1 | awk '{if (length != 0) print $0}'

    ④ tail -5 /etc/passwd | awk -F: '{print $1}'
    ⑤ tail -5 /etc/group | tee t2

    awk 'BEGIN{print "file t2"} {print "line" NR ":" $0} END {print "over"}' t2

    (6)查阅网络或帮助,体验命令 curl 和 wget 用法。

  2. 编写一个 shell 脚本


    3.实验感悟
    此次实验练习熟悉了 shell 中命令历史、别名、特殊字符用法, 练习了 shell 中正则表达式的用法,体验学习了更多常用 linux 命令工具,尤其是和管道线、正则表达式等配合的组合使用, 掌握了shell 中普通变量的基本用法, 掌握了shell 脚本程序建立和执行的方法,以后有机会会更加深入了解shell的用法。
posted @ 2018-05-02 22:53  shenmingwang  阅读(115)  评论(0编辑  收藏  举报