shell每日更新(7)

来源:互联网 发布:淘宝商城空调 编辑:程序博客网 时间:2024/04/29 11:41

这里说明下最近一段时间一直没有更新的原因吧,也是自己偷懒的原因:上周的六级考试。

这里我会一直坚持写完shell的学习历程:

6月25号晚上9点

shell脚本编写的一些规则:

1,开头

程序必须以下面的行开始(必须方在文件的第一行):
#!/bin/sh
符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中我们使用/bin/sh来执行程序。
当编辑好脚本时,如果要执行该脚本,还必须使其可执行。
要使脚本可执行:
编译 chmod +x filename 这样才能用./filename 来运行也可以使用sh filename或者 bash filename。这里如果需要进行测试的话最好加上-x

[root@fsailing1 shell]# sh -x read.sh+ echo 'Can you tell me what are you looking for?'Can you tell me what are you looking for?+ read lookchen+ echo 'you are looking for chen'

2,注释

在进行shell编程时,以#开头的句子表示注释,直到这一行的结束。我们真诚地建议您在程序中使用注释。
如果您使用了注释,那么即使相当长的时间内没有使用该脚本,您也能在很短的时间内明白该脚本的作用及工作原理。

其实就是为了方便大家参考内容。

3,变量

在其他编程语言中您必须使用变量。在shell编程中,所有的变量都由字符串组成,并且您不需要对变量进行声明。要赋值给一个变量,您可以这样写:
#!/bin/sh
#对变量赋值:
a="hello world"
# 现在打印变量a的内容:
echo "A is:"
echo $a
有时候变量名很容易与其他文字混淆,这里的表示方式有很多,比如:
num=2
echo "this is the $numnd"
这并不会打印出"this is the 2nd",而仅仅打印"this is the ",因为shell会去搜索变量numnd的值,但是这个变量时没有值的。可以使用花括号来告诉shell我们要打印的是num变量:
num=2
echo "this is the ${num}nd"
这将打印: this is the 2nd

[root@fsailing1 shell]# cat abc.sh#!/bin/bash# a simple shell script Example# a functionfunction say_hello(){        echo "Enter Your name:"        read name        echo "hello $name"}echo "Programer starts Here^^^^^"say_helloecho "Programme Ends."

4,就是环境变量

通常的做法就是export关键字处理过的变量作为环境变量。我们不对环境变量进行讨论,因为通常情况下仅仅在登录脚本中使用环境变量。

6月26号下午1点:

find使用说明:

Find path】 【option】【option

与时间有关的参数:-atime -ctime -mtime

-mtime n n表示数字,意思是在n天之前的“一天之内”被更改过的文件。

-mtime +n:在n天之前(不包含n天本身)被更改过的文件名。

-mtime -n:在n天之内(包含n天本身)被更改过的文件名。

EG:想找出系统上面24小时内有改动的文件:这里test是刚刚被修改的为了测试find

[root@fsailing1 shell]# find /root/shell -mtime 0/root/shell/root/shell/test.txt[root@fsailing1 shell]#

-atime n 查找n天以前被访问过的所有文件。 

-ctime n 查找n天以前文件状态被修改过的所有文件。 

与用户、用户组相关的:-user -uid -gid -group

与文件类型、名称有关的:-type -name -size -perm

-type x 查找类型为 x 的文件,x 为下列字符之一:

b 块设备文件

c 字符设备文件

d 目录文件

p 命名管道(FIFO)

f 普通文件

l 符号链接文件(symbolic links)

s socket文件

其他的重要操作:-exec -print

-exec commandcommand是其他命令,-exec后面可再接其他的命令来处理查找到的结果。

[root@fsailing1 shell]# find /root/shell -mtime 0 -exec cat test.txtfind:遗漏" -exec"遗漏[root@fsailing1 shell]# find /root/shell -mtime 0 -exec cat {}\;find:遗漏" -exec"遗漏[root@fsailing1 shell]# find /root/shell -mtime 0 -exec catfind:遗漏" -exec"遗漏[root@fsailing1 shell]# find /root/shell -mtime 0 -exec cat {} \;cat: /root/shell: 是一个目录100a 100b -50c -20d -30#test find -mtime#########[root@fsailing1 shell]#


原创粉丝点击