Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH Scripts.

Similar presentations


Presentation on theme: "Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH Scripts."— Presentation transcript:

1 Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH Scripts

2 Unix Family Tree

3 Unix Architecture

4 Unix History and Motivation  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 build new tools easily and efficiently To customize the shell and user interface. To customize the shell and user interface. To string together a series of Unix commands to create new functionality. To string together a series of Unix commands to create new functionality. To create custom commands that do exactly what we want. To create custom commands that do exactly what we want.

5 What is Shell?  Shell is Command Interpreter that turns text that you type (at the command line) in to actions: Command Interpreter that turns text that you type (at the command line) in to actions: User Interface: take the command from user User Interface: take the command from user  Programming Shell can do Customization of a Unix session Customization of a Unix session Scripting Scripting

6 Customization of a Session  Each shell supports some customization. User prompt User prompt Where to find mail Where to find mail Shortcuts (alias) Shortcuts (alias)  The customization takes place in startup files Startup files are read by the shell when it starts up Startup files are read by the shell when it starts up The Startup files can differ for different shell The Startup files can differ for different shell

7 Types of Shells  Interactive vs. Non-interactive; login or not  Interactive login shell started after login  Non-interactive shell Present when shell script is running Present when shell script is running Just inherits parent’s environment Just inherits parent’s environment  Interactive non-login shell started Started from a command line Started from a command line Copies parent environment then invokes ~/.bash_rc (or ~/.cshrc or ~/.tcshrc) Copies parent environment then invokes ~/.bash_rc (or ~/.cshrc or ~/.tcshrc)

8 Popular Shells  sh Bourne Shell  ksh Korn Shell  csh,tcsh C Shell (for this course)  bash Bourne-Again Shell

9 Families of Shells

10 Flavors of Unix Shells  Two main flavors of Unix Shells Bourne (or Standard Shell): sh, ksh, bash, zsh Bourne (or Standard Shell): sh, ksh, bash, zsh FastFast $ for command prompt$ for command prompt C shell : csh, tcsh C shell : csh, tcsh better for user customization and scriptingbetter for user customization and scripting %, > for command prompt%, > for command prompt  To check shell: % echo $SHELL (shell is a pre-defined variable) % echo $SHELL (shell is a pre-defined variable)  To switch shell: % exec shellname (e.g., % exec bash) % exec shellname (e.g., % exec bash)

11 Startup files  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) ~/.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)

12 Some Special Keys Under tcsh  Ctrl-U = Delete everything on the command- line  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

13 Don’t forget your Best Friend  % man command (e.g., % man ls) shows information about the command shows information about the command usually space or Enter for more information usually space or Enter for more information q to quit q to quit % man man % man man

14 Create a shell script  Creating a simple shell script A shell script is a file that contains commands that the shell can execute. A shell script is a file that contains commands that the shell can execute. Any commands you enter in response to a shell prompt.Any commands you enter in response to a shell prompt. A utility A utility A compiled program A compiled program Another shell script Another shell script Control flow commandsControl flow commands  Run a shell script Enter the script filename on the command line Enter the script filename on the command line The shell interprets and execute the commands one after another The shell interprets and execute the commands one after another  Why shell script? Simply and quickly initiate a complex series of tasks or a repetitive procedure. Simply and quickly initiate a complex series of tasks or a repetitive procedure.

15 Shell programming  Make the file executable When you create a shell script using a editor, does it have execute permission typically? When you create a shell script using a editor, does it have execute permission typically? Example: (Make sure you are using tcsh/csh script!...)Example: (Make sure you are using tcsh/csh script!...) willow> echo $SHELL /bin/tcsh willow>./test./test: Permission denied. willow> ls -l test -rw-r--r-- 1 student ums 33 Sep 18 16:33 test willow> chmod +x test willow>./test This is Test!

16 Invoking a Shell script  Give the shell a command on the command line The shell forks a process The shell forks a process Which creates a non-interactive duplicate of the shell processWhich creates a non-interactive duplicate of the shell process The newly forked process attempts to exec the command The newly forked process attempts to exec the command If the command is an executable programIf the command is an executable program Exec succeeds Exec succeeds System overlays the newly created subshell with the executable program System overlays the newly created subshell with the executable program The command is a shell scriptThe command is a shell script Exec failed Exec failed The command is assumed to be a shell script The command is assumed to be a shell script The subshell runs the commands in the shell. The subshell runs the commands in the shell.

17 Invoking a Shell script  The shell itself is program It can be run as a command in a shell and also accepts arguments. Note: Let’s find your default shell executing “echo $SHELL” It can be run as a command in a shell and also accepts arguments. Note: Let’s find your default shell executing “echo $SHELL” willow> echo $SHELL /bin/tcsh  To run a shell script Which does not have executable permission Which does not have executable permission Ex: willow>tcsh test Run the script with different shell other than your interactive shell Run the script with different shell other than your interactive shell Ex: willow>sh test

18 Invoking a Shell script  Put special characters on the first line of a shell script To tell OS checks what kind of file it is before attempting to exec it To tell OS checks what kind of file it is before attempting to exec it To tell which utility to use (sh, csh, tcsh, …) To tell which utility to use (sh, csh, tcsh, …)  Special sequence The firsts two character of a script are #! The firsts two character of a script are #! Then followed by the absolute pathname of the program that should execute the script Then followed by the absolute pathname of the program that should execute the scriptEx: willow> more test #!/bin/tcsh # This line will not run since it is commented out... echo 'This is Test!‘

19 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 #! In the first line. #! In the first line. Or inside quotes Or inside quotes

20 Parameters and Variables  A shell parameter is associated with a value that is accessible to the user. Shell variables Shell variables Names consist of letters, digits and underscoresNames consist of letters, digits and underscores By convention, environment variables use UPPERCASE By convention, environment variables use UPPERCASE User created variables (create and assign value)User created variables (create and assign value) Keyword shell variablesKeyword shell variables Have special meaning to the shell Have special meaning to the shell Being created and initialized by the startup file Being created and initialized by the startup file Positional parameters Positional parameters Allow you to access command line argumentsAllow you to access command line arguments Special parameters Special parameters Such asSuch as The name of last command The name of last command The status of most recently executed command The status of most recently executed command The number of command-line arguments The number of command-line arguments

21 Positional Parameters  The command name and arguments are the positional parameters. Because you can reference them by their position on the command line Because you can reference them by their position on the command line $0 : Name of the calling program $0 : Name of the calling program $1 - $9 : Command-line Arguments $1 - $9 : Command-line Arguments The first argument is represented by $1The first argument is represented by $1 The second argument is represented by $2The second argument is represented by $2 And so on up to $9And so on up to $9 The rest of arguments have to be shifted to be able to use $1- $9 parameters.The rest of arguments have to be shifted to be able to use $1- $9 parameters.

22 Positional Parameters  Example: Change directory to your assigned numbered subdirectory willow> cd 1 List the directory contents, confirming display_5args willow> ls -l display_5args Change mode of display_5args to executable willow> chmod +x display_5args Execute the script willow>./display_5args 1 2 3 4 5 you are running script./display_5args with parameter 1 2 3 4 5

23 Positional Parameters  $1-$9 allows you to access 10 arguments How to access others? How to access others?  Promote command-line arguments: shift Built-in command shift promotes each of the command-line arguments. Built-in command shift promotes each of the command-line arguments. The first argument ( which was $1) is discardedThe first argument ( which was $1) is discarded The second argument ( which was $2) becomes $1The second argument ( which was $2) becomes $1 The third becomes the secondThe third becomes the second And so onAnd so on Makes additional arguments available Makes additional arguments available Repeatedly using shift is a convenient way to loop over all the command-line arguments Repeatedly using shift is a convenient way to loop over all the command-line arguments

24 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 1 2 3 2 3 3

25 willow> more demo_shift #!/bin/tcsh echo $1 $2 $3 shift echo $1 $2 shift echo $1 shift echo $? shift shift willow>./demo_shift 1 2 3 4 1 2 3 2 3 300 shift: No more words.

26 Special Parameters  Useful values Command-line arguments Command-line arguments Execution of shell commands Execution of shell commands Can not change the value directly, like positional parameters Can not change the value directly, like positional parameters  Value of Command-line arguments: $* and $@ $* and $@represent all the command_line arguments ( not just the first nine) $* and $@represent all the command_line arguments ( not just the first nine) “$*” : treats the entire list of arguments as a single argument “$*” : treats the entire list of arguments as a single argument “$@” : produce a list of separate arguments (Only bash/ksh/sh) “$@” : produce a list of separate arguments (Only bash/ksh/sh)

27 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. 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 $* 1 2 3 using $@ 1 2 3

28 Special Parameters  The number of arguments: $# Return a decimal number Return a decimal number Use the test to perform logical test on this number Use the test to perform logical test on this number 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. 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.

29 Special Parameters  Exit status: $? When a process stops executing for any reason, it returns an exit status to its parent process. When a process stops executing for any reason, it returns an exit status to its parent process. By convention, By convention, Nonzero represents a false value that the command failed.Nonzero represents a false value that the command failed. A zero value is true and means that the command was successfulA 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 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.Otherwise, the exit status of the script is the exit status of the last command the script ran.

30 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 0

31 Summary  A shell is both a command interpreter and a programming language.  Job control Control-z/fg/bg/& Control-z/fg/bg/&  Variables Local and environment variables Local and environment variables Declare and initialize a variable ( no type) Declare and initialize a variable ( no type) Export unset Export unset  Command line expansion Parameter expansion/variable expansion/command/substitution/pathname expansion Parameter expansion/variable expansion/command/substitution/pathname expansion Quote ( ‘ ‘ “ “ \ ) Quote ( ‘ ‘ “ “ \ ) “ “ all but parameter, variable expansion and \“ “ all but parameter, variable expansion and \ ‘ ‘ suppress all types of expansion‘ ‘ suppress all types of expansion \ escaping the following special character\ escaping the following special character

32 Basic Script Example willow> more basic_script #!/bin/tcsh echo 'Listing the files in long format appending due date/time' echo ls -lrtah echo echo 'Listing the files in long format appending due date/time' echo df -k # Using diff to find two files differences and writing them to another file diff -c for_test.bash for_test >> file_differences & echo echo 'sleeping mode for 4 seconds. Please wait!' echo sleep 4 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’

33 Killing BAD Processes  The “kill” command: kill [- ] kill [- ] Send to process Send to process  The “killall” command: killall [- ] killall [- ] Send to all processes that start with Send to all processes that start with  Useful signals ( kill –l for the complete list): TERMthe default, “terminate”, kills things nicely KILLwill kill anything, but not nicely HUP“hangup”, used to reload configurations STOPstops (suspends) a running process

34 Summary  Shell parameters HOME HOME PATH PATH PS1 PS1 SHELL SHELL $0 $0 $n $n $* $* $@ $@ $# $# $$ $$ $! $! $? $?

35 Summary  Special Characters NEWLINE NEWLINE ; () () & | > >> >> < << << * ? \ ‘ “ ` ` ` ` [] [] $. # && && || || !


Download ppt "Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH Scripts."

Similar presentations


Ads by Google