csh脚本语法实例

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

(0)

相关推荐

  • csh脚本语法实例

    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   

  • Mysql 5.7.9 shutdown 语法实例详解

    mysql-5.7.9 终于提供shutdown 语法啦: 之前如果想关闭一个mysql数据库可以通过kill 命令.mysqladmin shutdown .service mysqld stop 等这样的方式. 然而在mysql-5.7.9之后mysql终于提供了SQL接口的shutdown语法啦! SQL接口下的shutdown语法: 语法 shutdown ; -- 这个shutdown要执行成功要有shutdown权限才行. shutdown的一个例子: [root@workstudi

  • Linux下IP设置脚本的实例及遇到问题解决办法

    Linux下IP设置脚本的实例及遇到问题解决办法 背景 一个Java web项目有一个功能是IP修改,Linux的IP修改脚本如下: #!/bin/bash #useing parameter ip:netmask:gateway:dns1 #system version:centos6,7/redhat6,7 #read parameter ipaddr=`echo $1|cut -d ":" -f 1` netmask=`echo $1|cut -d ":" -

  • Kotlin 基础语法实例详解

    Kotlin 基础语法实例详解 包 定义和引入Java一样,在文件开头, 行结束不需要" ; " package com.test.hello import android.os.Bundle 变量 只读变量,val 开头,初始化后不能再赋值,相当于Java的 final 变量 val a: Int = 1 val b = 1 //类型自动推断为Int val c: Int //没有初始化时必须指定类型 c = 1 //初始化 可变变量, var 关键字开头 var x = 10 x

  • 利用Java理解sql的语法(实例讲解)

    select 相当于 for 循环 select id from IDArray LinkedList a = new LinkedList(); for ( int i=0 ; i<tableA.length ; i++){ a.add(IDArray.get("id" ) ); } return a; 当执行子查询时,可以理解为 select id, ( select name from nameArray) as names ,from Idarray LinkedList

  • 对Python 网络设备巡检脚本的实例讲解

    1.基本信息 我公司之前采用的是人工巡检,但奈何有大量网络设备,往往巡检需要花掉一上午(还是手速快的话),浪费时间浪费生命. 这段时间正好在学 Python ,于是乎想(其)要(实)解(就)放(是)双(懒)手. 好了,脚本很长又比较挫,有耐心就看看吧. 需要巡检的设备如下: 设备清单 设备型号 防火墙 华为 E8000E H3C M9006 飞塔 FG3950B 交换机 华为 S9306 H3C S12508 Cisco N7K 路由器 华为 NE40E 负载 Radware RD5412 Ra

  • shell在指定目录下批量执行sql脚本的实例

    如下所示: #!/bin/bash #execute all script in specified directory MYDATE=`date +%F'-'%T'-'%w` MYSQL_PATH=/tmp/scripts #指定的目录 LOG_FILE=/tmp/scripts/exec_${MYDATE}.log confirm= db_name= db_pass= for file in ${MYSQL_PATH}/* do if [ -f "$file" ] ; then p

  • python钉钉机器人运维脚本监控实例

    如下所示: #!/usr/bin/python3 # -*- coding:UTF-8-*- # Author: zhuhongqiang from urllib import request import json from sys import argv access_token = "xxx" def send_msg(mobile, item_name): """ 钉钉机器人API接口地址: https://open-doc.dingtalk.co

  • python redis 删除key脚本的实例

    单机模式 代码片段 安装 pip install redis import redis r = redis.Redis(host='192.168.1.3', port=6188,db=0,decode_responses=True) list_keys = r.keys("DEMO_xx_*") for key in list_keys: r.delete(key) 集群模式 代码片段 安装 pip install redis-py-cluster from rediscluster

  • Python socket聊天脚本代码实例

    这篇文章主要介绍了Python socket聊天脚本代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 没有前端,多开了一条线程用于接收信息. 服务器端: # -*- coding:utf-8 -*- import socket import threading class Sock_Server: def __init__(self): self.host = '192.168.1.11' # 获取主机名 self.port = 1234

随机推荐