简介:
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"要大写哈....
/bin/bash
基本语法,编写简单shell程序输出当前时间;
正则表达式,编写shell程序输出当前目录下所有的目录;
逻辑判断,编写shell程序判断/tmp/mybook是否存在,若不存在则创建
//编写简单的shell程序.输出时间
echo "time is :`date`" //追加一行
~
~
//运行shell程序
[root@localhost ~]# ./shell.sh
time is :2006年 11月 20日 星期一» 20:31:56 CST
time is :2006年 11月 20日 星期一» 20:31:56 CST
//编写shell程序.输出当前目录下所有的目录
ls -l|grep ^d //这里用到了管道.过滤.匹配输入字符串的开始位置
//运行shell程序
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是否存在.若不存在则创建
[ ! -d /tmp/mybook ]& &mkdir /tmp/mybook& &echo "/tmp/mybook is created!" ||echo "/tmp/mybook is exist!" //这行比较麻烦.有点错都会出问题.注意方括号内的两边有空格.该空格的地方要空格
~
//运行程序
/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程序.区分三种引号和反斜线..
echo 'time is:'`date`
abc="100"
echo 'time is: $abc'
echo "time is: $abc"
~
//运行shell程序..
time is:2006年 11月 20日 星期一 21:11:08 CST
time is: $abc
time is: 100
[root@localhost tmp]#
附:
shell使用两种引号和反斜线
双引号“”(让引号内的变量生效)
单引号‘’(引号内的变量当作一个字符使用)
反斜线/(禁用特殊字符功能/$)
后引号``(让引号内的命令生效)
转载时..
请注明:
转至http://blog.csdn.net/Kipen/ 作者:flyshitou(石の頭丶)
请尊重他人的劳动果实..
谢谢..
--by flyshitou (石の頭丶)