【原创】shell基本语法.正则表达式.逻辑判断实验....

 

简介:

   shell 是计算机用来解释你输入的命令然后决定进行何 种处理的程序。shell 也可以在非交互模式下调用。
   在Linux系统下有几个不同的shell:
   bash "Bourne Again" shell
   sh Bourne shell,在很多UNIX下是标准的shell
   csh C shell,和C语言语法相近,大部分UNIX下有效
domain Korn shell
shell, 在小的系统里经常使用
   一般的Linux系统都将bash作为默认的shell
   查看你自己的shell类型:
   echo $SHELL   //记得"SHELL"要大写哈....

[root@localhost tmp]# echo $SHELL
/bin/bash


要求:
基本语法,编写简单shell程序输出当前时间;
正则表达式,编写shell程序输出当前目录下所有的目录;
逻辑判断,编写shell程序判断/tmp/mybook是否存在,若不存在则创建
操作系统:FC6

//编写简单的shell程序.输出时间

[root@localhost ~]# vi shell.sh
echo "time is :`date`"   //追加一行
~
~

//运行shell程序

[root@localhost ~]# chmod 755 shell.sh
[root@localhost ~]# ./shell.sh
time is :2006年 11月 20日 星期一» 20:31:56 CST
[root@localhost ~]# sh shell.sh               //两个命令都可以
time is :2006年 11月 20日 星期一» 20:31:56 CST

//编写shell程序.输出当前目录下所有的目录

[root@localhost var]# vi dir.sh
ls -l|grep ^d  //这里用到了管道.过滤.匹配输入字符串的开始位置

//运行shell程序

[root@localhost var]# sh dir.sh  (用 ./dir.sh  也可以)
drwxr-xr-x  2 root    root    4096 11-04 19:31 account
drwxr-xr-x 11 root    root    4096 11-04 20:23 cache
drwxr-xr-x  3 netdump netdump 4096 11-04 19:31 crash
drwxr-xr-x  3 root    root    4096 11-04 19:31 db
drwxr-xr-x  3 root    root    4096 11-04 19:30 empty
drwxr-xr-x  2 root    root    4096 10-11 06:06 games
drwxrwx--T  2 root    gdm     4096 11-06 13:17 gdm
drwxr-xr-x 18 root    root    4096 11-04 20:23 lib
drwxr-xr-x  2 root    root    4096 10-11 06:06 local
drwxrwxr-x  5 root    lock    4096 11-20 19:29 lock
drwxr-xr-x 10 root    root    4096 11-20 19:29 log
drwxr-xr-x  2 root    root    4096 10-11 06:06 nis
drwxr-xr-x  2 root    root    4096 10-11 06:06 opt
drwxr-xr-x  2 root    root    4096 10-11 06:06 preserve

//编写shell程序.判断/tmp/mybook是否存在.若不存在则创建

[root@localhost ~]# vi shell2.sh
[ ! -d /tmp/mybook ]&
&mkdir  /tmp/mybook& &echo  "/tmp/mybook is created!" ||echo "/tmp/mybook is exist!"   //这行比较麻烦.有点错都会出问题.注意方括号内的两边有空格.该空格的地方要空格
~

//运行程序

[root@localhost ~]# sh shell2.sh
/tmp/mybook is created!
[root@localhost ~]# sh shell2.sh
/tmp/mybook is exist!

附:

___________________
shell执行

 首先将shell的脚本设置成可执行

 chmod +x filename
 方法1:shell名+要执行的脚本
_________________
shell基本语法
 输入输出重定向
 “>”和“<”
 “>>”和“<<”
 1>:正确重定向
 2>:错误重定向
 2>&1:正确和错误一起重定向
 Command > file
 管道pipe
 command1| command2[| command3...]
 前台和后台
 “;”和“&”
 过滤grep
 在某个文件中找到你想要的内容

______________________
正则表达式
^:匹配输入字符串的开始位置。
 例如:^z能匹配 所有以“z”开头的字符串


//这个就好好分析一下..花几分钟记下它们的区别

编写shell程序.区分三种引号和反斜线..

[root@localhost tmp]# vi aa
echo 'time is:'`date`
abc="100"
echo 'time is: $abc'
echo "time is: $abc"
~

//运行shell程序..

[root@localhost tmp]# ./aa
time is:2006年 11月 20日 星期一 21:11:08 CST
time is: $abc
time is: 100
[root@localhost tmp]#

附:

shell使用两种引号和反斜线
 双引号“”(让引号内的变量生效)
 单引号‘’(引号内的变量当作一个字符使用)
 反斜线/(禁用特殊字符功能/$)
 后引号``(让引号内的命令生效)

ps:此帖绝对是本人原创...

转载时..
请注明:
转至http://blog.csdn.net/Kipen/   作者:flyshitou(石の頭丶)
请尊重他人的劳动果实..
谢谢..

 --by flyshitou (石の頭丶)

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值