当前位置: 代码迷 >> 综合 >> grep,压缩归档,shell提示符
  详细解决方案

grep,压缩归档,shell提示符

热度:80   发布时间:2023-10-19 22:36:24.0

先留个坑,配合着正则表达式一起在linux中的运用会好很多吧。

格式是    grep [options] pattern [file]

--------------

GREP(1)                                                                GREP(1)

NAME
       grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION
       grep  searches the named input FILEs (or standard input if no files are named, or if a single
       hyphen-minus (-) is given as file name) for lines containing a match to  the  given  PATTERN.
       By default, grep prints the matching lines.

       In  addition,  two  variant  programs  egrep  and  fgrep are available.  egrep is the same as
       grep -E.  fgrep is the same as grep -F.  Direct  invocation  as  either  egrep  or  fgrep  is
       deprecated,  but  is  provided  to  allow  historical  applications  that rely on them to run

       unmodified.

------------


[oh@localhost manlist]$ grep one grep.mdObtain patterns  from  FILE,  one	 per  line.   The  empty  fileoutput,  grep  ensures  that the standard input is positioned toSuppress error messages about nonexistent or  unreadable	files.there is more than one file to search.default  when there is only one file (or only standard input) toTYPE  is	binary,	 and  grep  normally outputs either a one-lineA regular expression may be  followed  by  one  of  several  repetition+      The preceding item will be matched one or more times.is  used	 if none of these environment variables are set, if the localeoperand  of  grep to be an option, even if it appears to be one.require	exponential  time  and space, and may cause grep to run out ofBack-references are very slow, and may require exponential time.
[oh@localhost manlist]$ grep -n one grep.md
59:	      Obtain patterns  from  FILE,  one	 per  line.   The  empty  file
116:	      output,  grep  ensures  that the standard input is positioned to
136:	      Suppress error messages about nonexistent or  unreadable	files.
152:	      there is more than one file to search.
156:	      default  when there is only one file (or only standard input) to
232:	      TYPE  is	binary,	 and  grep  normally outputs either a one-line
377:       A regular expression may be  followed  by  one  of  several  repetition
381:       +      The preceding item will be matched one or more times.
435:       is  used	 if none of these environment variables are set, if the locale
589:	      operand  of  grep to be an option, even if it appears to be one.
622:       require	exponential  time  and space, and may cause grep to run out of
625:       Back-references are very slow, and may require exponential time.
[oh@localhost manlist]$ 

grep有好多个衍生版本:  egrep支持POSIX扩展正则表达式     fgrep大型文件中搜索字符串。

 

压缩工具。bzip2,gzip,zip

归档工具:tar



--------------------

shell提示符

[oh@localhost manlist]$ echo $PS1
[\u@\h \W]\$
[oh@localhost manlist]$ echo $PS2
>
[oh@localhost manlist]$ 

俩个环境变量:

   PS1:控制默认命令提示符的格式。

   PS2:控制后续命令行提示符的格式。

也就是说对环境变量的赋值,来控制bash shell的命令提示符显示格式,那么,怎么赋值?

  bash shell提示符字符:

[u@]PS1="\u"
oh
ohPS1="[\u]"
[oh]PS1=\u
u

特别注意的是,赋值时没加“”,被理解为命令了

uPS1=[ \u \t ]
bash: u: command not found
u



参见:

https://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html

So when executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:

  • \a : an ASCII bell character (07)
  • \d : the date in “Weekday Month Date” format (e.g., “Tue May 26”)
  • \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
  • \e : an ASCII escape character (033)
  • \h : the hostname up to the first ‘.’
  • \H : the hostname
  • \j : the number of jobs currently managed by the shell
  • \l : the basename of the shell???s terminal device name
  • \n : newline
  • \r : carriage return
  • \s : the name of the shell, the basename of $0 (the portion following the final slash)
  • \t : the current time in 24-hour HH:MM:SS format
  • \T : the current time in 12-hour HH:MM:SS format
  • \@ : the current time in 12-hour am/pm format
  • \A : the current time in 24-hour HH:MM format
  • \u : the username of the current user
  • \v : the version of bash (e.g., 2.00)
  • \V : the release of bash, version + patch level (e.g., 2.00.0)
  • \w : the current working directory, with $HOME abbreviated with a tilde
  • \W : the basename of the current working directory, with $HOME abbreviated with a tilde
  • \! : the history number of this command
  • \# : the command number of this command
  • \$ : if the effective UID is 0, a #, otherwise a $
  • \nnn : the character corresponding to the octal number nnn
  • \\ : a backslash
  • \[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
  • \] : end a sequence of non-printing characters

Let us try to set the prompt so that it can display today???d date and hostname:
PS1="\d \h $ "


来个中文的。

https://billie66.github.io/TLCL/book/chap14.html

表14-1: Shell 提示符中用到的转义字符
序列 显示值
\a 以 ASCII 格式编码的铃声 . 当遇到这个转义序列时,计算机会发出嗡嗡的响声。
\d 以日,月,天格式来表示当前日期。例如,“Mon May 26.”
\h 本地机的主机名,但不带末尾的域名。
\H 完整的主机名。
\j 运行在当前 shell 会话中的工作数。
\l 当前终端设备名。
\n 一个换行符。
\r 一个回车符。
\s shell 程序名。
\t 以24小时制,hours:minutes:seconds 的格式表示当前时间.
\T 以12小时制表示当前时间。
\@ 以12小时制,AM/PM 格式来表示当前时间。
\A 以24小时制,hours:minutes 格式表示当前时间。
\u 当前用户名。
\v shell 程序的版本号。
\V Version and release numbers of the shell.
\w 当前工作目录名。
\W 当前工作目录名的最后部分。
\! 当前命令的历史号。
\# 当前 shell 会话中的命令数。
\$ 这会显示一个"$"字符,除非你拥有超级用户权限。在那种情况下, 它会显示一个"#"字符。
\[ 标志着一系列一个或多个非打印字符的开始。这被用来嵌入非打印 的控制字符,这些字符以某种方式来操作终端仿真器,比方说移动光标或者是更改文本颜色。
\] 标志着非打印字符序列结束。

oh
ohPS1="[\u]"
[oh]PS1=\u
uPS1=[ \u \t ]
bash: u: command not found
uPS1="[\u \t]:"
[oh 17:03:59]:

当然这样只是当前登录shell有效。
































  相关解决方案