cshell script的例子

这篇博客介绍了cshell脚本的常用功能,包括echo用法、从键盘输入参数、switch-case结构、if条件语句、循环操作、文件查询以及变量修饰符的使用。通过实例展示了如何获取命令行参数、检查文件状态以及进行简单的算术运算。此外,还提到了重定向输出到文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

echo相关

#!/bin/csh
echo "Hello World!"
echo "How are you today?"

will produce the output

Hello World!
How are you today?

But the script

#!/bin/csh
echo -n "Good morning, " #不换行输出
echo "Beatrice."

produces:

Good morning, Beatrice.

从键盘输入参数

  • 脚本
#!/bin/csh
set uname = $<
echo "Why, Good Morning, $uname!"
echo "Please input your name: "

switch-case

#!/bin/csh
echo -n "Please enter your first name: "
set uname = $<
switch ($uname)
        case [Gg]eorge:
                cat /messages/George
                breaksw
        case [Mm]ary:
                cat /messages/Mary
                breaksw
        case [Ss]andy:
                cat /messages/Sandy
                breaksw
        default:
                cat /messages/Goodbye
                exit 1
endsw

if

 #!/bin/csh
 if ($#argv == 0) echo There are no arguments
 if ($#argv != 0) echo There are $#argv arguments
#!/bin/csh
foreach i (*)
        if (-f $i) then
            echo "$i is a file."
        endif
        if (-d $i) then
             echo "$i is a directory."
        endif
end

循环

  1. foreach
foreach i ( 1 2 3 4 5 6 7 8 9 10 )
    echo $i
end
#!/bin/csh
echo "Setting name servers...."
foreach i ( ns1.cyberciti.biz ns2.cyberciti.biz  ) 
   echo $i 
end

output:

Setting name servers....
ns1.cyberciti.biz
ns2.cyberciti.biz
  1. while
#!/bin/csh
 # demoloop.csh - Sample loop script
 set j = 1
 while ( $j <= 5 )
   echo "Welcome $j times"
   @ j++
 end

outputs:

Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

文件查询相关:

  1. 查询文件是否为空:
#!/bin/csh
foreach dudfile(/home/users1/hansel/*)
    if (-z $dudfile || $dudfile == "core") then
        rm $dudfile
    endif
end
  1. 查询文件是否存在
The C Shell also supports file operators, shown in table 3.
+-----------------------------------------------------------+
|Operator      Meaning					    |
+-----------------------------------------------------------+
|-r filename   Returns true, if the user has read access    |
|-w filename   Returns true, if the user has write access   |
|-x filename   Returns true, if the user has execute access |
|-e filename   Returns true, if the file exists		    |
|-o filename   Returns true, if the user owns the file	    |
|-z filename   Returns true, if the file is empty	    |
|-f filename   Returns true, if the file is a plain file    |
|-d filename   Returns true, if the file is a directory	    |
+-----------------------------------------------------------+
If the file does not exist, or inaccessible, the test returns false.
  1. 判断文件类型:
#!/bin/csh
foreach i (*)
        if (-f $i) then
            echo "$i is a file."
        endif
        if (-d $i) then
             echo "$i is a directory."
        endif
end

outputs:

mycal.pl is a file.
skl is a directory.
x is a file.
x.pl is a file.
y is a file.

获取命令行参数

#!/bin/csh -f
echo The $0 command is called with $#argv parameters #    $#argv为参数的个数
echo   parameter 1 is $1     #第1个参数
echo   parameter 2 is $2     #第2个参数
echo   parameter 3 is $3     #第3个参数
echo   parameter 4 is $4     #第4个参数
echo All Parameters are \"$argv\"  #所有的参数
echo 2nd and on parameters are \"$argv[2-]\"   # 最后的两个参数

then run the command:

./myparams alpha beta omega

This prints out the following:

The ./myparams command is called with 3 parameters
parameter 1 is alpha
parameter 2 is beta
parameter 3 is omega
parameter 4 is
All Parameters are "alpha beta omega"
2nd and on parameters are "beta omega"

If the parameter is missing the value of the position parameter will be a blank string as is the case of the 4th parameter. Also, you can get the entire or part of the command-line using the variable $argv which is a wordlist.
If you want to include spaces in a single parameter you must quote the value. For example running this command:

./myparams “alpha beta” omega

would print out the following:

The ./myparams command is called with 2 parameters
parameter 1 is alpha beta
parameter 2 is omega
parameter 3 is
parameter 4 is
All Parameters are "alpha beta omega"
2nd and on parameters are "omega"

Variable Modifiers

n C shell, the path value obtained from a variable can be modified before it is used into a command or expression. Variable modifiers are given after a colon (😃 at the end of a variable. The meaning of the modifiers are as follows:

:h returns the directory of a path (aka "head")
:t returns the filename of a path (aka "tail")
:r returns the directory and filename without the last extension (aka "root")
:e returns the extension of the path (aka "end")
#!/bin/csh -f
set file = /usr/joe/backup.tar.gz

echo $file:h
echo $file:t
echo $file:r
echo $file:e
echo $file:t:r:r
echo $file:h:h

outputs:

/usr/joe
backup.tar.gz
/usr/joe/backup.tar
gz
backup
/usr

計算
csh, tcsh 中进行加减法时,需要使用到 @ 符号。

@ x = 1 + 2
echo $x

得到的结果是:3

关于重定向:
将stdout和stderr都定向到log.txt文件中:

xxxx > & log.txt

将stdout和stderr都同时定向到屏幕输出和log.txt文件中:

xxxx |& tee log.txt

以上的内容参考(Copy)自:
https://www.cyberciti.biz/faq/shell-script-while-loop-examples/
https://en.wikibooks.org/wiki/C_Shell_Scripting/Parameters
https://en.wikibooks.org/wiki/C_Shell_Scripting
http://www.grymoire.com/Unix/Csh.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值