Article Directory
echo: display text
-
Function: display text content.
-
Usage: echo [-neE] [string]
-
The main options are as follows:
Order | describe |
---|---|
-n |
do not output newlines at the end of the line |
-e |
Allows interpretation of the backslash-escaped characters listed below |
-E |
Do not interpret those sequences in STRINGs |
--help |
show help |
--version |
show version info |
-e
Order | describe |
---|---|
\a |
sound a warning |
\b |
delete the previous character |
\c |
without a newline at the end |
\f |
Newline but the cursor remains where it was |
\n |
Newline and cursor moved to the beginning of the line |
\r |
Move the cursor to the beginning of the line without wrapping |
\t |
insert tab character |
\v |
Same as \f |
\\: |
Insert "\"" character |
\nnn |
Insert the ASCII character represented by nnn (octal) |
case study
(1) echo
Display the string. Ordinary strings can echo
be entered directly after the string, but there will be problems when certain characters (such as "\") are output (in this way, "\" is treated as a continuation character and filtered out, To output a "\", you must input "\", which is similar to the requirements of printf output in C language), so it is better to use 'string' or "string", so that even "\" can be output, which is convenient and intuitive.
#echo hello world
hello world
#echo hello\ world
hello world
#echo hello\\ world
hello\ world
#echo 'hello\\ world' 或者: echo "hello\\ world"
hello\\ world
(2) The escape display of echo: add the -e parameter to output multiple lines.
#echo -e 'hello\nworld'
hello
world
(3) Output ASCII characters: echo -e \NNN (NNN is the octal code number of the ASCII character, and those that do not conform to the octal code will be printed literally).
#echo -e '\61 \62 \101 \141'
date: display date and time
-
Function: display or set system time and date.
-
Usage: date [options] show time usage (begins with "+", followed by usage)
-
The main options are as follows:
Order | describe |
---|---|
-d,--date=STRING |
Display the time specified by STRING instead of the current time. |
-f,--file=DATEFILE |
Display the time specified by each row in DATEFILE as if each row in DATEFILE were given as an argument to --date. |
-I,--iso-8601[=TIMESPEC] |
Output time in ISO-8601 date/time format. |
TIMESPEC=`date’ | (or when not specified) only the date is output, and hours'、 when it is equal to minutes' or `seconds', the date and time are output according to the specified precision. |
-r,--reference=FILE |
Displays the last modification time of FILE. |
-R,--rfc-822 |
Outputs a date in the format specified by RFC-822. |
-s,--set=STRING |
Set time according to STRING. |
-u,--utc,--universal |
Displays or sets the global time (Greenwich Mean Time). |
--help |
Display the help file and exit. |
--version |
Display version information and exit. |
Format FORMAT: controls the output format. This format is valid only when option specifies global time. They are respectively explained as follows.
%H
: Hour (expressed in 00~23).%I
: Hour (indicated by 01~12).%K
: Hour (expressed in 0~23).%l
: Hour (indicated by 0~12).%M
: Minute (expressed in 00~59).%P
: AM or PM.%r
: Time (including hour, minute and second, hour is represented by 12-hour AM/PM).%s
: Total seconds. The starting time is 1970-01-01 00:00:00 UTC.%S
: seconds (in locale idiom).%T
: Time (including hour, minute, and second, and the hour is expressed in 24-hour format).%X
: time (expressed in local usage).%Z
: urban area.%a
: Abbreviation for the week.%A
: The full name of the week.%b
: The abbreviation of the English name of the month.%B
: The full English name of the month.%c
: Date and time. Entering only the date command will display the same result.%d
: Date (indicated by 01~31).%D
: Date (including year, month, day).%j
: The day of the year.%m
: Month (indicated by 01~12).%U
: The week number of the year.%w
: The day of the week, 0 means Sunday, 1 means Monday, and so on.%x
: Date (expressed in local convention).%y
: Year (expressed in 00~99).%Y
: Year (expressed in 4 digits).%n
: When displayed, inserts a new row.%t
: When displayed, insert a tab character.MM
: month (required).DD
: date (required).hh
: hours (required)mm
: minutes (required).CC
: The first two digits of the year (optional).YY
: The last two digits of the year (optional).ss
: seconds (optional).
Note that only superusers have the authority to use the date command to set the time, and ordinary users can only use the date command to display the time.
case study
(1) Display the current time.
#date
2009 年 08 月 02 日星期六 17:19:27 CST
(2) After the time is displayed, the line will be skipped, and then the current date will be displayed.
# date +%T%n%D
10:19:57
03/24/10
(3) Display the month.
# date +%B
三月
(4) Display date and set time (12:34:56).
date --date 12:34:56
2010 年 03 月 24 日 星期三 12:34:56 CST
(5) Set the current system time (12:34:56).
# date --s 12:34:56
2010 年 03 月 24 日 星期三 12:34:56 CST
2.17 script:记录 Linux 会话信
script: record Linux session information
- Function: used to record all executed commands and their output.
- Usage: script [options] file
- The main options are as follows:
Order | describe |
---|---|
-a,--append |
Append output. |
-c,--command<命令> |
Run commands instead of an interactive shell. |
-e,--return |
Return the exit code of the child process. |
-f,--flush |
Run flush after each write. |
--force |
Still used even if the output file is a link. |
-q,--quiet |
quiet mode. |
-t,--timing[=<文件>] |
Output time data to standard error (or to a file). |
-V,--version |
Print version information and exit. |
-h,--help |
Show this help and exit. |
Explain that many system administrators know the importance of keeping an activity log of various tasks, configuration changes, and so on. For some organizations, simple logs of "I did this" or "John did that" are sufficient; others need to document all changes. Copy-pasting terminal output can be tedious, and we use a little-known program called script to solve this problem, which is part of the util-linux package of most Linux products. The script records everything about the session, both what is typed and what is seen, it even records colors. Therefore, if a color is included in the command prompt or program output, the script will record it.
case study
(1) Use script. Simply execute the following command:
$ script
By default, it writes to a typescript file in the current directory, and everything typed is logged to that file. To log to another file, just use the script /path/to/file command. When finished recording, type exit to exit. This command will close the script session and save the file. You can use cat or any other program to examine the log files. The disadvantage of using script is that it logs all special characters, so the input file will be full of control characters and ANSI escape sequences. You can use a very simple shell in script to solve this problem:
$ SHELL=/bin/sh PS1="$ " script
使用 script 时,不要使用交互式程序或处理窗口的程序,如 vior top,它们会破坏会话的输 出结果。另外,日志文件会记录使用的任何命令行程序和完成一项任务所采取的步骤。如果需 要在脚本中编辑一个文件,可以考虑退出 script 会话,然后用 script -a(它在旧会话后添加新会 话)对文件进行编辑后再重新启动会话。
(2)script -a filename1 -t 2>filename2。
退出命令为 exit。
若要查看记录结果,使用 cat filename 即可,filename1 和 filename2 可以是同一个文件。
apropos:搜索关键字
- 作用:在 whatis 资料库中搜索特定字串。
- 用法:apropos[选项]关键字
- 主要选项如下:
命令 | 描述 |
---|---|
-d,--debug |
输出调试信息。 |
-v,--verbose |
输出详细的警告信息。 |
-r,--regex |
将每个 keyword 作为正则表达式解释。这是默认行为。每个 keyword 将匹配手册页和描述。 |
-w,--wildcard |
将每个 keyword 作为 Shell 样式的通配符解释。 |
-e,--exact |
每个 keyword 将精确匹配手册页名字和描述。 |
-a,--and |
只显示匹配所有 keyword 的手册页和描述。默认显示匹配任何 keyword 的项。 |
-l,--long |
不根据终端宽度缩减输出。 |
-s section,--section section |
只查找指定的手册 section。 |
-m system[,...],--systems=system[,...] |
用于查找其他操作系统的手册页。 |
-M path,--manpath=path |
指定从其他以冒号分隔的手册页层次查找。默认使用$MANPATH 环境变量。这个选项覆盖$MANPATH 的内容。 |
-L locale,--locale=locale |
apropos 调用 C 函数 setlocale 来得到当前本地化信息,包括$LC_MESSAGE 和$LANG 。使用该选项提供一个 locale 字符串来临时更改本地化信息。 |
-C file,--config-file=file |
使用这个用户配置文件而不是默认的~/.manpath。 |
-h,--help |
打印帮助信息并退出。 |
-V,--version |
打印版本信息并退出。 |
案例练习
要“查找”文件,又不知道用什么命令,可以输入下面的命令:
$apropos search
其他部分可以根据实际情况增减。
locate:搜索文件
- 作用:用于查找符合条件的文件,它会去保存文件与目录名称的数据库中查找符合范本样 式条件的文件或目录。
- 用法:locate[选项]相关字
- 主要选项如下:
命令 | 描述 |
---|---|
-b,–basename | 匹配唯一的路径名称的基本文件名。 |
-c,–count | 只显示找到条目的号码 |
-d,–database DBPATH | 用 DBPATH 替代默认的数据库(/var/lib/mlocate/mlocate.db)。 |
-e,–existing | 只显示当前存在的文件条目。 |
-L,–follow | 当文件存在时跟随蔓延的符号链接(默认)。 |
-h,–help | 显示本帮助。 |
-i,–ignore-case | 匹配模式时忽略大小写区别。 |
-l,–limit,-n LIMIT | 限制为 LIMIT 项目的输出(或计数)。 |
-m,–mmap | 忽略向后兼容性。 |
-P,–nofollow,-H | 当检查文件时不跟随蔓延的符号链接。 |
-0,–null | 输出时以 NUL 分隔项目。 |
-S,–statistics | 不搜索项目,显示有关每个已用数据库的统计信息。 |
-q,–quiet | 不报告关于读取数据库的错误消息。 |
-r,–regexp REGEXP | 搜索基本正则表达式 REGEXP 来代替模式。 |
–regex | 模式是扩展正则表达式。 |
-s,–stdio | 忽略向后兼容性。 |
-V,–version | 显示版本信息。 |
-w,–wholename | 匹配完整路径名(默认)。 |
说明 有时候,明明知道某一文件或目录存在,却不知该到哪里去找到它,这时可以使 用 locate 命令来搜寻文件或目录。同 find 命令相比较,locate 命令从数据库中查找, 而不是每次搜索文件系统。因为是从数据库中查找,locate 的速度远远快于 find 命令。 但是,使用 locate 命令查找的结果仅仅是在当前数据库中。locate 数据库是通过 cron 的日任务更新的,用户也可以手工进行,使用“locate –u;updatedb”命令完成(通常 需要 root 权限)。当我们建立好这个数据库后,就可以方便地搜寻所需文件了。
案例练习
(1)建立数据库。
locate -u
(2)查找所有包含字符“kde”且在目录 bin 中的文件。
# locate kde | grep bin
/sbin/blockdev
/usr/kerberos/bin/kdestroy
/usr/sbin/lockdev