csh脚本语法实例

 更新时间:2014年11月24日 11:52:54   投稿:junjie  
这篇文章主要介绍了csh脚本语法实例,小编看起来和bash shell也差不太多,需要的朋友可以参考下

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

相关文章

  • Linux 命令expect使用详解

    Linux 命令expect使用详解

    expect是由Don Libes基于Tcl语言开发的,是一种脚本语言,主要应用于自动化交互式操作的场景,借助Expect处理交互的命令,本文给大家介绍Linux 命令expect使用详解,感兴趣的朋友一起看看吧
    2023-11-11
  • win下调用putty执行命令脚本分享

    win下调用putty执行命令脚本分享

    这篇文章主要介绍了win下调用putty执行命令脚本,可以利用这个实现一些自动化的工作,需要的朋友可以参考下
    2014-03-03
  • Linux shell数组与关联数组的用法实例

    Linux shell数组与关联数组的用法实例

    今天小编就为大家分享一篇关于Linux shell数组与关联数组的用法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • linux中kvm的安装及快照管理

    linux中kvm的安装及快照管理

    这篇文章主要介绍了linux中kvm的安装及快照管理的相关资料,需要的朋友可以参考下
    2016-12-12
  • linux 定时执行shell、python脚本的方法

    linux 定时执行shell、python脚本的方法

    这篇文章主要介绍了linux 定时执行shell、python脚本的方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-05-05
  • linux安装php扩展脚本分享

    linux安装php扩展脚本分享

    本文主要介绍了linux下安装php扩展的步骤,安装是由shell批量执行的,,需要的朋友可以参考下
    2014-03-03
  • Shell函数返回值方式

    Shell函数返回值方式

    本文主要介绍了Shell函数返回值方式,主要介绍了两种返回方式,分别介绍了场景的使用和区别,具有一定的参考价值,感兴趣的可以了解一下
    2022-08-08
  • linux 比较两个文件夹diff不同 (diff命令, md5列表)

    linux 比较两个文件夹diff不同 (diff命令, md5列表)

    这篇文章主要介绍了linux 比较两个文件夹diff不同 (diff命令, md5列表),比较文件夹diff,可以直接使用diff命令,也可以比较文件md5列表,下面通过实例给大家介绍下,感兴趣的朋友跟随脚本之家小编一起学习吧
    2018-05-05
  • Shell脚本实用的六个技巧示例

    Shell脚本实用的六个技巧示例

    本文介绍Shell脚本实用的六个技巧示例:使用Shell脚本实现自动化备份、使用Shell脚本实现定时任务、使用Shell脚本实现远程登录服务器、使用Shell脚本自动化部署应用、使用Shell脚本实现快速部署开发环境、使用Shell脚本实现快速检查服务器性能
    2023-11-11
  • 进程状态ps -ef中的e、f含义讲解

    进程状态ps -ef中的e、f含义讲解

    这篇文章主要介绍了进程状态ps -ef中的e、f含义讲解,通过本文学习我们知道-e和-A都显示有关其他用户进程的信息,包括那些没有控制终端的进程,-f显示用户id,进程id,父进程id,最近CPU使用情况,进程开始时间等等,具体含义及更多命令跟随小编通过本文学习
    2022-11-11

最新评论