C Shell Scripting Learning Notes

Programming | 14 April 2019

In this page, you will find the syntax and most common how to’s of C Shell Scripting. This might be useful for beginners in programming or professionals in automation industries such as VLSI or Telecomm to find csh syntax and code quickly using this single page online reference material. Click on any of the contents in the sidebar to view the code.

Basics

Expressionscode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/csh

# declaring variables
set a = 2
set b = 6

# math operations (white space must taken care)
set c = `expr $a+$b`
echo $c # prints 2+6

# add
set c = `expr $a + $b`
echo $c # prints 8

# subtract
set c = `expr $b - $a`
echo $c # prints 4

# divide
set c = `expr $b / $a`
echo $c # prints 12

# remainder
set c = `expr $b % $a`
echo $c # prints 12

Control Structurescode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# check if file exist
if (-e filename) echo "File exist"

#-------------------------
# file status expressions
#-------------------------
# d - file is a directory
# e - file exists
# f - file is an ordinary file
# o - user owns the file
# r - user has read access to the file
# w - user has write access to the file
# x - user has execute access to the file
# z - file is zero bytes long

# if/then/else
set a = 5
set b = 10

if ($a > $b) then
    echo "a is greater than b"
else if ($a < $b) then
    echo "a is lesser than b"
else
    echo "a is equal to b"
endif

# prints "a is lesser than b"

loopscode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#----------------
# foreach loop
#----------------
# declare a word list
set colors = "white red black green blue"

# iterate over the word list (paranthesis is important)
foreach c ($colors)
    echo $c
end

#----------------
# while loop
#----------------
set a = 0
while ($a < 4)
    echo "a is $a"
    @ a++
end

# prints
# a is 0
# a is 1
# a is 2
# a is 3

breakcode

1
2
3
4
5
6
7
8
9
10
11
12
13
set a = 0
while (1 > 0)
    echo "a is $a"
    @ a++
    if ($a == 4) break
end
echo "breaked from while loop after a is $a"

# prints
# a is 1
# a is 2
# a is 3
# breaked from while loop after a is 4

continuecode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
set colors = "white red black green blue"
foreach c ($colors)
    if ($c == black) then
        echo "as color is $c, continuing.."
        continue
    endif
    echo "color is $c"
end

# prints
# color is white
# color is red
# as color is black, continuing..
# color is green
# color is blue

gotocode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
set a = 20

if ($a == 10) then
    goto csk
else if ($a == 20) then
    goto rcb
else
    goto kkr

csk:
    echo "this is csk"
    exit 1

rcb:
    echo "this is rcb"
    exit 1

kkr:
    echo "this is kkr"
    exit 1

# prints 
# this is rcb

switchcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if ($#argv == 0) then
    echo "No arguments provided.."
    exit 1
else
    switch($argv[1])
    case [yY][eE][sS]:
        echo "Input is YES"
        breaksw
    case [nN][oO]:
        echo "Input is NO"
    default:
        echo "Input is not YES/NO"
        breaksw
    endsw
endif

# source test.csh yes
# print "Input is YES"

Interrupt Handlingcode

1
2
3
4
5
6
7
8
9
10
# used to transfer control to onintr statement once CTRL+C is done to kill the script.
onintr close
while (1 > 0)
    echo "Loading avengers...."
    sleep 2
end

close:
echo "Avengers killed.."
echo "Yet Ironman is safe!"

How to's

How to store the output of a command?code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# backquotes is used to store command's output to a variable for further processing
set a = `pwd`
echo $a # prints /usr2/gilango/study

set b = `date`

echo $b    # Sun Apr 14 13:45:25 IST 2019
echo $b[1] # Sun 

foreach f ($b)
    echo $f
end

# prints
# Sun
# Apr
# 14
# 13:45:25
# IST
# 2019

How to read user input?code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# two ways to read user input


# method 1: set a = $<
echo -n "Input a value for a: "
set a = $<
echo "You entered a as: $a"

# prints 
# Input a value for a: 9
# You entered a as: 9

# method 2: set a = `head -1`
echo -n "Input a value for a: "
set a = `head -1`
echo "You entered a as: $a"

# prints 
# Input a value for a: 21
# You entered a as: 21

How to use single and double quotes?code

1
2
3
4
5
6
7
8
9
10
11
12
13
# single quotes
#   * allow inclusion of spaces
#   * prevent variable substitution
#   * allow filename generation

# double quotes
#   * allow inclusion of spaces
#   * allow variable substitution
#   * allow filename generation

set a = -l
echo 'ls $a' # prints ls $a
echo "ls $a" # prints ls -l

In case if you found something useful to add to this article or you found a bug in the code or would like to improve some points mentioned, feel free to write it down in the comments. Hope you found something useful here.