乔部落格

世界上有10种人:认识二进制的和不认识二进制的

shell总结

joe posted @ 2010年4月24日 17:27 in shell with tags bash , 3080 阅读

Shell元字符

元字符

含义

>

将标准输出重定向到文件

>>

将标准输出附加到文件

<

从文件中获取标准输入

|

p1|p2p1的标准输出作为p2的标准输入

<<

here文档:标准输入从here文档读入,直到出现串

*

匹配文件名中的零个或多个字符

?

匹配文件名中的任何单个字符

[ccc]

匹配文件名中 ccc范围内的任何字符

;

命令结束符,如p1;p2表示先执行p1,再执行p2

&

;类似,但不等p1结束

`…`

运行中的命令,输出结果代替`…`

(…)

在子shell中运行中的命令

{…}

在当前shell中运行中的命令

$1, $2

$0…$9可代表shell文件的参数

$变量

Shell变量的值

${变量}

Shell变量的值,为避免在文本联接时混淆

\

转义字符,\cc字符作为字符

‘…’

单引号内表示文字

…”

中的$, `…`, \得到解释后,将作为文本文字

#

注释

变量=

为变量赋值

p1&&p2

运行p1;若成功,再运行p2

p1||p2

运行p1;若不成功,再运行p2

 

 

 

Shell内部变量

变量

含义

$#

参数个数

$*

Shell的所有参数

$@(注)

类似$*

$-

Shell的选项

$?

上次执行命令的返回值

$$

Shellpid

$!

&启动的最后一个命令的pid

$HOME

Cd命令的默认参数

$IFS

参数分隔符

$PATH

搜索命令的目录表

$PS1,$PS2

提示符

注:($*$@区别)

1.$*$@扩展为参数,并被重复扫描;参数的空格将字符串分成多个参数。

2.”$*”表示shell文件的所有参数及其空格连在一起作为单个词处理。

3.”$@”shell文件接收的参数等价,参数中的空格被忽略,其结果是等同于原来参数的一个单词列表。

 

Shell变量的赋值

变量

赋值

$var

var的值;若var无定义则无值

${var}

同上;用于变量后跟着字母数字串的情况

${var-thing}

var有定义时,取值var;否则取值thing,而$var的值不变

${var=thing}

var有定义时,取值var;否则取值thing$var的值设为thing

${var?message}

var有定义时,取值var;否则打印message

${var+thing}

var有定义时,取值thing;否则无值

 

>atouch a创建文件。

Cat > a从标准输入获取字符,写入文件a中,按ctrl-d结束。

(以上参考《UNIX编程环境》)

为了装软件方便,我写了一个简单的小脚本(我使用yum作为软件包管理者)。

脚本如下:

 

 

#!/bin/bash

#program name: pack.sh

if [ $# -eq 0 ]

then

echo “try pach.sh -h for help”

exit 1

fi

case $1 in

“-h”)echo “usage: pack.sh -[suihc] ‘pack’”

echo “-s for search, -u for update, -i for install, -c for check updates”

;;

“-s”)echo “This may take a few minutes, please wait…”

sudo yum list | grep -i $2

;;

“-c”)echo “This may take a few minutes, please wait…”

if [ -z $2 ]

then

sudo yum check-update

else

sudo yum check-update | grep -i $2

fi

;;

“-u”)sudo yum -y update $2

;;

“-i”)sudo yum -y install $2

;;

*)echo “NO such options!try pach.sh -h”

exit 2

;;

esac

 

 

seo 说:
2021年9月23日 23:33

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. Painters in Melbourne Eastern Suburbs

seo 说:
2021年9月23日 23:37

If you are looking for more information about flat rate locksmith Las Vegas check that right away. House Painters West Auckland

seo 说:
2021年9月23日 23:40

Thank you for taking the time to publish this information very useful! Scaffolding Auckland


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter