TCSH Scripting: Basic - A. Basic - B. Intermediate - C. D. Resources 1
TCSH Scripting: Basic - A. Basic - B. Intermediate - C. D. Resources 1
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.
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.
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
Unix Page 2