Linux学习记录五——命令的使用


前言

本篇文章旨在继续学习一些命令,甚至尝试创建自己的命令。


一、有关命令的介绍?

什么是命令

一条命令基本属于以下4种情况:

  1. 可执行程序。可执行程序类似于在该程序类别中,程序可以编译为二进制文件,比如C语言、c++编写的程序,也可以是shell、Python等脚本语言编写的程序。
  2. shell内置命令。bash支持许多在内部称之为shell builtin的内置命令。例如,cd、less、file就是shell内置命令。
  3. shell函数。shell函数是合并到环境变量中的小型shell脚本
  4. alias(别名、化名)命令。我们可以在其他命令的基础上定义自己的命令。

二、帮助使用命令的命令

1、识别命令

Linux提供了两个方法来识别命令类型

1.1 type——显示命令的类型

  type命令是一个shell内置的命令,可根据指定的命令名显示shell将要执行的命令类型。格式如下:
type command
举几个例子:

yrf-tan@yrftan-Lenovo-G50-70m:~$ type type
type is a shell builtin
yrf-tan@yrftan-Lenovo-G50-70m:~$ type ls
ls is aliased to `ls --color=auto'
yrf-tan@yrftan-Lenovo-G50-70m:~$ type cd
cd is a shell builtin
yrf-tan@yrftan-Lenovo-G50-70m:~$ type ln
ln is /bin/ln
yrf-tan@yrftan-Lenovo-G50-70m:~$ type mkdir
mkdir is /bin/mkdir

由显示结果可以看到ls是一个别名命令。

1.2 which——显示可执行程序的位置

  有时候,系统中可能安装了一个可程序的多个版本,使用which命令可以确定一个给定可执行程序的准确位置。
which命令只适用于可执行程序,而不适用于内置命令和命令别名(真正可执行程序的替代物)
举几个例子:

yrf-tan@yrftan-Lenovo-G50-70m:~$ which ls
/bin/ls
yrf-tan@yrftan-Lenovo-G50-70m:~$ which cd
yrf-tan@yrftan-Lenovo-G50-70m:~$ which df
/bin/df
yrf-tan@yrftan-Lenovo-G50-70m:~$ which which
/usr/bin/which

2、获得命令文档

我们可以查看每一类命令的可用文档

2.1 help——获得shell内置命令的帮助文档

  bash为每一个shell内置命令提供了一个内置的帮助工具。输入help,然后输入shell内置命令的名称即可使用该帮助工具。
举几个例子:

yrf-tan@yrftan-Lenovo-G50-70m:~$ help help
help: help [-dms] [pattern ...]
    Display information about builtin commands.
    
    Displays brief summaries of builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise the list of help topics is printed.
    
    Options:
      -d	output short description for each topic
      -m	display usage in pseudo-manpage format
      -s	output only a short usage synopsis for each topic matching
    		PATTERN
    
    Arguments:
      PATTERN	Pattern specifiying a help topic
    
    Exit Status:
    Returns success unless PATTERN is not found or an invalid option is given.
yrf-tan@yrftan-Lenovo-G50-70m:~$ help help
help: help [-dms] [pattern ...]
    Display information about builtin commands.
    
    Displays brief summaries of builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise the list of help topics is printed.
    
    Options:
      -d	output short description for each topic
      -m	display usage in pseudo-manpage format
      -s	output only a short usage synopsis for each topic matching
    		PATTERN
    
    Arguments:
      PATTERN	Pattern specifiying a help topic
    
    Exit Status:
    Returns success unless PATTERN is not found or an invalid option is given.


yrf-tan@yrftan-Lenovo-G50-70m:~$ help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.
    
    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
    
    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.
    
    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.
    
    Options:
      -L	force symbolic links to be followed: resolve symbolic
    		links in DIR after processing instances of `..'
      -P	use the physical directory structure without following
    		symbolic links: resolve symbolic links in DIR before
    		processing instances of `..'
      -e	if the -P option is supplied, and the current working
    		directory cannot be determined successfully, exit with
    		a non-zero status
      -@	on systems that support it, present a file with extended
    		attributes as a directory containing the file attributes
    
    The default is to follow symbolic links, as if `-L' were specified.
    `..' is processed by removing the immediately previous pathname component
    back to a slash or the beginning of DIR.
    
    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

注意表示法:出现在命令语法描述中的方括号表示一个可选的选项。竖线符号表示的是两个互斥的选项。

2.2 help——显示命令的使用信息

  很多可执行程序都支持–help选项,该选项描述了命令支持的语法和选项。一些程序不支持该选项,但是使用时通常会产生一条错误消息,该错误消息也能揭示相同的命令使用信息,所以都可以试试。
举几个例子:

yrf-tan@yrftan-Lenovo-G50-70m:~$ cd --help
cd: cd [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.
    
    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
    
    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.
    
    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.
    
    Options:
      -L	force symbolic links to be followed: resolve symbolic
    		links in DIR after processing instances of `..'
      -P	use the physical directory structure without following
    		symbolic links: resolve symbolic links in DIR before
    		processing instances of `..'
      -e	if the -P option is supplied, and the current working
    		directory cannot be determined successfully, exit with
    		a non-zero status
      -@	on systems that support it, present a file with extended
    		attributes as a directory containing the file attributes
    
    The default is to follow symbolic links, as if `-L' were specified.
    `..' is processed by removing the immediately previous pathname component
    back to a slash or the beginning of DIR.
    
    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

2.3 man——显示程序的手册页

  大多数供命令行使用的可执行1文件,提供一个称之为manual或者是man page的正式文档。该文档可以用man来查看,用法如下:
man program
  program是需要查看的命令名称,在大多数linux系统中调用less命令来查看手册文档。所以,当显示手册文档时,你熟悉的less命令都能奏效。
手册文档的结构安排:

部分内容
1用户命令
2内核系统调用的程序接口
3C库函数程序接口
4特殊文件,如设备节点和驱动程序
5文件格式
6游戏和娱乐,例如屏幕保护程序
7其他杂项
8系统管理命令

  有时候我们需要查看手册文档的具体部分,以查找我们需要的信息。为了具体指明在哪个部分,我们可以这样使用man命令:
man section search_term
例如:

yrf-tan@yrftan-Lenovo-G50-70m:~$ man 5 passwd

该命令将会显示文件/etc/passwd 的文件格式描述手册

2.4 apropos——显示合适的命令

  我们有可能会搜索参考手册列表,才进行基于某个搜素条目的匹配。在输出中,每一行的第一个字段是手册页的名称,第二个字段显示部分(section)。带-k选项的man命令与apropos命令在功能上基本相同。
举几个例子:

yrf-tan@yrftan-Lenovo-G50-70m:~$ apropos passwd
chgpasswd (8)        - update group passwords in batch mode
chpasswd (8)         - update passwords in batch mode
gpasswd (1)          - administer /etc/group and /etc/gshadow
grub-mkpasswd-pbkdf2 (1) - generate hashed password for GRUB
openssl-passwd (1ssl) - compute password hashes
pam_localuser (8)    - require users to be listed in /etc/passwd
passwd (1)           - change user password
passwd (1ssl)        - compute password hashes
passwd (5)           - the password file
update-passwd (8)    - safely update /etc/passwd, /etc/shadow and /etc/group
yrf-tan@yrftan-Lenovo-G50-70m:~$ apropos home
addgnupghome (8)     - Create .gnupg home directories
gpgconf (1)          - Modify .gnupg home directories
mkhomedir_helper (8) - Helper binary that creates home directories
pam_mkhomedir (8)    - PAM module to create users home directory
systemd-gpt-auto-generator (8) - Generator for automatically discovering and mounting root, /home and /srv partitions, as well as discovering and e...

2.5 whatis——显示命令的简要描述

whatis程序显示匹配具体关键字的手册页的名字和一行描述。

yrf-tan@yrftan-Lenovo-G50-70m:~$ whatis ls
ls (1)               - list directory contents

yrf-tan@yrftan-Lenovo-G50-70m:~$ whatis less
less (1)             - opposite of more

2.6 info——显示程序的info条目

  info页面可通过info阅读器显示,info页面使用超链接。info程序读取info文件,该文件是树形结构,分为各个单独的节点。每一个节点包含一个主题。info文件包含的超链接可以实现节点间的跳转。通过前置星号可以识别超链接,将光标放在超链接上并按Enter键可以激活它。
可以通过输入info以及程序名(可选)来调用info
info命令:

命令功能
显示命令帮助
nNext——显示下一个节点
pPrevious——显示上一个节点
uUp——显示目前节点的父节点(通常是一个菜单)
ENTER进入光标所指的超链接
q退出

3、使用别名创建自己的命令

小技巧:通过使用分号来分隔多条命令,就可以将多条命令按序输入在一行中。工作方式如下:
command1;command2;command3…

例如:

yrf-tan@yrftan-Lenovo-G50-70m:~$ cd /usr;ls;cd -
bin  games  include  lib  libexec  local  sbin  share  src
/home/yrf-tan

3.1 alias——创建别名

  现在,我们可以可以通过使用alias命令将以上命令整合成一条新的命令。首先要为新命令想一个名字,输入之前先用type检查一下想出来的名字是否被使用过了。
比如起个名字叫happ,让我们来检查一下这个名字有没有被使用过,

yrf-tan@yrftan-Lenovo-G50-70m:~$ type happy
bash: type: happy: not found

没有被使用过,那我们就使用这个为新名字。
命令结构: alias name=‘string’
下面创建新命令的别名:

yrf-tan@yrftan-Lenovo-G50-70m:~$ alias happy='cd /usr; ls;cd -'
yrf-tan@yrftan-Lenovo-G50-70m:~$ type happy
happy is aliased to `cd /usr; ls;cd -'

让我们来试试这个命令:

yrf-tan@yrftan-Lenovo-G50-70m:~$ happy
bin  games  include  lib  libexec  local  sbin  share  src
/home/yrf-tan

用type再来查看试试:

yrf-tan@yrftan-Lenovo-G50-70m:~$ type happy
happy is aliased to `cd /usr; ls;cd -'

3.2 unalias——删除别名

要删除别名可以使用unalias,使用格式如下:
unalias name
例如:

yrf-tan@yrftan-Lenovo-G50-70m:~$ type happy
happy is aliased to `cd /usr; ls;cd -'
yrf-tan@yrftan-Lenovo-G50-70m:~$ unalias happy
yrf-tan@yrftan-Lenovo-G50-70m:~$ type happy
bash: type: happy: not found

  在命令行中定义别名有个问题就是当shell会话结束后这些别名也就随之消失。我们可以在文件中添加别名,这样每一次登录系统时,这些文件都会建立系统环境。


总结

  这篇文章主要介绍了如何查找命令文档,我们可以结合这篇文章所学来查看之前遇到的命令的文档,学习一下这些命令的其他使用。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值