早上了,還有一個小時正式開工,先看會書。嘎嘎。。
上回書說道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