csh脚本语法实例

所属分类: 脚本专栏 / linux shell 阅读数: 1977
收藏 0 赞 0 分享

csh实例 参考:

复制代码 代码如下:

#!/bin/csh -vx
#csh -vx show the command before running to help debug

#just to check syntax
#csh -n $0

#argv
if ($#argv < 2) then
    echo "Sorry, but you entered too few parameters"
    echo "usage:  $0 arg1 arg2
    exit
endif
set arg1 = $1
set arg2 = #2

foreach i ($*)
   echo $i
end
  
#execute commands
echo "Hello there `whoami`.  How are you today?"
echo "You are currently using `hostname` and the time is `date`"
echo "Your directory is `pwd`"
whoami
hostname
date
pwd

#var
set name = Mark
echo $name
set name = "Mark Meyer" # if the string has space, must use ""
echo $name
# it means set to NULL
set name =
unset name
# get user input
set x = $< 
set current_user = `whoami`

#buildin vars
echo $user      # who am I?
echo $status    # a numeric variable, usually used to retun error codes

#Arithmetic variables
@ i = 2
@ k = ($x - 2) * 4
@ k = $k + 1
@ i--
@ i++

#array
set name = (mark sally kathy tony)
echo $#name    # num of the array
echo $name[1]
echo $name[4]
echo $name[2-3]
echo $name[2-]        # all elements from 2 to the end
echo $name[1-3]
echo $name[$i]
set name = ($name doran)
set name = (doran $name)
set name = ($name[1-2] alfie $name[3-])
shift name  # get rid of the frist element of the array
shift #if no argument is given, it will get rid of argv

#Expressions and operators
==        equal     (either strings or numbers)
!=        not equal     (either strings or numbers)
=~        string match
!~        string mismatch
<=        numerical less than or equal to
>=        numerical greater than or equal to
>         numerical greater than
<         numerical less than

-e file           file merely exists (may be protected from user)
-r file           file exists and is readable by user
-w file           file is writable by user
-x file           file is executable by user
-o file           file is owned by user
-z file           file has size 0
-f file           file is an ordinary file
-d file           file is a directory

!   -- negate                
&&  -- logical and
||  -- logical or

#if-else
# run cmd as if expression
if ({grep -s junk $1}) then 
   echo "We found junk in file $1"
endif
# check if the var is defined
if ($?dirname) then
    ls $dirname
endif

if (-e somefile) then
 grep $1 somefile
else
 echo "Grievous error!  Database file does not exist".
endif

#foreach
foreach i (*)
    if (-f $i) then
        echo "============= $i ==================="
        head $i
    endif
    if (-d $i) then
        (cd $i; headers)
    endif
end

#while
while ($#argv > 0)
    grep $something $argv[1]
end

@ n = 5
while ($n)
     # do something
     @ n--
end

#switch-case
switch ($argv[$i])
 case quit:
        break        # leave the switch statement
 case list:
        ls
        breaksw
 case delete:
 case erase:
        @ k = $i + 1
        rm $argv[$k]
        breaksw
endsw
   
#here document
grep $i <<HERE
John Doe   101 Surrey Lane    London, UK    5E7 J2K
Angela Langsbury   99 Knightsbridge, Apt. K4     Liverpool
John Major  10 Downing Street  London
HERE

cat > tempdata <<ENDOFDATA
53.3 94.3 67.1
48.3 01.3 99.9
42.1 48.6 92.8
ENDOFDATA

exit 0

更多精彩内容其他人还在看

Shell脚本中计算字符串长度的5种方法

这篇文章主要介绍了Shell脚本中计算字符串长度的5种方法,来自于个人Shell脚本长期的开发经验,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本实现把进程负载均衡到多核CPU中

这篇文章主要介绍了Shell脚本实现把进程负载均衡到多核CPU中,可以把进程指定运行在某个CPU中,需要的朋友可以参考下
收藏 0 赞 0 分享

5个Shell脚本编程入门练习例子

这篇文章主要介绍了5个Shell脚本编程入门例子,涵盖了各种操作,又有一些游戏的性质,作为入门练习例子是不很不错的,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本编程中常用的数学运算实例

这篇文章主要介绍了Shell脚本编程中常用的数学运算实例,包含最基本的加减乘除,还有质数、偶数的判断等,需要的朋友可以参考下
收藏 0 赞 0 分享

5个实用的shell脚本面试题和答案

这篇文章主要介绍了5个实用的shell脚本面试题和答案,给出的脚本堪称编码规范,麻雀虽小,异常处理,友好提示,一应俱全,值得学习,需要的朋友可以参考下
收藏 0 赞 0 分享

使用bash shell删除目录中的特定文件的3种方法

这篇文章主要介绍了使用bash shell删除目录中的特定文件的3种方法,分别为扩展模式匹配符、GLOBIGNORE 变量和find 命令,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell获取文件的文件名和扩展名的例子

这篇文章主要介绍了Shell获取文件的文件名和扩展名的例子,简明版的代码实例,看了就懂,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell多线程操作及线程数控制实例

这篇文章主要介绍了Shell多线程操作及线程数控制实例,文中从单线程实现一个需求开始,不断加入代码实现多线程以及线程数的控制功能,需要的朋友可以参考下
收藏 0 赞 0 分享

阿里云主机一键安装lamp、lnmp环境的shell脚本分享

这篇文章主要介绍了阿里云主机一键安装lamp、lnmp环境的shell脚本分享,需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本转发80端口数据包给Node.js服务器

开发基于Node.js的WEB应用很方便,但是服务端口问题很蛋疼,由于Linux内核规定普通用户只能使用大于1024的端口号,所以使用普通用户运行Node.js服务就不能监听80端口
收藏 0 赞 0 分享
查看更多