1 什么是shell

shell在系统中的位置: 用户->应用->shell->内核->硬件
shell是和内核亲密接触的,提供和内核之间的接口,可以写出功能很强大的脚本文件

2 有哪些类型

shell有很多版本:sh -> csh -> ksh -> bash -> tcsh ...
一般在 /bin/下
你要用哪个shell执行程序,则 /bin/sh file 或者 /bin/bash file
用当前默认shell执行可以这样 . file
或者你可以这样 /bin/bash < test.sh
shell都是读入程序代码执行的,在可执行脚本第一行可以指定执行的shell

3 程序参数

./test.sh a b c (abc就是参数)
内部可以获取参数情况:
$# 参数个数
$? 最后命令的完成码或者内部执行的shell返回值
$0 shell程序
$* 所有参数组成的字符串
$N 第N个参数

4 赋值

a=b;a="b c";a="b $HOME";a='b $HOME';a="${HOME}W"
let "j=i*5+1" 等同 ((j=i*5+1))

5 输入输出


[root@localhost ~]# echo -n "your Name:";read myname;echo "Your name is "$myname
your Name:aslibra.com
Your name is aslibra.com


echo "string">/tmp/a.txt
program >/dev/null 2>&1
/bin/bash < file
program >> log.txt
program | sort | uniq | wc -l >> log.txt

输出有两个类型,系统错误2和标准输出1
a文件不存在,cat会产生系统错误为2,2>&1可以转到标准输出

做一个示例脚本来说明问题,产生标准输出和错误输出(中文为注释):

[root@aslibra ~]# cat test.sh
echo hello
echo -n `cat a`
[root@aslibra ~]# ./test.sh
hello
cat: a: No such file or directory
#第一行正常输出,第二行没有a文件,产生错误输出
[root@aslibra ~]# ./test.sh >output
cat: a: No such file or directory
#输出重定向到文件,也就不会在界面看到标准输出
[root@aslibra ~]# cat output
hello
#标准输出在文件里面
[root@aslibra ~]# ./test.sh >output 2>&1
#把错误输出输出到标准输出
[root@aslibra ~]# cat output
hello
cat: a: No such file or directory
#两个都在output文件里面了
[root@aslibra ~]# echo>output
#清空output文件
[root@aslibra ~]# ./test.sh >output 1>&2
hello
cat: a: No such file or directory
#把标准输出转到错误输出
[root@aslibra ~]# cat output
[root@aslibra ~]#
#这样output文件没有任何内容


原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
Tags: ,
发表评论
表情
emotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemotemotemotemotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写
 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐