[工具使用:shell脚本] c shell 学习记录(csh)

基础总结


1)定义变量


1、通过set来定义局部变量x,通过x 或 者 x或者x或者{x}来使用,%x 表示变量的值的长度,?x来判断变量x是否设置,如果设置则为1,否则为0

set x = 5
echo $x
echo ${x}kg
echo $%x 
  • 全局变量的定义setenv v value 该变量将被此shell派生的所有子shell继承。

  • $$表示当前进程的PID, $status 或 $? 表示退出状态。

2)定义数组


定义数组myarr, 通过myarr[index] 来访 问 数 组 中 的 值 , 注 意 index 是 从 1 开 始 的 。 通 过 myarr[index]来访问数组中的值,注意index是从1开始的。通过myarr[index]来访问数组中的值,注意index是从1开始的。通过myarr或myarr[ ∗ ] 来 访 问 数 组 所 有 的 元 素 。 通 过 myarr[*]来访问数组所有的元素。通过myarr[∗]来访问数组所有的元素。通过#myarr来查看元素的个数。
()中加空格,索引myarr[2]的时候就是一个,不然就是两个

set myarr = (str1, str2,str3)
echo $myarr[2]
echo $myarr
echo $myarr[*]

3)命令替换

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

set d = `date`
echo $d
echo $d[6]-$d[2]-$d[3]

4)命令行参数


通过argv[1] , argv[1],argv[1],argv[2]或$1,2 来 访 问 命 令 行 参 数 。 命 令 行 参 数 的 个 数 为 2来访问命令行参数。命令行参数的个数为2来访问命令行参数。命令行参数的个数为#argv。
 

5)文件名扩展的元字符

只能使用?,*,[abc],[a-c]。

6)IO重定向和管道


将命令的输出重定向到文件为>。
将命令的输出重定向并追加到文件为>>。
将命令的输入重定向到文件为<。
将命令的报错信息重定向到一个文件(cmd>/dev/tty)>&errors。
将命令的输出和错误输出分别重定向(cmd > goodstuff) >& badstuff。

将命令的输出和报错信息重定向到一个文件cmd>&file。
将命令的输出经管道发往另一个命令cmd|cmd.
将命令的输出和报错信息经管道发往另一个命令cmd|&cmd。
条件语句为 cmd && cmd 或 cmd || cmd。
command<<WORD 表示将command的输入重定向为从第一个WORD处开始,到下一个WORD处之间的内容(即here文档)。
 

7)从键盘读取并保存到变量中

set var = $<

8)算术

@ var = 5 + 5
echo $var
@ v2 = $var + 3
echo $v2

9) label 和 goto

csh中没有函数的概念,使用类似windows批处理中的label和goto。

goto label
......
label:
....

10) if/else/switch/case

if(expression)then
 commands
endif
 

if {(command)} then
 commands
endif
 

if(expression) then
 commands
else if(expression) then
 commands
else
 commands
endif
 

switch("$value")
case pattern1:
 commands
 breaksw
case pattern2:
 commands
 breaksw
default:
 commands
 breaksw
endsw

11)while/foreach

while(expression)
 commands
 continue
 break
end

foreach var (wordlist)
 commands
end

12)repeat

repeat表示重复执行后面的命令。

repeat 3 echo "helloworld"

13)csh中设置环境变量PATH的方法

csh中使用path代替PATH,设置类似于数组的使用。

set path = ($path /home)
echo $path
echo $PATH

14)用`可以将一条命令包裹起来

set ret=`pwd` 

echo ${ret}

条件语句

cshell 命令语法

1.if / then / else

   if (expr) then 
        commands 
     else if (expr2) then 
        commands 
     else 
        commands 
     endif
     
     Example:

     #!/bin/csh
     if ($#argv == 0) then  
        echo "No number to classify"  
     else if ($#argv > 0) then  
        set number = $argv[1]  
        if ($number < 0) then  
           @ class = 0  
        else if (0 <= $number && $number < 100) then  
           @ class = 1  
        else if (100 <= $number && $number < 200) then  
           @ class = 2  
        else  
           @ class = 3  
        endif
        echo The number $number is in class $class
     endif  
     

2.foreach / end

Syntax:

    foreach name (wordlist)
        commands
    end
    

Example:

    #!/bin/csh
    foreach color (red orange yellow green blue)
       echo $color
    end

3.while / end

Syntax:

    while (expression)
        commands
    end
    

Example:

    #!/bin/csh
    set word = "anything"
    while ($word != "")
      echo -n "Enter a word to check (Return to exit): "
      set word = $<
      if ($word != "") grep $word /usr/share/dict/words
    end

4.break

Syntax:

    break
    

Example:

    #!/bin/csh
    foreach number (one two three exit four)
      if ($number == exit) then
        echo reached an exit
        break
      endif
      echo $number
    end

Syntax:

    continue
    

Example:

    #!/bin/csh
    foreach number (one two three exit four)
      if ($number == exit) then
        echo reached an exit
        continue
      endif
      echo $number
    end
    

5.goto

Goto 语句将控制权转移到以 label 开头的语句:

  • Syntax:

    goto label
    

Example:

    #!/bin/csh
    if ($#argv != 1) goto error1
    if ($argv[1] < 6) goto error2
    goto OK
  
    error1:
      echo "Invalid - wrong number or no arguments"
      echo "Quitting"
      exit 1
  
    error2:
      echo "Invalid argument - must be greater than 5"
      echo "Quitting"
      exit 1
  
    OK:
      echo "Argument = $argv[1]"
      exit 1

6.单引号与双引号表示不同的作用

单引号:

  • 允许包含空格,

  • 防止变量替换,

  • 允许生成文件名

双引号:

  • 允许包含空格

  • 允许变量替换

  • 允许生成文件名

    #!/bin/csh
    set opt=-l
    set x1='ls $opt'
    echo $x1
    set x2="ls $opt"
    echo $x2
  
Will produce the output:
     ls $opt
     ls -l
     

7.存储命令的输出

  • 通过反引号(ESC下面那个)进行特殊命令的存储,并且通过索引进行调用

  • Syntax:

    set variable = `command` 
    

Example:

    #!/bin/csh
    set date_fields=`date`
    echo $date_fields
    echo $date_fields[1]
    echo $date_fields[2]
    foreach field(`date`)
      echo $field
    end
  
Sample output:
     Thu Mar 9 22:25:45 HST 1995
     Thu
     Mar
     Thu
     Mar
     9
     22:25:45
     HST
     1995

8.读取用户输入

 set variable = $<
          - or -
 set variable = `head -1`

然后就需要用户自己在命令行输入对应的数组

#!/bin/csh
    echo -n Input your value: 
    set input = $<
    echo You entered: $input
  
            - or -
  
 #!/bin/csh
    echo -n Input your value: 
    set input = `head -1`
    echo You entered: $input
    

简单个例

设置一个数组名为ages,赋予变量(0 0 0 0 0)

$ set ages = (0 0 0 0 0) 

将ages中的第二个数值定义为15

$ @ ages[2] = 15

将ages中的第三个数值定义为第二个数值加上4

 $ @ ages[3] = ( $ages[2] + 4 ) 

输出这个数组第三个数值

$ echo $ages[3] 

输出数组的所有结果

$ echo $ages

c shell 循环语句:If语句

If Example: 
#!/bin/csh -f 
# Set class depending on argument value 
set number = $argv[1] 
if ($number < 0) then 
	@ class = 0 else 
if ($number >= 0 && $number < 100) then 
	@ class = 1 
else if ($number >= 100 && $number < 200) then 
	@ class = 2 
else 
	@ class = 3 
endif 
echo The number $number is in class $class 

c shell 循环语句:switch语句

#!/bin/csh -f 
switch (string variable) 
	case pattern: 
		commands
		breaksw
	case pattern: 
		commands
		breaksw
	:
	default:
		commands
		breaksw
endsw

c shell 循环语句:while语句

@ limit = $argv[1] 

@ index = 1 

@ sum = 0 

while ($index <= $limit) 

	@ sum += $index 

	@ index++ 

end 

echo The sum is $sum  

定义一个变量,使用命令:

set var1=a3 		#sets var1's value to a3.
set var2=(a b c) 	# sets the array variable var2 to a b, and c.

通过在变量名前面键入美元符号($)来使用变量。如果变量是一个数组,则可以使用括号[]指定下标,并且可以使用 $# var2格式获取元素的数量

要分配计算值,使用@命令如下:

@var = $a + $x * $z

对比bash&cshell

Bourne shell

#!/bin/sh
i=2
j=1
while [ $j -le 10 ]
do
   echo '2 **' $j = $i
   i=`expr $i '*' 2`
   j=`expr $j + 1`
done
#!/bin/sh
if [ $days -gt 365 ]
then
   echo This is over a year.
fi
#!/bin/sh
for i in d*
do
   case $i in
      d?) echo $i is short ;;
      *) echo $i is long ;;
   esac
done

C shell

#!/bin/csh
set i = 2
set j = 1
while ( $j <= 10 )
   echo '2 **' $j = $i
   @ i *= 2
   @ j++
end
#!/bin/csh
if ( $days > 365 ) then
   echo This is over a year.
endif
#!/bin/csh
foreach i ( d* )
   switch ( $i )
      case d?:
         echo $i is short
         breaksw
      default:
         echo $i is long
   endsw
end

实际使用过程中的记录

1. 重定向到某个文件(不在终端输出)
cmd >& directory/file

2. 删除
cmd >& /dev/null

3. 在脚本1中,为了实现一定的并行
可能会存在如下代码:
cmd1 &
cmd2 &
cmd3 &

然后,想调用另外一个脚本2,在另外一个脚本2(比如下面命令行中的get_some_status)中,想每隔一段时间等获取一下运行状态,
脚本2中可能会存在如下代码:
while (some_conditions)
  (cmd11; cmd12; cmd13) &
  (cmd21; cmd22; cmd23) &
  wait
  sleep ${xxx}s
end
那么,在脚本1中,可以用如下方式调用脚本2(可以认为新开一个terminal去执行脚本2,如果不新开不执行,那么脚本2中的wait会等待所有上面 & 的进程执行完成,与预期不符(想只wait脚本2中 & 的进程))

gnome-terminal -x csh -c "source ${status_dir}/get_some_status ; exec csh"

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值