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

相关文章

  • bash脚本中if语句的使用方法

    bash脚本中if语句的使用方法

    与大多数语言一样,bash 有自己的条件形式。在使用时,要遵循以上格式;即,将 "if" 和 "then" 放在不同行,并使 "else" 和结束处必需的 "fi" 与它们水平对齐。这将使代码易于阅读和调试
    2014-05-05
  • Shell实现字符串处理的方法详解

    Shell实现字符串处理的方法详解

    这篇文章主要为大家详细介绍了Linux Shell中实现字符串处理的方法,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的可以了解一下
    2022-09-09
  • Shell中实现飞行文字效果

    Shell中实现飞行文字效果

    这篇文章主要介绍了Shell中实现飞行文字效果,比较炫的一个特效,在网页中比较常见,需要的朋友可以参考下
    2014-06-06
  • Shell内置命令之exit的语法与实例

    Shell内置命令之exit的语法与实例

    系统中是有exit命令的,用于退出当前用户的登录状态,但是在 Shell 脚本中,exit 语句是用来退出当前脚本的,下面这篇文章主要给大家介绍了关于Shell内置命令之exit的语法与实例,需要的朋友可以参考下
    2022-03-03
  • Shell脚本实现根据文件的修改时间来分类文件

    Shell脚本实现根据文件的修改时间来分类文件

    这篇文章主要介绍了Shell脚本实现根据文件的修改时间来分类文件,本文直接给出实现代码,需要的朋友可以参考下
    2015-07-07
  • Linux nano命令使用示例

    Linux nano命令使用示例

    nano是一个小型、免费、友好的编辑器,旨在取代非免费Pine包中的默认编辑器Pico,nano是一个字符终端的文本编辑器,有点像DOS下的editor程序,这篇文章主要介绍了Linux nano命令使用示例,需要的朋友可以参考下
    2023-03-03
  • shell脚本学习指南[五](Arnold Robbins & Nelson H.F. Beebe著)

    shell脚本学习指南[五](Arnold Robbins & Nelson H.F. Beebe著)

    这篇文章主要介绍了shell脚本学习指南[五](Arnold Robbins & Nelson H.F. Beebe著),需要的朋友可以参考下
    2014-02-02
  • shell脚本实现多彩进度条

    shell脚本实现多彩进度条

    这篇文章主要介绍了shell脚本实现多彩进度条的相关资料,需要的朋友可以参考下
    2017-07-07
  • 处理JSON最强命令jq使用场景

    处理JSON最强命令jq使用场景

    jq命令是处理json字符串的神器, 主要用于获取JSON属性/简单重组JSON字符串,本章详细介绍jq的主要应用场景,感兴趣的朋友跟随小编一起看看吧
    2023-07-07
  • shell脚本编程之if语句学习笔记

    shell脚本编程之if语句学习笔记

    这篇文章主要介绍了shell脚本编程之if语句学习笔记,本文先是给出了程序代码,然后详细的分解了第句代码的作用,需要的朋友可以参考下
    2014-09-09

最新评论