TCSH Scripting MCSR Basic
TCSH Scripting MCSR Basic
TCSH Scripting MCSR Basic
Brief
Unix Architecture
The first version of Unix came from AT&T in the early 1970s (Unix is old!). Unix was developed by programmers and for programmers. Unix is designed so that users can extend the functionality
To build new tools easily and efficiently To customize the shell and user interface. To string together a series of Unix commands to create new functionality. To create custom commands that do exactly what we want.
What is Shell?
Shell
is
Command Interpreter that turns text that you type (at the command line) in to actions: User Interface: take the command from user
Programming
Shell can do
Customization of a Session
Each
The
files
Startup files are read by the shell when it starts up The Startup files can differ for different shell
Types of Shells
Interactive
vs. Non-interactive; login or not Interactive login shell started after login Non-interactive shell
Interactive
Started from a command line Copies parent environment then invokes ~/.bash_rc (or ~/.cshrc or ~/.tcshrc)
Popular Shells
Bourne Shell Korn Shell C Shell (for this course) Bourne-Again Shell
Families of Shells
To check shell:
% echo $SHELL (shell is a pre-defined variable) % exec shellname (e.g., % exec bash)
To switch shell:
sh,ksh: /etc/profile (out-of-the-box login shell settings) /etc/profile.local (addtnl. local system settings) ~/.profile (addtnl. user customized settings) ~/.kcshrc (non-login shell user customization) bash: /etc/profile (out-of-the-box login shell settings) /etc/bash.bashrc (out-of-box non-login settings) /etc/bash.bashrc.local (global non-login settings) ~/.bash_profile (login shell user customization) ~/.bashrc (non-login shell user customization) ~/.bash_logout (user exits from interactive login shell) csh/tcsh: /etc/login (out-of-the-box login shell settings) /etc/csh.login (non-login shell customizations) /etc/csh.login.local (global non-login settings) ~/.login: (login shell user customizations) ~/.cshrc: (non-login shell user customizations) ~/.cshrc.logout: (non-login shells at logout) ~/.logout: (read by login shells at logout)
Startup files
Ctrl-U = Delete everything on the commandline Ctrl-A = Move cursor to the front Ctrl-E = Move cursor to the end Ctrl-P = Set the current command-line to the previous command Ctrl-N = Set the current command-line to the next command TAB = Filename completion
A shell script is a file that contains commands that the shell can execute.
Any commands you enter in response to a shell prompt. A utility A compiled program Another shell script Control flow commands
Enter the script filename on the command line The shell interprets and execute the commands one after another Simply and quickly initiate a complex series of tasks or a repetitive procedure.
Shell programming
Make
When you create a shell script using a editor, does it have execute permission typically?
Example: script!...) (Make sure you are using tcsh/csh
willow> echo $SHELL /bin/tcsh willow> ./test ./test: Permission denied. willow> ls -l test -rw-r--r-- 1 student ums willow> chmod +x test willow> ./test This is Test!
It can be run as a command in a shell and also accepts arguments. Note: Lets find your default shell executing echo $SHELL
willow> echo $SHELL /bin/tcsh
To
Run the script with different shell other than your interactive shell
Ex: willow>sh test
To tell OS checks what kind of file it is before attempting to exec it To tell which utility to use (sh, csh, tcsh, ) The firsts two character of a script are #! Then followed by the absolute pathname of the program that should execute the script Ex:
willow> more test #!/bin/tcsh # This line will not run since it is commented out... echo 'This is Test!
Special sequence
Make a comment #
Comments
make shell scripts easier to read and maintain Pound sign (#) start a comment line until the end of that line as second line in previous example, except
Shell variables
Names consist of letters, digits and underscores By convention, environment variables use UPPERCASE User created variables (create and assign value) Keyword shell variables Have special meaning to the shell Being created and initialized by the startup file
Positional parameters
Allow you to access command line arguments
Special parameters
Such as The name of last command The status of most recently executed command The number of command-line arguments
Positional Parameters
The
Positional Parameters
Example:
willow> cd 1
Change directory to your assigned numbered subdirectory List the directory contents, confirming display_5args
willow> ls -l display_5args
Positional Parameters
Makes additional arguments available Repeatedly using shift is a convenient way to loop over all the command-line arguments
Positional Parameters
Example:
willow> more demo_shift #!/bin/tcsh echo $1 $2 $3 shift echo $1 $2 shift echo $1 willow> ./demo_shift 1 2 3 123 23 3
willow> more demo_shift #!/bin/tcsh echo $1 $2 $3 shift echo $1 $2 shift echo $1 shift echo $? shift echo $? shift echo $? willow> ./demo_shift 1 2 3 4 1 2 3 2 3 3 0 0 shift: No more words.
Special Parameters
Useful values
Command-line arguments Execution of shell commands Can not change the value directly, like positional parameters $* and $@represent all the command_line arguments ( not just the first nine) $* : treats the entire list of arguments as a single argument $@ : produce a list of separate arguments (Only bash/ksh/sh)
BASH SCRIPT WITH $*and $@ willow> more for_test.bash #!/bin/bash echo "using \$* " for arg in "$*" do echo "$arg" done echo "using \$@ " for arg in "$@" do echo "$arg" done willow> ./for_test.bash 1 2 3 using $* 123 using $@ 1 2 3
TCSH SCRIPT WITH $*and $@ willow> more for_test #!/bin/tcsh echo 'using $*' foreach arg ($*) echo "$arg" end echo 'using $@' foreach arg ($@) echo "$arg" end willow> ./for_test 1 2 3 using $* 1 2 3 using $@ Illegal variable name.
Special Parameters
The
number of arguments: $#
Return a decimal number Use the test to perform logical test on this number
willow> ./num_args 1 this script is called with 1 arguments. willow> ./num_args 2 this script is called with 1 arguments. willow> ./num_args 0 this script is called with 1 arguments.
willow> more num_args echo this script is called with $# arguments. willow> chmod +x num_args willow> ./num_args this script is called with 0 arguments.
Special Parameters
Exit status: $?
When a process stops executing for any reason, it returns an exit status to its parent process. By convention,
Nonzero represents a false value that the command failed. A zero value is true and means that the command was successful
You can specify the exit status that a shell script returns by using the exit built-in followed by a number
Otherwise, the exit status of the script is the exit status of the last command the script ran.
willow> ls a a: No such file or directory willow> echo $? 2 willow> echo olemiss olemiss willow> echo $? 0
willow> more exit_status echo this program will have the exit code of 8. exit 8 willow> ./exit_status this program will have the exit code of 8. willow> echo $? 8 willow> echo $? 0
Summary
Control-z/fg/bg/& Local and environment variables Declare and initialize a variable ( no type) Export unset Parameter expansion/variable expansion/command/substitution/pathname expansion Quote ( \ )
all but parameter, variable expansion and \ suppress all types of expansion \ escaping the following special character
Variables
echo echo 'GO REBELS' echo 'To find out the differences of files for_test and for_test.bash, ' echo 'Please open file_differences via using cat command as shown below:' echo 'cat file_differences
Summary
Shell parameters
Summary
Special
Characters
* ? \ ` ` [] $ . # && || !