语法:csh语法速查
差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
语法:csh语法速查 [2024/01/03 09:45] – 移除 - 外部编辑 (Unknown date) 127.0.0.1 | 语法:csh语法速查 [2024/09/03 10:29] (当前版本) – [9. 判断文件类型] user01 | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== csh语法速查 ====== | ||
+ | |||
+ | |||
+ | |||
+ | 参考: | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | |||
+ | cshell脚本没有函数的概念,就可以把它当做是批处理脚本。 | ||
+ | |||
+ | 它的好处是在命令行输入的命令都可以直接放到脚本里面去执行(不用转换),这个对于批处理来说确实挺有好处的。 | ||
+ | |||
+ | |||
+ | ===== - csh脚本执行方式 ===== | ||
+ | <code bash> | ||
+ | # 文件名执行方式 | ||
+ | ./ | ||
+ | / | ||
+ | |||
+ | # source xxx脚本执行方式 | ||
+ | source ./ | ||
+ | |||
+ | #用source xxx脚本的好处是: 在shell环境中定义的alias语句,可以直接在脚本文件中使用; 在脚本文件里面定义的alias,同样在shell环境中也可以使用。 | ||
+ | # | ||
+ | |||
+ | # | ||
+ | # -- 好处是不依赖于当前shell环境下人为设置的变量或alias,可能不太容易出错。 | ||
+ | |||
+ | # | ||
+ | |||
+ | # 不管是用那种方式执行脚本,都是建议在脚本文件的第一行加上 | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== - csh脚本举例 ===== | ||
+ | ==== - 例子1 ==== | ||
+ | |||
+ | 假设有脚本文件a.csh内容如下: | ||
+ | <code bash> | ||
+ | #!/bin/csh | ||
+ | |||
+ | set a = ${#argv} | ||
+ | |||
+ | echo "argv number = $a" | ||
+ | |||
+ | echo $0 $1 $2 $3; # 显示脚本名和3个输入参数 | ||
+ | echo "argv: $argv[1] $argv[2] $argv[3]"; | ||
+ | |||
+ | echo ${0} # 显示脚本名 | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | 执行: | ||
+ | ./a.csh 1 2 3 | ||
+ | 运行结果: | ||
+ | argv number = 3 | ||
+ | ./a.csh 1 2 3 | ||
+ | argv: 1 2 3 | ||
+ | ./a.csh | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== - 例子2 ==== | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/csh | ||
+ | |||
+ | set files = ~/*; # 用户home目录下的所有文件和目录 | ||
+ | foreach tmp ($files) | ||
+ | if (-f $tmp) then | ||
+ | set bname = `basename $tmp` | ||
+ | echo " | ||
+ | else if(-d $tmp) then | ||
+ | echo "dir: $tmp, basename = $bname" | ||
+ | endif | ||
+ | sleep 1; #等待1s | ||
+ | sleep 1; | ||
+ | end | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== - 变量 ===== | ||
+ | <code bash> | ||
+ | set x = 5 | ||
+ | echo $x; #输出5 | ||
+ | echo ${x}; #输出5 | ||
+ | echo ${x}kg; # | ||
+ | echo $%x; # | ||
+ | |||
+ | $# | ||
+ | ${#argv}, argv参数个数 | ||
+ | $# | ||
+ | $argv[1], | ||
+ | $argv[0], argv参数0, 是指脚本的名字 | ||
+ | |||
+ | |||
+ | set i = 0 | ||
+ | @ i = $i + 1 # 使用算术命令 | ||
+ | @ i = $i - 1 # 使用算术命令 | ||
+ | @ i = $i * 2 # 使用算术命令 | ||
+ | @ i = $i / 2 # 使用算术命令 | ||
+ | # 类似c++的自加减乘除操作 | ||
+ | @ i++ # 自加操作 | ||
+ | @ i-- # 自减操作 | ||
+ | @ i+=2 # 自加2 | ||
+ | @ i-=2 # 自减2 | ||
+ | @ i*=2 # 自乘2 | ||
+ | echo $i | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ==== - 系统变量 ==== | ||
+ | * argv, 脚本变量参数。 $0表示shell名,$1会被替换为$argv[1] | ||
+ | * cwd, 当前工作目录 | ||
+ | * path,path变量 | ||
+ | * home, 等同于'' | ||
+ | * prompt, 命令行提示符变量 | ||
+ | |||
+ | ==== - cshell prompt ==== | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | <code csh> | ||
+ | |||
+ | prompt | ||
+ | include any of the following formatting sequences (+), which are replaced by the given information: | ||
+ | |||
+ | %/ The current working directory. | ||
+ | %? The current working directory, but with one’s home directory represented by ‘?’ and other users’ home directories represented by ‘?user’ as per Filename substitution. ‘?user’ substitution happens only if the shell has already used ‘?user’ inapathname in the current session. | ||
+ | |||
+ | %c[[0]n], %.[[0]n] | ||
+ | |||
+ | %C Like %c, but without ‘?’ substitution. | ||
+ | %h, %!, ! The current history event number. | ||
+ | %M The full hostname. | ||
+ | %m The hostname up to the first ‘.’. | ||
+ | %S (%s) Start (stop) standout mode. | ||
+ | %B (%b) Start (stop) boldfacing mode. | ||
+ | %U (%u) Start (stop) underline mode. | ||
+ | %t, %@ The time of day in 12-hour AM/PM format. | ||
+ | %T Like ‘%t’, but in 24-hour format (but see the ampm shell variable). | ||
+ | %p The ‘precise’ time of day in 12-hour AM/PM format, with seconds. | ||
+ | %P Like ‘%p’, but in 24-hour format (but see the ampm shell variable). | ||
+ | \c c is parsed as in bindkey. | ||
+ | ?c c is parsed as in bindkey. | ||
+ | %% A single ‘%’. | ||
+ | %n The user name. | ||
+ | %d The weekday in ‘Day’ format. | ||
+ | %D The day in ‘dd’ format. | ||
+ | %w The month in ‘Mon’ format. | ||
+ | %W The month in ‘mm’ format. | ||
+ | %y The year in ‘yy’ format. | ||
+ | %Y The year in ‘yyyy’ format. | ||
+ | %l The shell’s tty. | ||
+ | %L | ||
+ | %$ | ||
+ | %# | ||
+ | %{string%} | ||
+ | Includes string as a literal escape sequence. It should be used only to change terminal | ||
+ | attributes and should not move the cursor location. This cannot be the last sequence in | ||
+ | prompt. | ||
+ | %? The return code of the command executed just before the prompt. | ||
+ | %R In prompt2, the status of the parser. In prompt3, the corrected string. In history, the history string. | ||
+ | |||
+ | |||
+ | The bold, standout and underline sequences are often used to distinguish a superuser shell. For | ||
+ | example, | ||
+ | > set prompt = "%m [%h] %B[%@]%b [%/] you rang? " | ||
+ | tut [37] [2:54pm] [/ | ||
+ | |||
+ | If ‘%t’, ‘%@’, ‘%T’, ‘%p’, or ‘%P’ is used, and noding is not set, then print ‘DING!’ on the change of hour (i.e, ‘:00’ minutes) instead of the actual time. | ||
+ | |||
+ | Set by default to ‘%# ’ in interactive shells. | ||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ | ==== - 通过perl处理变量 ==== | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | set a = " | ||
+ | set a = `echo $a | perl -pe " | ||
+ | |||
+ | echo $a | ||
+ | # | ||
+ | |||
+ | |||
+ | set cl = " | ||
+ | set cl_type = `echo $cl | perl -e '$_ = <>; if (/^$/) { print " | ||
+ | echo $cl_type | ||
+ | # cl_type当前为num | ||
+ | |||
+ | |||
+ | </ | ||
+ | ===== - 数组 ===== | ||
+ | |||
+ | <code bash> | ||
+ | # | ||
+ | set arr = () | ||
+ | |||
+ | # 初始化,带有3个元素的数组 | ||
+ | set myarr = (str1, str2,str3) | ||
+ | echo $myarr[2] | ||
+ | echo $myarr | ||
+ | echo $myarr[*] | ||
+ | |||
+ | set files = * | ||
+ | </ | ||
+ | |||
+ | |||
+ | 追加元素 | ||
+ | |||
+ | <code bash> | ||
+ | set myarr = ($myarr str4) | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | shift操作 | ||
+ | |||
+ | <code bash> | ||
+ | # array test | ||
+ | set ar = (0 1 2 3 4) | ||
+ | echo $#ar | ||
+ | echo ${#ar} | ||
+ | echo $ar[1] | ||
+ | echo $ar[1-3] | ||
+ | echo $ar[*] | ||
+ | shift ar | ||
+ | echo $ar[*] | ||
+ | </ | ||
+ | |||
+ | 执行结果: | ||
+ | < | ||
+ | 5 | ||
+ | 5 | ||
+ | 0 | ||
+ | 0 1 2 | ||
+ | 0 1 2 3 4 | ||
+ | 1 2 3 4 | ||
+ | </ | ||
+ | ===== - if/ | ||
+ | |||
+ | <code bash> | ||
+ | if ($n < 5 || 20 <= $n) then | ||
+ | endif | ||
+ | |||
+ | if ($n < 5 && 20 <= $n) then | ||
+ | endif | ||
+ | |||
+ | if(expression)then | ||
+ | commands | ||
+ | endif | ||
+ | |||
+ | if {(command)} then | ||
+ | commands | ||
+ | endif | ||
+ | |||
+ | |||
+ | if(expression) then | ||
+ | commands | ||
+ | else if(expression) then | ||
+ | commands | ||
+ | else | ||
+ | commands | ||
+ | endif | ||
+ | </ | ||
+ | |||
+ | ===== - 判断上一条命令退出值 ===== | ||
+ | <code bash> | ||
+ | echo " | ||
+ | if($? != 0) then | ||
+ | echo "run failed 1" | ||
+ | exit 1 | ||
+ | endif | ||
+ | |||
+ | rm " | ||
+ | if($? != 0) then | ||
+ | echo "run failed 2" | ||
+ | exit 1 | ||
+ | endif | ||
+ | </ | ||
+ | ===== - switch/case ===== | ||
+ | <code bash> | ||
+ | switch(" | ||
+ | case pattern1: | ||
+ | commands | ||
+ | breaksw | ||
+ | case pattern2: | ||
+ | commands | ||
+ | breaksw | ||
+ | default: | ||
+ | commands | ||
+ | breaksw | ||
+ | endsw | ||
+ | </ | ||
+ | |||
+ | ===== - while/ | ||
+ | |||
+ | <code bash> | ||
+ | while(expression) | ||
+ | # 类似c++的自加减乘除操作 | ||
+ | @ i++ # 自加操作 | ||
+ | @ i-- # 自减操作 | ||
+ | @ i+=2 # 自加2 | ||
+ | @ i-=2 # 自减2 | ||
+ | @ i*=2 # 自乘2 | ||
+ | |||
+ | commands | ||
+ | continue | ||
+ | break | ||
+ | |||
+ | end | ||
+ | |||
+ | set wordlist = (a b c) | ||
+ | foreach var ($wordlist) | ||
+ | echo $var | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== - 判断文件类型 ===== | ||
+ | |||
+ | 注意: 在使用类似-e -f的时候,后面建议跟普通路径名,而不是带通配的那种路径名。 | ||
+ | <code bash> | ||
+ | #!/bin/tcsh | ||
+ | |||
+ | # 比如建议使用 | ||
+ | if (! -e ./abc/abc1) then | ||
+ | endif | ||
+ | |||
+ | # 不建议使用如下的方式, | ||
+ | if (! -e ./*/abc1) then | ||
+ | endif | ||
+ | |||
+ | |||
+ | -f 和-e的区别 | ||
+ | Conditional Logic on Files | ||
+ | |||
+ | -a file exists. | ||
+ | -b file exists and is a block special file. | ||
+ | -c file exists and is a character special file. | ||
+ | -d file exists and is a directory. | ||
+ | -e file exists (just the same as -a). | ||
+ | -f file exists and is a regular file. | ||
+ | -g file exists and has its setgid(2) bit set. | ||
+ | -G file exists and has the same group ID as this process. | ||
+ | -k file exists and has its sticky bit set. | ||
+ | -L file exists and is a symbolic link. | ||
+ | -n string length is not zero. | ||
+ | -o Named option is set on. | ||
+ | -O file exists and is owned by the user ID of this process. | ||
+ | -p file exists and is a first in, first out (FIFO) special file or | ||
+ | named pipe. | ||
+ | -r file exists and is readable by the current process. | ||
+ | -s file exists and has a size greater than zero. | ||
+ | -S file exists and is a socket. | ||
+ | -t file descriptor number fildes is open and associated with a | ||
+ | terminal device. | ||
+ | -u file exists and has its setuid(2) bit set. | ||
+ | -w file exists and is writable by the current process. | ||
+ | -x file exists and is executable by the current process. | ||
+ | -z string length is zero. | ||
+ | |||
+ | 是用 -s 还是用 -f 这个区别是很大的! | ||
+ | |||
+ | </ | ||