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:vk@fackmail.co.in
}


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

 

CommandExplanation
gAll 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.

 

CommandExplanation
1,$ Line Address location is all i.e. find all lines for following pattern
sSubstitute 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.
gGlobal 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 解压

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值