• shell基础


    1.显示当前登录linux系统的用户

    2.当用户登录到linux系统后,一旦出现提示符时,便可以输入操作命令了。命令可以分为如下两类:

    (1).bash内置的命令;

    (2).应用程序。

    怎么知道哪些指令是bash内置的命令,哪些是应用程序呢?可以用which命令进行查看,如下:

    [root@redhat2 ~]# which echo

    /bin/echo

    [root@redhat2 ~]# which ls

    alias ls='ls --color=auto'

            /bin/ls

    [root@redhat2 ~]# 

    3.特殊字符及引号

    5.显示当前的目录

     [root@redhat2 gok]# pwd

      /usr/share/gok

    6.命令的排列、替换和别名

    在shell中可以使用命令排列同时执行多个命令,可以使用命令替换将一个命令的输出当作了另一个命令的输入,也可以将复杂命令定义别名。

    (1).命令的排列

        如果希望一次执行多个命令,shell允许在不听命令之间,放上特殊的排列字符。这里将介绍经常用的两种方法。

    【1】 使用 “;”

    使用 “;”命令时先执行命令1,不管命令1是否出错,接下来就执行命令2。

    命令语法:

    命令1;命令2

    例:

    注:先在屏幕上列出 /boot目录中的所有内容,然后列出目录/boot及其子目录所占磁盘大小

    2】使用“&&

    使用“&&”命令时只有当命令1正确运行完毕后,才能执行命令2.

    命令1&&明令2

    例:使用排列命令“&&”同时执行两个命令。

    (2). 命令替换

    命令替换这种机制的语法如下:

    命令1 $(命令2)

    除了使用“$( )”之外,还可以使用后引号“  `   ”,命令语法如下所示。

    命令1  `命令2`

    这样虽然可以减少输入,但可读性差,而且容易和没有替换功能的一般单引号混淆。命令替换是一项很实用的功能。假设想查看进程less的进程号,就可以使用如下命令:

    发现less进程的进程号为2524。现在要杀死该进程,可以使用命令替换功能,命令如下所示:

    [root@redhat2 ~]# kill -9 $(pidof less)

    [root@redhat2 ~]#

    命令 pidof l ess”的输出“2524”作为命令“kill -9 的参数,然后就可以杀死该进程了。结束一个名为less的进程前先得用命令pidof找出相应的进程号,然后以这个进程号为参数,运行kill命令,这样就可以结束less进程。

    使用如下替换命令也可以实现同样功能:

    7.命令别名

      在需要执行某一非常长的命令时,所有的命令以及命令的选项、参数都要一一输入,很枯燥也容易犯错。可以为常用命令定义快捷方式,这些快捷方式可以用较简单的命令别名来定义。

    命令语法:

    alias [别名]=[需要定义别名的命令]

    说明:命令中有空格的话,就需要用引号(如在命令与可选项间就有空格)。

    例如,使用下面的命令查看/boot目录内容

    显然,如果每次需要查看/boot目录都要输入这样的命令,会很麻烦,因此可以定义别名。

    使用如下命令定义别名为ok

    当用户需要取消别名的定义时使用unalias,其命令格式如下:

    语法命令:

    Unalias [别名]

    要取消之前定义的别名ok,使用如下命令。

    在重新启动计算机以及关闭终端以后,定义的别名会失效。若系统中有一个命令,同时又定义了一个与其同名的别名,则别名将优先于系统中原有的命令的执行。

    8.管道

    它可以将某个命令的输出信息当作某个命令的输入,由管道符号“|”来标识。

    命令语法:

    [命令1] | [命令2] |[命令3]

    例1:

    [root@redhat2 ~]# ls --help|more
    Usage: ls [OPTION]... [FILE]...
    List information about the FILEs (the current directory by default).
    Sort entries alphabetically if none of -cftuvSUX nor --sort.

    Mandatory arguments to long options are mandatory for short options too.
    -a, --all do not ignore entries starting with .
    -A, --almost-all do not list implied . and ..
    --author with -l, print the author of each file
    -b, --escape print octal escapes for nongraphic characters
    --block-size=SIZE use SIZE-byte blocks. See SIZE format below
    -B, --ignore-backups do not list implied entries ending with ~
    -c with -lt: sort by, and show, ctime (time of last
    modification of file status information)
    with -l: show ctime and sort by name
    otherwise: sort by ctime
    -C list entries by columns
    --color[=WHEN] colorize the output. WHEN defaults to `always'
    or can be `never' or `auto'. More info below
    -d, --directory list directory entries instead of contents,
    and do not dereference symbolic links
    -D, --dired generate output designed for Emacs' dired mode
    -f do not sort, enable -aU, disable -ls --color
    -F, --classify append indicator (one of */=>@|) to entries
    --file-type likewise, except do not append `*'
    --format=WORD across -x, commas -m, horizontal -x, long -l,

    命令ls  /etc显示/etc目录的内容,命令more时分页显示内容.

    2

    使用复杂的管道

    [root@redhat2 ~]# rpm -qa |grep a|more
    system-config-network-tui-1.6.0.el6.2-1.el6.noarch
    xdg-utils-1.0.2-17.20091016cvs.el6.noarch
    liberation-fonts-common-1.05.1.20090721-5.el6.noarch
    sgml-common-0.6.3-32.el6.noarch
    khmeros-fonts-common-5.0-9.el6.noarch
    iwl100-firmware-39.31.5.1-1.el6.noarch
    alsa-utils-1.0.22-3.el6.x86_64
    PackageKit-0.5.8-20.el6.x86_64
    ncurses-base-5.7-3.20090208.el6.x86_64
    readahead-1.5.6-1.el6.x86_64
    openldap-2.4.23-26.el6.x86_64
    spice-vdagent-0.8.1-3.el6.x86_64

    //命令rpm -qa 显示已经安装在系统上的RPM包,命令grep a 是过滤软件包,命令more是分页显示这些信息。

    9.重定向文件

     有时,希望将命令的输出结果保存到文件中,或以文件内容作为命令的参数,这时就需要用到重定向。重定向不适用系统标准输入端口、标准输出端口或标准错误端口,而进行重新的指定。重定向有5种方式、分别是:输出重定向、输入重定向、错误重定向、追加重定向以及同时实现输出和错误的重定向。

    (1).输出重定向

    输出重定向,即将命令的输出保存到文件file中,如果存在相同的文件,则覆盖file文件中的内容。

    命令语法:

    Command >file

    例1:

    使用输出重定向将目录/boot的内容保存到文件/root/abc。

    [root@redhat2 ~]# ls /boot >/boot/abc
    [root@redhat2 ~]# cat /boot/abc
    abc
    config-2.6.32-279.el6.x86_64
    efi
    grub
    initramfs-2.6.32-279.el6.x86_64.img
    lost+found
    symvers-2.6.32-279.el6.x86_64.gz
    System.map-2.6.32-279.el6.x86_64
    vmlinuz-2.6.32-279.el6.x86_64
    [root@redhat2 ~]#

    2:使用echo 命令和输出重定向创建文本文件/root/mm,内容是hell ! Welcome to beijing!

    [root@redhat2 ~]# echo hello>/root/mm
    [root@redhat2 ~]# cat /root/mm
    hello
    [root@redhat2 ~]#

    (2).输入重定向

    输入重定向,即将文件file的内容作为command命令的输入。

    命令语法:

    Command <file

    1:使用输入重定向将文件/root/mm的内容作为输入cat命令执行。

    [root@redhat2 ~]# cat </root/mm
    hello
    [root@redhat2 ~]#

    (3).错误重定向

    错误重定向,即将command命令执行的出错信息输出到文件file中。

    Command 2> file

    查看根本不存在的/root/kk文件,出现报错信息,将其保存到文件/root/b中。

    [root@redhat2 ~]# cat /root/kk 2>/root/b
    [root@redhat2 ~]# cat /root/b
    cat: /root/kk: No such file or directory
    [root@redhat2 ~]#

    (4).追加重定向

    追加重定向,即将命令执行的输出添加到已存在的文件中。

    命令语法:

    Command >>file

    例:

    使用追加重定向将数据写入文件/root/a

    [root@redhat2 ~]# cat /root/b
    cat: /root/kk: No such file or directory
    [root@redhat2 ~]# echo aaa>/root/a
    [root@redhat2 ~]# echo bbb>>/root/a
    [root@redhat2 ~]# cat /root/a
    aaa
    bbb
    [root@redhat2 ~]#

    (5).同时实现输出和错误的重定向

    [root@redhat2 ~]# ls /boot &>/root/kk
    [root@redhat2 ~]# cat /root/kk
    abc
    config-2.6.32-279.el6.x86_64
    efi
    grub
    initramfs-2.6.32-279.el6.x86_64.img
    lost+found
    symvers-2.6.32-279.el6.x86_64.gz
    System.map-2.6.32-279.el6.x86_64
    vmlinuz-2.6.32-279.el6.x86_64
    [root@redhat2 ~]#

  • 相关阅读:
    关于 Web 性能优化
    重命名某一路径下文件
    使用相对坐标定位元素
    使用appium做自动化时如何切换activity
    newman安装时遇到问题的解决
    pytesser的使用
    用户名密码的参数化(读取文件)
    百度登录
    Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
    百度搜索设置下拉框的操作
  • 原文地址:https://www.cnblogs.com/numberone/p/4216911.html
Copyright © 2020-2023  润新知