Bash+Quick+Reference 笔记2

 早上了,还有一个小时正式开工,先看会书。嘎嘎。。

上回书说道bash的某些option,今天要讲完这些option。

--login shell is a login shell

--noediting:把readline关闭了。此处的readline是什么具体含义也不是很明白了。希望读者自己去实践,如果用到这些的话,笔者主要是应用出身,底层的很多也是一知半解的。就怕“庸医”。还敢继续么:

--noprofile:donnot read /etc/profile or any of the personal startup file 。意思是不带任何含有基础环境变量的shell。这个类似于exec的应用,里面的ll命令都是不存在的。

--norc :donn't read ~/.bashrc。enabled automatically when invoked as sh。鸡肋。

--posix:这种情况下也是一个受严格限制的shell,系统的变量,别名通通都消失了,只有基础的某些命令可用。大家要明白一个道理,ls这些命令也是linux内核上附加的工具而已。

--version:print a version and exit

基于上面的话,已经全部讲完了。费死牛劲了。。不懂的话也没关系i,因为这些偏向于底层了,对于搞应用的人来说就是深入shell内部后可能需要这方面的知识,也许你当前的shell版本,很多选项已经不支持了,以上讲解基于3.1版本的bash。

下面我就讲述下关于shell arguments的一段概述了:

变量就是$1,$2,$3等这些变量的集合,第一个对应就是$1,依次类推。$0代表的是变量的名字,脚本不必要是可执行的,但是必须是可读的。

as follws 是介绍的是bash特有的某些语法:syntax

special files:指的是shell为登录shell的时候会读取某些配置文件

1. /etc/profile. Executed automatically at login.

2. The first file found from this list: ˜/.bash_profile, ˜/.bash_login, or ˜/.profile.

Executed automatically at login.

3. ˜/.bashrc is read by every nonlogin shell. Ho wever, if invoked as sh, Bash instead

reads $ENV, for POSIX compatibility.

这些原文文献。基本上是很好理解的。在此你也不必纠结于自己的shell是什么类型的shell了。

*:match any string of zero or more characters。匹配任意数量的字符。

?:匹配单个字符。

[abc]匹配当中的某些字符,大于等于1

[!abc]就是匹配之外的

~:当前用户的家目录,说道这点要说个事情,就是当前用户,和最初的登录用户是不一样的概念。whoami和who am i分别指的是当前用户,和最初的登录用户。su的存在,或者ssh的存在可能会切换到其他用户,而其他用户进而就会变成当前用户,而当前用户的某些环境变量问题则又是另一块需要研究的内容了,不赘述。

~name:home directory of name

~+:$PWD(pwd)

~-:$LODPWD (cd -)

上面提到的都是基础的选项,都是由某些开关来控制的,属于基础,下面所提到的选项都是扩展变量开关所控制的,(执行shopt命令,查看这些开关),如下所示:

?(pattern) Match zero or one instance of patter n.

*(pattern) Match zero or more instances of patter n.

+(pattern) Match one or more instances of patter n.

@(pattern) Match exactly one instance of patter n.

!(pattern) Match any strings that don’t match patter n.

如果想使用上面的扩展必须打开相应的开关。

这是指的是shell环境中,比如egrep和awk本身就支持这些扩展。|表示或的意思。还有很多为了支持国际化语言,还有POSIX标准,这些不再赘述,i18n,l11n,分别是国际化,和本地化,大家明白为什么是18和11么?因为这是国际化和本地化的两个单词,中间字母的个数。普及下知识。有几个通配符(wildcards的例子)

ls new*,cat ch?,vi [D-R]* pr !(*.o|core)|lp

#######################################################################

quoting:

;command separator

&background execution

()command grouping

|pipe

<> & redirection symbols

*?[]~+-@!文件通配符,包括扩展的

"' \used in quoting other characters

``command subsitution

$ variavle substitution(cmd or arithmetic substitution)

space tab newline word separators

" " Ev erything between " and " is taken literally, except for the following characters that

keep their special meaning:

$ Variable (or command and arithmetic) substitution will occur.

‘ Command substitution will occur.

" This marks the end of the double quote.

’ ’ Ev erything between ’ and ’ is taken literally, except for another ’. You cannot embed

another ’ within such a quoted string.

\ The character following a \ is taken literally. Use within " " to escape ", $, and ‘.

Often used to escape itself, spaces, or newlines.

这个是quoting的解释,中文翻译过来英文总是存在很多的误差,比如单引号是可以屏蔽所有特殊字符的含义,但是它自身却不能被自身所屏蔽,这个在中文的很多shell文章翻译中都没提到,导致哥又一次工作处理特殊的文件名字的脚本的时候,老是报token错误。坑爹啊。剩下的翻译基本上ok。

$""和""是差不多的。差不多的含义是显示的时候是会带有""的,而纯粹的是不带有双引号的。

$``和``是差不多的。但是$``处理的是如下的字符:

Sequence Value Sequence Value

\a        Alert     \t   Tab

\b       Backspace   \v Vertical tab

\c        X Control character X \nnn Octal value nnn

\e Escape \xnn Hexadecimal value nn

\E Escape       \’ Single quote

\f Form feed    \" Double quote

\n Ne wline    \\ Backslash

\r Carriage return   

examples:

引用:

echo 'single quotes "protects" double quotes'

echo "well,isn't that \"special\""

echo "you have `ls |wc -l`files in `pwd`"

echo "the value of \$x is $x"

cmd & Execute cmd in background.

cmd1 ; cmd2 Command sequence; execute multiple cmds on the same line.

{ cmd1 ; cmd2; } Execute commands as a group in the current shell.

(cmd1 ; cmd2) Execute commands as a group in a subshell.

cmd1 | cmd2 Pipe; use output from cmd1 as input to cmd2.

cmd1 ‘cmd2‘ Command substitution; use cmd2 output as arguments to cmd1.

cmd1 $(cmd2) POSIX shell command substitution; nesting is allowed.

cmd $((expression)) POSIX shell arithmetic substitution. Use the result of expression as

argument to cmd.

cmd1 && cmd2 AND; execute cmd1 and then (if cmd1 succeeds) cmd2. This is a

“shor t circuit” operation: cmd2 is never executed if cmd1 fails.

cmd1 || cmd2 OR; execute either cmd1 or (if cmd1 fails) cmd2. This is a “shor t

circuit” operation; cmd2 is never executed if cmd1 succeeds.

! cmd NOT; execute cmd, and produce a zero exit status if cmd exits

with a nonzero status. Other wise, produce a nonzero status when

cmd exits with a zero status.

最后一条非常有意思。大体i意思就是本来一条命令被正确执行,但是退出的正确状态是0,但是加上!之后就是退出的状态是非0.

例子:

nroff file>file.txt &(nroff是转换编码方式,比如utf-8之类的,具体查看man nroff)

cd;ls

(date;who;pwd)>logfile

sort file|pr -3|lp

vi `grep -l ifdef *.c`

egrep '(yes|no)' `cat list`

egrep '(yes|no)' $(cat list)

egrep '(yes|no)' $(<list)

grep xx file && lp file

grep xx file || echo "xx not found"

######################################

redirection forms:

0 standard input  1 standard output 2 standard error

cmd >| file

Send output of cmd to file (overwrite), even if the shell’s noclobber option is set.这里面我只说这一个,就是这个强制重定向。即使是只读的。

除了012三个文件描述符,还有3-9用户可以自行使用的文件描述符,可以用来定义输入和输出。

exec 3>&

3>test.txt(定义了一个标准输出,输出到一个test.txt文件当中去)可以关闭这个标准输出。自我感觉用途不甚大,有机会可以自行研究。

例子:

cat part1>book

cat part2 part3>>book

mail tim<reprot

sed 's/^/xx /g'<<END_ARCHIVE

>

>

>END_ARCHIVE

echo "usage error:see administrator" 1>&2

find / -print >filelist 2>no_access

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章