您的位置:首页 > 运维架构 > Shell

shell一些命令的测试(第六章 引用)

2010-04-03 18:54 447 查看
在fedora 12 下,学习还真的是件很郁闷的事情。Linux for QQ 经常报软件包错误就算了,大不了俺不上QQ。可以连自身带的FireFox也是自动退出,弹出软件包错误的提示!天啊,神人知道的,务必告知!

弄点了awk ,接下来就弄点shell的命令吧。那shell又是何方神圣呢?

Shell是一种具备特殊功能的程序, 它是介于使用者和 UNIX/linux 操作系统之核心程序(kernel)间的一个接口。也叫外壳!此处所指的shell是一种在UNIX/LINUX系统中的编程工具,他提供内置的命令,并能解释linux的命令,有自己的一套完整的语法规则,功能强大。

以下便是一些简单的测试。记录以下内容以便写所谓的实验报告!

=================单引号====================p2

示例:

在express查找:google

$grep google express

在express查找:google is

$grep google is express

思考:shell传了几个参数给grep? //google是参数 is 和express是文件夹

$grep ‘google is’ express

思考: shell传了几个参数给grep?

[root@biao LinuxTest]# cat express //express 的内容,以下所说的express文件都是此文件

"Open Source" is a good mechanism to develop programs.

apple is my favorite food.

Football game is not use feet only.

this dress doesn't fit me.

However, this dress is about $ 3183 dollars.

GNU is free air not free beer.

Her hair is very beauty.

I can't finish the test.

Oh! The soup taste good.

motorcycle is cheap than car.

This window is clear.

the symbol '*' is represented as start.

Oh! My god!

The gd software is a library for drafting programs.

You are the best is mean you are the no. 1.

The world <Happy> is the same with "glad".

I like dog.

google is the best tools for search keyword.

goooooogle yes!

go! go! Let's go.

# I am VBird

[root@biao LinuxTest]# grep google express

google is the best tools for search keyword.

[root@biao LinuxTest]# grep google is express

grep: is: 没有那个文件或目录 //此处的is表示的是文件或目录名,而不是字符串is

express:google is the best tools for search keyword.

[root@biao LinuxTest]# grep 'google is' express

google is the best tools for search keyword.

[root@biao LinuxTest]#

p4

tip:shell会忽略掉单引号内的所有特殊字符的原来作用。 而双引号则不会。

text =‘* means all files in zhe directory’

echo $text

echo “$text”

[root@biao LinuxTest]# text='*means all files in the directory' //变量

[root@biao LinuxTest]# echo $text

*means all files in the directory

[root@biao LinuxTest]# echo "$text"

*means all files in the directory

[root@biao LinuxTest]#

tip:双引号中,除了下面的三种字保留本意外,其他的被shell忽略:

1:$

2:反引号``

3:反斜杠/

p6 ====反斜杠=======

作用1:

等价与在一个字符前后加上单引号 //这是为了在双引号里面单引号失去作用,从而反斜杠可以替换

比较:echo > 和 echo /> //前者又语法错误 后者相当于‘>'

x=* 比较:echo $x 和 echo /$x

思考 :echo // 的作用? //是反斜杠失去原有的特有意义

[root@biao LinuxTest]# echo >

bash: syntax error near unexpected token `newline'

[root@biao LinuxTest]# echo />

>

[root@biao LinuxTest]# x=*

[root@biao LinuxTest]# echo $x

1.txt~ 4-awk.ppt awkfile awkfile~ ep~ epX express express~ grade grade~ nf nu result3 result3~ resultAWK.txt resultShell resultShell~ sub tot tot~ whos

[root@biao LinuxTest]# echo /$x

$x

[root@biao LinuxTest]# echo //

/

作用2:

当用于一行的最后一个字符时,表示换行。

$ lines=one

>two

比较:$ echo “$lines” ,echo $lines, echo ‘lines’

[root@biao LinuxTest]# lines=one

[root@biao LinuxTest]# echo "$lines"

one

[root@biao LinuxTest]# echo $lines

one

[root@biao LinuxTest]# echo 'lines'

lines

p8

练习:在终端上显示

<<< echo $x >>> displays the value of x,which is $x

显示的时候,第2个$x的值应该被替换,比如如果x=3,显示:

<<< echo $x >>> displays the value of x,which is 3

[root@biao LinuxTest]# x=3

[root@biao LinuxTest]# echo "<<< echo /$x >>> displays the value of x,which is $x"

<<< echo $x >>> displays the value of x,which is 3

[root@biao LinuxTest]#

p9

Shell 执行反引号中命令,并把该命令的标准输出插在命令所在的位置。

echo your current working directory is `pwd`

[root@biao LinuxTest]# echo your current working directory is `pwd`

your current working directory is /root/LinuxTest

[root@biao LinuxTest]#

p10

[root@biao LinuxTest]# cat nu

echo there are $(who |wc -l)users logged in

[root@biao LinuxTest]# ./nu //可能需要修改其权限 chmod +X 文件名

there are 3users logged in

[root@biao LinuxTest]# now=$(date)

[root@biao LinuxTest]# echo $now

2010年 04月 03日 星期六 18:09:40 HKT

[root@biao LinuxTest]# filelist=$(ls)

[root@biao LinuxTest]# echo $filelist

1.txt~ 4-awk.ppt awkfile awkfile~ ep~ epX express express~ grade grade~ nf nu result3 result3~ resultAWK.txt resultShell resultShell~ sub tot tot~ two whos

[root@biao LinuxTest]# echo "$filelist"

1.txt~

4-awk.ppt

awkfile

awkfile~

ep~

epX

express

express~

grade

grade~

nf

nu

result3

result3~

resultAWK.txt

resultShell

resultShell~

sub

tot

tot~

two

whos

[root@biao LinuxTest]# echo '$filelist'

$filelist

[root@biao LinuxTest]# namelist=$(cat names)

cat: names: 没有那个文件或目录

[root@biao LinuxTest]#

p12

[root@biao LinuxTest]# pwd //确认下当前的路径

/root/LinuxTest

[root@biao LinuxTest]# filename=/root/LinuxTest/express //复制路径

[root@biao LinuxTest]# filename=$(echo $filename |tr "$(echo $filename |cut -c 1)" "^") //暂时不知所云!

[root@biao LinuxTest]# echo filename

filename

[root@biao LinuxTest]#

p13

计算表达式的值,跟$((expression))一样

expr 1 + 2

i=1

i=$(expr $i + 1)

[root@biao LinuxTest]# expr 1+2 //??????????

1+2 //正确结果为3

[root@biao LinuxTest]# i=1

[root@biao LinuxTest]# i=$(expr $i+1)

[root@biao LinuxTest]# echo $i

1+1

[root@biao LinuxTest]#

意为:filename存放文件路径字符串,语句就是将文件路径字符串输入到管道,然后通过tr命令 将参数二转换为参数一。cut负责将从文件路径字符串中取出第一个字符。

最后输出结果:^root^LinuxTest^express

expr命令的参数必须以空格隔开。 如expr 1 + 2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: