Linux Shell

http://www.freeos.com/guides/lsst/ch07sec04.html

http://www.tldp.org/LDP/abs/html/why-shell.html

 

Linux Shell Expression

Shell Built in Variables Meaning
$0 the script full name
$# Number of command line arguments. Useful to test no. of command line args in shell script.
$* All arguments to shell
$@ Same as above
$- Option supplied to shell
$$ PID of shell
$! PID of last started background process (started with &)

$? return the result of running

$ is the special character of ex which mean last-line character.


/foo/ mean pattern
g use g, which mean global line address
s/foo/foo/g s mean a command "substitute"
"g /Unix/ s//Linux" Here // is replace by the last pattern/regular expression i.e. Unix.

Command Explanation
g All occurrence
/[^ [^] This means not
/^$ Empty line, Combination of ^ and $


:1,$ s/^   *$// this is an example

 

.任意字符
^行首匹配
$行尾匹配
^$表示空行,不含字符的行
^ $匹配只有單個空格的行
[0-9]
[a-zA-Z]
^[A- Z]搜索以大寫字母開頭的行
[^A-Z]匹配大寫字母意外的任意字符
*表示匹配0個或若干個字符,如:a*,表示匹配0個或若干個a;  aa*表示匹配至少1個a
.*來表示0或若干個任意字符
e.e*表示匹配第一個e和最後一個e之間的任意字符
[-0-9]匹配一 個連字符或數字
[]a-z]匹配一個]或者字母
\{min,max\}匹配任意數目的字符串
[a-z]\{10\}只匹配10個 a-z字符的字符串
s/.\{5\}$// 刪除每行的最後5個字符
\(...\),n是1到9的數字,表示存儲用的寄存器,用\n來引 用存在寄存器中的內容
^\(.\)\1匹配行首的第一個字符,並將該字符存到1號寄存器中,然後匹配1號寄存器中的內容,這由\1的描述。該正則 表達式的最終效果是,如果一行的頭兩個字符相同,就匹配他們。
^\(.\).*\1$匹配一行中的頭一個字符(^.)跟最後一個字符(\1$)相 同的行。.*匹配中間的所有內容
^\(...\)\(...\)行中頭三個字符存在1號寄存器,接着的三個字符存在2號寄存器.
s/\(.*\)  \(.*\)/\2 \1/g 交換兩個字段

.任何字符
^行首
$行尾
*前導的正則表達式重複0或若干次
[字 符表]字符中的任一字符
a..表示a後的2個字符
^wood表示行首的wood
x$表示行爲的x
^INSERT$只包含 字符串INSERT的行
^$不包含任何字符的行
x*表示0或若干個連續的x
xx*表示1或多個連續的x
.*表示0活若干 個字符
w.*s表示以w開始,s結尾的任何字符串
[tT]小寫或大寫的t

[^字符表]表示任一不在字符表中的字符 [^0-9] [^a-zA-Z]
\{min,max\}表示前導的正則表達式重複只燒min次,至多max次[0-9]\{3,9\}表示3到 9個數字
\(...\)表示將小括號中匹配的字符串存儲到下一個寄存器中(1-9),
^\(.\)表示行中第1個字符存到1號寄存器
^\ (.\)\1表示行首戀歌字符,且他們相同

awk
Metacharacter Meaning
. (Dot) Match any character
* Match zero or more character
^ Match beginning of line
$ Match end of line
\ Escape character following
[ ] List
{ } Match range of instance
+ Match one more preceding
? Match zero or one preceding
| Separate choices to match

awk Variable Meaning
FILENAME Name of current input file
RS Input record separator character (Default is new line)
OFS Output field separator string (Blank is default)
ORS Output record separator string (Default is new line)
NF Number of input record
NR Number of fields in input record
OFMT Output format of number
FS Field separator character (Blank & tab is default)

sed
Option Meaning Example
-e Read the different sed command from command line.
 $ sed  -e  'sed-commands'    data-file-name
        $ sed  -e   's/Linux/UNIX(system v)/'    demofile1
-f Read the sed command from sed script file.
 $sed   -f   sed-script-file    data-file-name
        $ sed  -f  chgdb.sed    friends.tdb
-n Suppress the output of sed command. When -n is used you must use p command of
        print flag. $ sed -n  '/^\*..$/p'   demofile2

Matcheing any number of occurrence
Syntax:
\{n,\m}
Matches any number of occurrence between n and m.

Sed an example:
$ cat > mkchgfrddb
s/A.bad/Aurangabad/g
s/MH/Maharastra/g
s/^$/===================================================================/g
/V.K. /{
N
N
a\
email:[email protected]
}


Compare

Regular expression
Escaped "angle brackets" -- \<...\> -- mark word boundaries.
"\<the\>" matches the word "the," but not the words "them,"  "there," "other," etc.
Escaped "curly brackets" -- \{ \} -- indicate the number of occurrences of a preceding RE to match

 

Command Explanation
g All occurrence
/[^ [^] This means not
/^$ Empty line, Combination of ^ and $.

 

Parentheses -- ( ) -- enclose a group of REs. They are useful with the following "|" operator and in substring extraction using expr.

The -- | -- "or" RE operator matches any of a set of alternate characters.


POSIX Character Classes. [:class:]

This is an alternate method of specifying a range of characters to match.

[:alnum:] matches alphabetic or numeric characters. This is equivalent to A-Za-z0-9.

[:alpha:] matches alphabetic characters. This is equivalent to A-Za-z.

[:blank:] matches a space or a tab.

[:cntrl:] matches control characters.

[:digit:] matches (decimal) digits. This is equivalent to 0-9.

[:graph:] (graphic printable characters). Matches characters in the range of ASCII 33 - 126. This is the same as [:print:], below, but excluding the space character.

[:lower:] matches lowercase alphabetic characters. This is equivalent to a-z.

[:print:] (printable characters). Matches characters in the range of ASCII 32 - 126. This is the same as [:graph:], above, but adding the space character.

[:space:] matches whitespace characters (space and horizontal tab).

[:upper:] matches uppercase alphabetic characters. This is equivalent to A-Z.

[:xdigit:] matches hexadecimal digits. This is equivalent to 0-9A-Fa-f.

 

Command Explanation
1,$ Line Address location is all i.e. find all lines for following pattern
s Substitute command
/[a-z]/ Find all lowercase letter - Target
\u&/ Substitute to Uppercase. \u& means substitute last patter (&) matched with its UPPERCASE replacement (\u)Note: Use\l (small L) for lowercase character.
g Global replacement

 

Redirection IO
Standard File File Descriptors number Use Example
stdin 0 as Standard input  Keyboard
stdout 1 as Standard output  Screen
stderr 2 as Standard error  Screen

   1>filename
      # Redirect stdout to file "filename."
   1>>filename
      # Redirect and append stdout to file "filename."
   2>filename
      # Redirect stderr to file "filename."
   2>>filename
      # Redirect and append stderr to file "filename."
   &>filename
      # Redirect both stdout and stderr to file "filename."
      # This operator is now functional, as of Bash 4, final release.

   M>N
     # "M" is a file descriptor, which defaults to 1, if not explicitly set.
     # "N" is a filename.
     # File descriptor "M" is redirect to file "N."
   M>&N
     # "M" is a file descriptor, which defaults to 1, if not set.
     # "N" is another file descriptor.
   [j]<>filename
      #  Open file "filename" for reading and writing,
      #+ and assign file descriptor "j" to it.
      #  If "filename" does not exist, create it.
      #  If file descriptor "j" is not specified, default to fd 0, stdin.
      #
      #  An application of this is writing at a specified place in a file.
      echo 1234567890 > File    # Write string to "File".
      exec 3<> File             # Open "File" and assign fd 3 to it.
      read -n 4 <&3             # Read only 4 characters.
      echo -n . >&3             # Write a decimal point there.
      exec 3>&-                 # Close fd 3.
      cat File                  # ==> 1234.67890
      #  Random access, by golly.

File Descriptors


ls -yz >> command.log 2>&1 # any error message will also be there
ls -yz 2>&1 >> command.log # the error message goes only to stdout

Subshell
method ##!/bin/bash
        (...)

Function:
REPLY=$(echo $(wc -l < /etc/passwd))

 

 

 

() 意義:找出『羣組』字串
範例:搜尋 (glad) 或 (good) 這兩個字串,因為 g 與 d 是重複的,所以, 我就可以將 la 與 oo 列於 ( ) 當中,並以 | 來分隔開來,就可以啦!
egrep -n 'g(la|oo)d' regular_express.txt
()+ 意義:多個重複羣組的判別
範例:將『AxyzxyzxyzxyzC』用 echo 叫出,然後再使用如下的方法搜尋一下!
echo 'AxyzxyzxyzxyzC' | egrep 'A(xyz)+C'
上面的例子意思是說,我要找開頭是 A 結尾是 C ,中間有一個以上的 "xyz" 字串的意思~

 

 

Linux Administration
Aaddress:http://www.yolinux.com/TUTORIALS/LinuxTutorialSysAdmin.html

Network management with the Linux Shell
    * ping [-n] machine : Sends a ping to a machine (-n: no DNS))
    * traceroute [-n] machine : Makes a traceroute to a machine (-n: no DNS)
    * netstat [-n] : Shows network usage by processes
    * netstat [-n] -a : Same, with the display of server processes
    * fuser, fstart, lsof : Detailed list of the use of files and network
    * ifconfig -a : Displays the configuration of network interfaces
    * ifconfig interface IP mask : Configure a network interface
    * route [-n] show : Displays the routing table (-n: no DNS)
    * route [-n] add route [gw] gateway : Add a routing entry [gw only]
    * route add default [gw] gateway : Adds a default route [gw only]
    * route delete default : Delete the default route
    * hostname : Displays and sets the machine name
    * /etc/resolv.conf : Configuration file for DNS resolution
    * whois domain name (whois Kioskea.net par exemple) : Displays information about the domain name

Basic command line:
top            Show top processes
iostat           Report CPU statistics and input/output statistics for devices and
                 partitions.
ps -auxw    process status

Memory Usage:

Linux Commands to Monitor Memory Usage:

    vmstat  Monitor virtual memory
    free  Display amount of free and used memory in the system. (Also: cat /proc/meminfo)
    pmap  Display/examine memory map and libraries (so). Usage: pmap pid
    top  Show top processes
    sar -B  Show statistics on page swapping.
    time -v date  Show system page size, page faults, etc of a process during execution. Note you must fully qualify the command as "/usr/bin/time" to avoid using the bash shell command "time".
    cat /proc/sys/vm/freepages  Display virtual memory "free pages".
    One may increase/decrease this limit: echo 300 400 500 > /proc/sys/vm/freepages
    cat /proc/meminfo  Show memory size and usage.

Hardware Info:
/usr/bin/lsdev    List devices and info on system hardware. Also IRQ's.(RPM package procinfo)
Also cat /proc/devices
/sbin/lspci  list all PCI devices (result of probe) Also lspci -vvx and cat /proc/pci
cat /proc/interrupts  List IRQ's used by system and the device using the interrupt.
cat /proc/ioports  List I/O ports used by system.
cat /proc/dma  List DMA channels and device used by system.
cat /proc/cpuinfo  List info about CPU

 

 linux解壓縮命令

tar

-c: 建立壓縮檔案
-x:解壓
-t:查看內容
-r:向壓縮歸檔文件末尾追加文件
-u:更新原壓縮包中的文件

這五個是獨立的命令,壓縮解壓都要用到其中一個,可以和別的命令連用但只能用其中一個。下面的參數是根據需要在壓縮或解壓檔案時可選的。

-z:有gzip屬性的
-j:有bz2屬性的
-Z:有compress屬性的
-v:顯示所有過程

-O:將文件解開到標準輸出

下面的參數-f是必須的

-f: 使用檔案名字,切記,這個參數是最後一個參數,後面只能接檔案名。

# tar -cf all.tar *.jpg
這條命令是將所有.jpg的文件打成一個名爲all.tar的包。-c是表示產生新的包,-f指定包的文件名。

# tar -rf all.tar *.gif
這條命令是將所有.gif的文件增加到all.tar的包裏面去。-r是表示增加文件的意思。

# tar -uf all.tar logo.gif
這條命令是更新原來tar包all.tar中logo.gif文件,-u是表示更新文件的意思。

# tar -tf all.tar
這條命令是列出all.tar包中所有文件,-t是列出文件的意思

# tar -xf all.tar
這條命令是解出all.tar包中所有文件,-x是解開的意思

壓縮

tar –cvf jpg.tar *.jpg//將目錄裏所有jpg文件打包成tar.jpg

tar –czf jpg.tar.gz *.jpg     //將目錄裏所有jpg文件打包成jpg.tar後,並且將其用gzip壓縮,生成一個gzip壓縮過的包,命名爲jpg.tar.gz

tar –cjf jpg.tar.bz2 *.jpg//將目錄裏所有jpg文件打包成jpg.tar後,並且將其用bzip2壓縮,生成一個bzip2壓縮過的包,命名爲jpg.tar.bz2

tar –cZf jpg.tar.Z *.jpg     //將目錄裏所有jpg文件打包成jpg.tar後,並且將其用compress壓縮,生成一個umcompress壓縮過的包,命名爲jpg.tar.Z

rar a jpg.rar *.jpg //rar格式的壓縮,需要先下載rar for linux

zip jpg.zip *.jpg //zip格式的壓縮,需要先下載zip for linux

解壓

tar –xvf file.tar//解壓 tar包

tar -xzvf file.tar.gz//解壓tar.gz

tar -xjvf file.tar.bz2     //解壓 tar.bz2

tar –xZvf file.tar.Z     //解壓tar.Z

unrar e file.rar//解壓rar

unzip file.zip //解壓zip

總結

1、*.tar 用 tar –xvf 解壓

2、*.gz 用 gzip -d或者gunzip 解壓

3、*.tar.gz和*.tgz 用 tar –xzf 解壓

4、*.bz2 用 bzip2 -d或者用bunzip2 解壓

5、*.tar.bz2用tar –xjf 解壓

6、*.Z 用 uncompress 解壓

7、*.tar.Z 用tar –xZf 解壓

8、*.rar 用 unrar e解壓

9、*.zip 用 unzip 解壓

 

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