cshrc的基本语法介绍

set/unset 变量

  • 通过set来定义局部变量x,

  • 通过$x或${x}来使用变量x的值

  • $%x表示变量的值的长度,

  • $?x来判断变量x是否设置,如设置则为1,否则为0。

  • 全局变量的定义setenv v value 该变量将被此shell派生的所有子shell继承。

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


set x = 5

echo $x

echo ${x}kg

echo $%x   

数组

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

echo $myarr[2]

echo $myarr

echo $myarr[*]

命令替换

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

set d = `date`

echo $d

echo $d[6]-$d[2]-$d[3]

命令行参数

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

代字符号扩展

  • 文件名扩展的元字符: 只能使用?,*,[abc],[a-c]。
  • 代字符号扩展: ~username 表示username的home目录

别名

  • alias m more 为more创建别名m,有的alias grepc="grep -r "
  • alias 列出所有的alias。
  • unalias m 用来删除more的alias定义。

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

while/foreach

while(expression)

  commands

  continue
  break
end

foreach var (wordlist)

  commands

end

repeat


repeat 3 "echo helloworld"

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



set path = ($path /home)

echo $path

echo $PATH 

posted @ 2022-03-25 13:56  Thisway2014  阅读(2278)  评论(0编辑  收藏  举报