CSH script Syntax Instance _linux shell

Source: Internet
Author: User

CSH Example Reference:

Copy Code code as follows:

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

#just to check syntax
#csh-N $

#argv
if ($ #argv < 2) Then
echo "Sorry, but you entered too few parameters"
echo "Usage: $ arg1 arg2
Exit
endif
Set arg1 = $
Set arg2 = #2

foreach I ($*)
Echo $i
End

#execute commands
echo "Hello there ' WhoAmI". How do you Are? "
echo "You are currently using ' hostname ' and '" the ' date '
echo "Your directory is ' pwd '"
WhoAmI
Hostname
Date
Pwd

#var
Set name = Mark
Echo $name
Set name = "Mark Meyer" # If the string has spaces, 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 ' array
Shift #if no argument is G Iven, it'll 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< br> >=        numerical greater than or equal to
>          numerical greater than
<         Numerical less than

-e file           file merely exists (May is protected from user) br>-R file           file exists and is readable by user
-W FILE&N bsp;          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 a ordinary file-D file           file is a directory

! --Negate
&&--Logical AND
|| --Logical OR

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

if (-e somefile) then
grep $ somefile
Else
echo "Grievous error! The 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 <John Doe Surrey Lane London, UK 5E7 j2k
Angela Langsbury Knightsbridge, Apt. K4 Liverpool
John Major 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.