您的位置:首页 > 运维架构 > Shell

UNIX shell 学习笔记 一 : 几个shell的规则语法对比

2013-12-31 01:06 435 查看
1. 查看系统有哪些可用的shell

  cat /etc/shell

2. 每种shell都有一个特殊内置变量来存上一条命令的退出状态,例:

  C/TC shell  $status

  % cp fx fy

  % echo $status # 显示cp的退出状态,0成功,1失败

  Bourne,Bash,Korn Shells  $?

  $ cp fx fy

  $ echo $? # 显示cp的退出状态,0成功,1失败

C shell, TC shell编程语法与结构

  1. shbang行  

    是脚本第一行,通知内核使用哪种shell解释脚本,如

C /TCshellkorn shellBourne shellbash
#!/bin/csh #!/bin/ksh#!/bin/sh#!/bin/bash
  2. 注释 符号# 注释其后的一行

  3. 通配符

    特殊字符,被称作是shell的元字符或通配符,如*、?、[]用于文件名扩展, <、>、>>、<&、|用于重定向和管道,如果要直接使用这些字符必须用反斜杠或引号进行引用,如

      echo "how old are you?" # C, TC , Bourne, korn, bash shell

      echo ok \! #C shell, TC shell

  4. 显示输出 echo命令,korn还提供了内置命令print

  5. 局部变量

C /TCshellkorn shellBourne shellbash
set variable = value variable = value

typeset variable=value

variable=valuevariable=value

declare var=val

  

  6. 全局变量

C/TC shellkorn shellBourne shellbash
setenv VARIABLE value export VARIABLE =value VAR=value

export VAR

export VARIABLE =value

declare -x VAR=value

  

  7. 提出变量值

    用符号$, 如 $variable

  8. 读取用户输入

C\TC shellBourne\Bash shellkorn
符号或命令$<read命令

  从用户输入中读取一行并将它赋给该命令右侧

的一个或多个变量

execho "input number .. "
set num = $<

说明:读取用户输入并给变量赋值

echo "what is your name?"

read name

read n1 n2

read name?"your name?"

print -n "your name?"

read name

read n1 n2

  

  9. 参数

C\TC shellBourne\Bash\korn shell
符号或命令scriptname arg1 arg2 scriptname arg1 arg2

ex$1 表示第一个参数arg1

$* 表示所有参数

$argv[1]第一个参数

$argv[*]所有参数

$#argv 表示参数个数。

$1 第一个参数

$* 所有位置参数

$# 参数个数

  

  10. 数组

C,TC shellBourne shell
define数组是一系列的词表,由()括起来,空格隔开,下标索引从1开始,

内置命令shift将数组中左边第一个词移开

set内置命令后跟一系列词,每个词可通过位置来访问,

最多允许使用9个位置。下标索引从1开始,内置命令

shift将数组中左边第一个词移开

exset word_list = (a b c)

shift word_list # remove 'a' from word_list

echo $word_list[1] # display 'b'

echo $word_list or echo $word_list[*] # display all elements of word_list

set a b c

echo $1 #显示第一个参数 'a'

shift # remove 'a' from word_list

$* 所有位置参数

korn shell:
  位置参数创建的数组元素下标从1开始,set -A创建的数组元素下标从0开始,如
    set apples pears peaches      #位置参数
    print $1 $2 $3   # $1 is apples
    set -A arr w1 w2 w3 ...    #数组
    print ${arr[0]}    #打印w1
  Bash:
  与korn shell类似,如
    set apples pears peaches      #位置参数
    print $1 $2 $3   # $1 is apples
    declare -a arr=( w1 w2 w3 ...  )  #数组
   echo ${arr[0]}    #打印w1


  11. 命令替换

    反引号`可以将其包含的字符解释成命令,如

      set now = "today is `date`" # date将作为命令执行并将结果插入到原处

    korn\bash shell 还除了可用反引号替换命令,还支持另一种语法,如

      today = " today is `date`"

      today = "today is $(date)"

  12. 算术运算

C,TC shellBourne shell
define保存算术运算结果的变量必须以一个@符号加一个空格开头

不支持算术运算,通过linux/unix命令计算

ex@ nvalue = 4+1

n=`expr 5+5`

echo $n

  

  korn shell:

    typeset -i var #声明整数

    num=5+5 #声明整数后可进行计算

    ((m = 4+4)) #(())语法表示算术运算,即let命令

  bash shell:

    typeset -i var #声明整数,与ksh兼容

    declare -i var

    num=5+5 #声明整数后可进行计算

    ((m = 4+4)) #(())语法表示算术运算,即let命令

  13. 运算符

C/TC /korn/bash shellBourne shell
等式运算符==

(korn: '=' for str,'=='for numric)

=字符串

-eq 数字

等式判断
!=!= 字符串

-ne 数字

不等
关系运算符>-gt大于
>=-ge大于等于
<-lt小于
<=-le小于等于
逻辑运算符&&-a
||-o
!!
  

14. 条件运算

  C/TC shell:

korn\bash shell:
while exp # exp 可选形式 cmd, [[str expr]], ((num expr))
do
//todo
done

until exp  #exp 可选形式 cmd, [[str expr]], ((num expr))
do
//todo
done

for name in tom dic harry  #traverse list and remove the first item
do
//todo
done

select var in wordlist
do
//todo
done


View Code
16. 函数定义

  korn shell:

#korn shell
func() {                #from bourne shell
//todo
}
---------------------------------------
function func {       #korn version
//todo
echo current directory is  `pwd`
}


17. 文件测试  

C/TC shellBourne shell

Kornshell

-r 当前用户可以读该文件

-w 当前用户可写该文件

-x 当前用户可执行该文件

-e 文件存在

-o 该文件属于当前用户

-z 该文件长度为0

-d 该文件是一个目录  

-f 该文件是一个普通文件

-r 当前用户可以读该文件

-w 当前用户可写该文件

-x 当前用户可执行该文件

-s 该文件大小非0

-d 该文件是一个目录  

-f 该文件是一个普通文件

-d 该文件是一个目录

-r 当前用户可以读该文件

-a 该文件存在且不是目录

-s 该文件大小非0

-w 当前用户可写该文件

-x 当前用户可执行该文件

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: