Table of Contents
1. Shell简介
1.1 Shell种类
1.2 Shell 对比
1.3 详细特性比较
1.4 shell初始文件
1. Shell简介
1.1 Shell种类
· C shell
· TC shell
· Bourne shell
· Bash shell
· Korn shell
其中,C shell 和 TC shell依照C语言的语法,而Bourne shell 是基于老式的编程语言Algol, Bash shell 和 Korn shell 起源于Bourne shell,但也可以看作是Bourne shell and C shell 的结合。
1.2 Shell 对比
项目 | C | TC | Boume | bash | Korn |
别名 | 是 | 是 | 否 | 是 | 是 |
高级模式匹配 | 否 | 否 | 否 | 是 | 是 |
命令行编辑 | 否 | 是 | 否 | 是 | 是 |
目录栈 | 是 | 是 | 否 | 是 | 否 |
文件名补全 | 是[*] | 是 | 否 | 是 | 是 |
函数 | 否 | 否 | 是 | 是 | 是 |
历史 | 是 | 是 | 否 | 是 | 是 |
作业控制 | 是 | 是 | 否 | 是 | 是 |
键绑定 | 否 | 是 | 否 | 是 | 否 |
格式化提示符 | 否 | 是 | 否 | 是 | 否 |
拼写纠正 | 否 | 是 | 否 | 是 | 否 |
1.3 详细特性比较
特性 | C/TC | Bourne | bash | Korn |
shell结构 | ||||
shbang行 | #!/bin/csh #!/bin/tcsh | /bin/sh | #!/bin/bash | #!/bin/ksh |
注释 | # this is comment | # this is comment | # this is comment | # this is comment |
变量 | ||||
局部变量赋值 | set x=5 | x=5 | x=5 | x=5 |
环境变量 | setenv VAR value | NAME=’Bob’ export NAME | export NAME=’Bob’ | export NAME=’Bob’ |
访问变量 | echo $NAME | echo $NAME | echo $NAME | echo $NAME 或print $NAME |
字符个数 | echo $%var | N/A | ${#var} | ${#var} |
进程PID | $$ | $$ | $$ | $$ |
退出状态 | $status,$? | $? | $? | $? |
前一个后台作业 | $!(tcsh only) | $! | $! | $! |
数组 | ||||
给数组赋值 | set x=( a b c ) | N/A | y[0]=’a’;y[1]=’b’ | y[0]=’a’;y[1]=’b’ |
访问数组元素 | echo $x[1] | N/A | echo ${y[0]} | print ${y[0]} |
I/O | ||||
读取输入 | set var=$< | read var | read var | read var |
算术 | ||||
执行计算 | @var = 5 + 1 | var=`expre 5 + 1` | (( n = 5+ 5)) | (( n = 5+ 5)) |
初始化文件 | ||||
登录时执行 | .login | .profile | .bash_profile | .profile |
每次调用shell 时 | .cshrc | N/A | .bashrc | .kshrc |
编程结构 | ||||
if条件语句 | if(exp) then commands endif
if{ ( command ) } then commands endif | if[ exp ] then commands fi
if command then commands fi | if[[ string exp ]] then commands fi if((numeric exp)) then commands fi | if[[ string exp ]] then commands fi if((numeric exp)) then commands fi |
goto | goto label | N/A | N/A | N/A |
switch/case | switch(“$value”) case pattern1: commands breaksw case pattern2: commands breaksw default: commands breaksw endsw | case “$value” in pattern1) commands ;; pattern2) commands ;; *) commands ;; esac | case “$value” in pattern1) commands ;; pattern2) commands ;; *) commands ;; esac | case “$value” in pattern1) commands ;; pattern2) commands ;; *) commands ;; esac |
while | while(expression) commands end | while command do command done | while command do command done | while command do command done |
for/foreach | foreach var(list) commands end | for var in list do commands done | for var in list do commands done | for var in list do commands done |
until
| N/A | until command do commands done | until command do commands done | until command do commands done |
repeat | repeat 3 “echo hello” hello hello hello | N/A | N/A | N/A |
select | N/A | N/A | select var in list do commands done | select var in list do commands done |
函数 | ||||
| N/A | fun() {commands ;} fun fun parm1 param2…. | function fun() {commands ;} function fun{commands;} fun fun parm1 param2…. | function fun() {commands ;} function fun{commands;} fun fun parm1 param2…. |
(待续) |
|
|
|
|
1.4 shell初始文件
1.4.1 B shell
B shell只适用一个初始化执行文件,为.profile,一般位于用户的home目录下,用来设置用户的环境,当用户注册或在命令行
启动B shell时,即读.profile文件,一般通过它来设置用户查找路径和其他环境变量。
1.4.2 C shell
C shell则不同于B shell,它使用两个初始化文件,用于设置用户的环境变量,为:.login和.cshrc,位于home目录
登录时,C shell读取.login;启动C shell时,只读.cshrc文件。
1.4.3 Korn shell
//TODO