0% found this document useful (0 votes)
174 views

TCSH Scripting: Basic - A. Basic - B. Intermediate - C. D. Resources 1

This document provides an overview of tcsh scripting. It covers topics such as shebangs, variables, parameters, file testing, boolean operators, arithmetic, user input, arrays, and executing commands. Key points include using #!/bin/csh as the shebang, setting variables with set, accessing parameters with $1 $2 etc., testing files with -e, boolean operators like && and ||, using @ for arithmetic variables, getting user input with $<, and executing commands within backticks.

Uploaded by

444M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views

TCSH Scripting: Basic - A. Basic - B. Intermediate - C. D. Resources 1

This document provides an overview of tcsh scripting. It covers topics such as shebangs, variables, parameters, file testing, boolean operators, arithmetic, user input, arrays, and executing commands. Key points include using #!/bin/csh as the shebang, setting variables with set, accessing parameters with $1 $2 etc., testing files with -e, boolean operators like && and ||, using @ for arithmetic variables, getting user input with $<, and executing commands within backticks.

Uploaded by

444M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

tcsh scripting

Thursday, April 30, 2020 12:56 PM

1. Resources
a. Basic - https://en.wikibooks.org/wiki/C_Shell_Scripting
b. Basic - https://www.grymoire.com/Unix/Csh.html
c. Intermediate - https://www.computerhope.com/unix/ucsh.htm
d. http://www-cs.canisius.edu/ONLINESTUFF/UNIX/shellprogramming.html#D

2. #!/bin/csh
a. Called as Shebang/hashbang. All scripts under unix execute using the interpreter specified on the first line

3. Extension is .csh

4. echo
a. echo Hello, World! -> "echo" followed by what you want to print will print to console
i. output: Hello, World!

5. ./hello
a. hello is the filename and it should be saved as executable type.

6. Variables
a. set A = Space exists => when printed prints just Space
b. set B = "Space exists" => when printed prints whole Space exists
c. set C = (Space exists) => when printed prints whole Space exists
d. unset name
e. Variables are case sensitive.
f. if to be used later should be referred by $
g. echo $<VariableName> prints the value of the <VariableName>
h. echo $#<VariableName> prints the no of words in the <VaraibleName>
i. echo $?<VariableName> prints if the <VariableName> exists or not (1/0)

7. Parameters
a. Variables $0, $1, $2 and so on are known as positional parameters.
i. $0 refers to the name of the command and $1, $2 and greater will be the parameters passed into the script
b. Parameters passed to a script are stored in wordlist and can be called $argv
i. example
1) invoking script ./myparams "alpha beta" omega
2) echo The $0 command is called with $#argv parameters --> the myparams command ….. 2 parameters
3) echo parameter 1 is $1 --> alpha beta
4) echo parameter 2 is $2 --> omega
5) echo parameter 3 is $3 --> "blank"
6) echo parameter 4 is $4 --> "blank"
7) echo 2nd and on parameters are \"$argv[2-]\" --> "omega"
8) echo All parameters are \"$argv\" --> "alpha beta omega"

8. Variable Modifiers
a. Variable modifiers are convenient to manipulate paths within script
i. set file = /usr/joe/backup.tar.gz
ii. echo $file:h --> returns the directory of a path --> /usr/joe
iii. echo $file:t --> returns filename --> backup.tar.gz
iv. echo $file:r --> returns directory and filename without extension --> /usr/joe/backup.tar
v. echo $file:e --> returns the extension of the path --> gz
vi. echo $file:t:r:r --> backup
vii. echo $file:h:h --> /usr
b. Concatenating a variable with a colon character requires code such as $var"": or ${var}: to avoid "Bad:modifier"
error//

9. if ( -f ~/.aliases ) then
source ~/.aliases
endif
a. -f --> the shell will start faster, because it will neither search for nor execute commands from the file .cshrc in
the invoker's home directory.
b. Note that if the environment variable HOME is not set, fast startup is the default.

10. file existence


if (-e somefile) then
grep $1 somefile
else
echo "Grievous error! Database file does not exist". endif

11. Boolean operators


a. ! ---- negate
b. && ---- logical and
c. || ---- logical or
i. Use spaces when writing multiple conditions and even with the use of parenthesis
ii. C shell confuses with multiple conditions

12. Special variable -- $$


a. There is a special variable called $$ which has the process id number of the process that is running this shell
script. Many programmers use this to create unique file names, often in the /tmp directory.

Unix Page 1
script. Many programmers use this to create unique file names, often in the /tmp directory.
b. Here's an example of copying the first parameter (which is obviously a filename) into a temp file whose name uses
the pid number:
cp $1 /tmp/tempfile.$$
This will create a file whose name is something like /tmp/tempfile.14506, if the pid number is 14506.

13. Arithmetic variables


a. @ should be used when assigning integers to integer type variables but when referencing $ is used
b. If set is used, it treats the assigned as strings
c. @ i = 2 --> blank between @ and i cannot be omitted
d. increment and decrement operators are supported @ i--, @ i++

14. How to display user prompt and get the response


$< -- is used as the input device in the Cshell
while (1)
echo -n "Gimme something: "
set x = $<
if (! $x) break
end

15. Boolean conditions


== 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

16. Output
a. echo -n "Please type in your name: "
i. -n --> If you do not want to cause a newline to be printed, use -n. New line after this will not get printed

17. Built-in variables


$user -- who am I?
$hostess -- name of the computer I am logged on to
$path -- my execution path (list of directories to be searched for executables)
$term -- what kind of terminal I am using
$status -- a numeric variable, usually used to return error codes
$prompt -- what I am currently using for a prompt
$shell -- which shell am I using (usu. either /bin/csh or /bin/sh)

18. Array variables


a. lists are dynamic size, meaning they can shrink or grow
i. set name = (mark sally kathy tony)
1) echo $#name --> 4
2) echo $name[1] --> mark
3) echo $name[4] --> tony
ii. reduction
1) set name = ($name[2-])

19. Executing commands


a. When needed to execute a command inside another command in order to get its output or its return code. To get the output, use the backquotes ``
i. echo "Hello there `whoami` . how are you today?"

Unix Page 2

You might also like