CSH syntax example reference

Source: Internet
Author: User

 

CSH instance reference:

 

# ! /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 'whoam '. 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 = 'whoam'

# 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

-E file merely exists (may be protected from user)
-R file exists and is readable by user
-W file is writable by user
-X file is executable by user
-O file is owned by user
-Z file has size 0
-F file is an ordinary file
-D 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 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

 

From:Http://www-cs.canisius.edu/ONLINESTUFF/UNIX/shellprogramming.html

 

Complete!

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.