tcsh shell 基础知识

1、Shell 简介
  Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。Shell 编程跟 JavaScript、php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。Linux 的 Shell 种类众多,常见的有:Bourne Shell(/usr/bin/sh或/bin/sh)、Bourne Again Shell(/bin/bash)、C Shell(/usr/bin/csh)、K Shell(/usr/bin/ksh)Shell for Root(/sbin/sh)。

  执行shell脚本方法:1、source text.sh;2、./test.sh(选择默认指定shell解释器)

2、基本语法
  2.1 操作符

    2.1.1、变量

      字符串变量:set var = hello; 格式:set 空格 变量名 = 字符串;

      数值变量:@ num = 1; 格式:@ 空格 变量名 = 数值;

    2.1.2、传递参数

      获取参数的格式为:$n,n为执行脚本的参数顺序。

      例如:source test.sh m 0, $0=tcsh, $1=m, $2=0;

    2.1.3、数组

      定义数组myarr, 通过$myarr[index]来访问数组中的值,注意index是从1开始的。通过$myarr或$myarr[*]来访问数组所有的元素。通过$#myarr来查看元素的个数。

      例如:set myarr = (str1, str2,str3), $myarr[2] = str2; $#myarr = 3;

    2.1.4、echo

      Shell 的 echo 指令用于字符串的输出。命令格式:echo string;

      例如:echo "hello word"; 输出:hello word

    2.1.5、命令替换

      通过set x = `cmd`来执行命令,且结果赋值给变量。

      例如:set d = `date`;先执行date获取系统时间,然后将时间赋值给d。

  2.2、控制结构

    2.2.1、条件控制结构

      1、if 条件判断

        if (expression) then 注:如果expression为真,则执行commands
          commands
        endif
        if (expression) then 注:如果expression为真,则执行command1,否则执行
          command1
        else
          command2
        endif

      2、switch条件选择

        switch (string) 加注:允许在几条替换命令中选择,string为不同的模式
          case pattern:
            commands
          breadsw
          default:
            commands
          endsw

    2.2.2、循环控制结构

      1、foreach 循环

        foreach var (wordlist)
          commands
        end

      2、repeat 循环

        repeat num "commands"

      3、while循环

        while(expression)
          commands
          continue

3、常用linux指令
  3.1、pwd

    pwd:获取当前工作路径。

    例如:set basedir = `pwd`; #basedir为当前工作path

  3.2、cat

    cat:read 文件每一行内容。

    例如set user_name = `cat temp.v`,temp.v第一行为user_name。如果temp.v有多行信息,则分别读取user_name[0],user_name[1]... ...user_name[n]。

  3.3、>

    >:内容写入

    例如:echo "hello word" > temp.v; 即将hello word字符写入temp.v。

  3.4、sed

    sed:命令是利用脚本来处理、编辑文本文件。

    格式:1、sed -i "s/a/b/g" temp.v;将temp.v中的a替换为b,其中a可以包含特殊字符,例如?、#、!,但是不能存在 “/”;

    2、sed -i "s/a/b/g" temp.v;功能与1相同,但是不能识别特殊字符。

  3.5、tr

    tr:translate的简写,主要用于压缩重复字符,删除文件中的控制字符以及进行字符转换操作。

    -s 替换重复的字符

       -s: squeeze-repeats,用SET1指定的字符来替换对应的重复字符 (replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character)

      例子:删除重复字符:echo "aaabbbaacccfddd" | tr -s ;输出:abacfd

      删除空格: cat b.txt | tr -s ["\n"] I like football Football is very fun! Hello

    -d 删除字符

        -d:delete,删除SET1中指定的所有字符,不转换(delete characters in SET1, do not translate

      例子:删除字母:echo "a12HJ13fdaADff" | tr -d "[a-z][A-Z]";输出:1213

    -t 字符替换

      -t:truncate,将SET1中字符用SET2对应位置的字符进行替换,一般缺省为-t

      例子:echo "a1213fdasf" | tr -t [afd] [AFO];输出:A1213FOAsF

    -c 字符补集替换

       -c:complement,用SET2替换SET1中没有包含的字符

      例子:[root@localhost ~]# cat a.txt

      Monday 09:00 Tuesday 09:10 Wednesday 10:11 Thursday 11:30 Friday 08:00 Saturday 07:40 Sunday 10:00

      [root@localhost ~]# cat a.txt | tr -c "[a-z][A-Z]" "#" | tr -s "#" | tr -t "#" "\n"

      Monday Tuesday Wednesday Thursday Friday Saturday Sunday

      解析:tr -c "[a-z][A-Z]" "#":替换09:10为“#”

      tr -s "#":删除重复的“#”

      tr -t "#" "\n":字符替换为空格

  3.5、LINUX中rm、source、alias、gvim、mkdir等均可正常使用

4、实例展示
  4.1、字母大小写转换

    set ver = $1;

    set mode = $2; #mode=0,大写转小写;mode=1,小写转大写

    if ($mode == 1) then

      echo $var | tr -t a-z A-Z ;

    else

      echo $var | tr -t A-Z a-z ;

    endif

    例如:source test.sh ABC 0, 输出abc; source test.sh abc 1, 输出ABC。

  4.2简易计算器

    set in0 = $1;

    set in1 = $2;

    set sign = $3;

    switch($sign)

      case +:

        @ c = $in0 + $in1;

      breaksw

      case -:

        @ c = $in0 - $in1;

      breaksw

      default:

        @ c = 0;

      breaksw

    endsw

    echo “$c”

    例如:source test.sh 1 2 +, 输出3;source test.sh 1 2 1, 输出-1。

  4.3、文本处理

    功能简述:新建文件、将当前工作path写入文件,删除文本中数字、打印输出文件内容。

    mkdir test ; #新建文件test

    set basedir = `pwd` ; #获取工作path

    echo $basedir | tr -d "[0-9]" > ./test/test.v #删除path中数字

    set print_addr = `cat ./test/test.v` ; #read test.v中的内容

    echo "$print_addr" #打印输出

    例如:source test.sh user/yy01, 输出:user/yy。

5、总结
  shell脚本适合自动化指令集合,将linux上分布执行的命令进行自动化管理。shell语法简单,但是不同的解析器基本语法各不相同,市面上大多bash语法,当初写tcsh费了很多时间,今日总结,不仅检测自己的学习成果,更希望能够帮助大家少走弯路,程序亲测有效,欢迎与大家一起讨论学习,快点加入吧~~~~~~

posted @ 2021-11-27 23:49  威龙王子  阅读(1083)  评论(1)    收藏  举报