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

UnixArchitecture 2ndsession

Uploaded by

more_peeyush
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

UnixArchitecture 2ndsession

Uploaded by

more_peeyush
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

UNIX Shell and Architecture

[email protected]
Agenda
• UNIX Architecture
• SHELL –An interface to the underlying OS
• Different types of SHELL's
• SHELL features
• SHELL Variables
• SHELL Scripting

Sun Proprietary/Confidential: Internal Use Only


UNIX Architecture in a
nutshell

Sun Proprietary/Confidential: Internal Use Only


Common shells used on UNIX systems
Name Path FreeBSD Linux Mac OS X Solaris 9
5.2.1 2.4.22 10.3
• Bourne /bin/sh . link to bash link to bash .
• Bash /bin/bash optional . .
• C shell /bin/csh link to tcsh link to tcsh link to tcsh .
• Korn shell /bin/ksh .
• TENEX C /bin/tcsh . . . .

Sun Proprietary/Confidential: Internal Use Only


Login Shell
• The Shell that a user gets upon login to machine or
opening a new terminal window.
• Defined in the file /etc/passwd E.g.:
testuser:x:100:10::/export/home/testuser:/usr/bin/bash

username : passwd : uid : gid : home dir : login shell


• To change, superuser must edit the file or use some
user administration GUI/command. E.g. In
OpenSolaris: /usr/sbin/usermod

Sun Proprietary/Confidential: Internal Use Only


Shell Features
• Configurable Prompt
• Configuration Scripts
• Command History
• Command line editing
• Filename Completion
• Job Control
• Redirection, Pipes
• Here Document
• Wildcards
• Language features and scripting

Sun Proprietary/Confidential: Internal Use Only


Shell Features: Prompt
• E.g.: testuser@testmachine:~>
• Configure by setting the Shell variable PS1. What is
the PS1 contents to get the above prompt ?
• Lets see:
testuser@testmachine:~> echo $PS1
\h:\w #
testuser@testmachine:~>
testuser@testmachine:~> PS1="> "

Sun Proprietary/Confidential: Internal Use Only


Shell Features: Configuration
• The Shell can be configured at start-up via
predefined scripts:
• During login: First “/etc/profile” is loaded, then
“/<homedir>/.login/”, then
“/<homedir>/.<shellname>rc”
• Starting a non-login shell:
“/<homedir>/.<shellname>rc”

Sun Proprietary/Confidential: Internal Use Only


Shell Features: History
• The Shell can remember previously executed
commands and recall them.
• Setting the number of commands to remember:
HISTSIZE=100 – bash and ksh
set history=100 – csh and tcsh
• Storing the history on disk:
Automatic in bash and ksh
set savehist=100 in csh

Sun Proprietary/Confidential: Internal Use Only


Shell Features: Recalling History (1)
• “history” command displays numbered list of previous
commands.
• Csh, Tcsh
> !4 execute the 4th command in the history list
> !cp execute the last command beginning with “cp”
> !! execute the previous command again
• Ksh
> Two styles of history recall: Emacs mode and Vi mode
> To select: set -o vi or set -o emacs
> Emacs mode: CTRL+P get previous line, EMACS editing
> Vi mode: Type ESC to enter into Vi editing mode
Sun Proprietary/Confidential: Internal Use Only
Shell Features: Recalling History (2)
• Bash (Combination)
> !4 execute the 4th command in the history list
> !cp execute the last command beginning with “cp”
> !! execute the previous command again
> Up-Arrow display the previous command
> CTRL+R do a reverse incremental search
> Left/Right Arrow move cursor
> CTRL+A Beginning of line
> CTRL+E End of line

Sun Proprietary/Confidential: Internal Use Only


Shell Features: Command Editing
• Bash
> Left/Right Arrow move cursor
> CTRL+A Beginning of line
> CTRL+E End of line
• Ksh
> set -o vi Vi editing mode with Vi keys
> set -o emacs EMACS editing mode with emacs keys
• Csh, Tcsh (Tcsh supports arrow keys)
> ^<pattern>^<replacement>^ replace pattern and execute previous
command
> CTRL+A Beginning of line
> CTRL+E End of line

Sun Proprietary/Confidential: Internal Use Only


Shell Features: File Completion
• Bash
> Type partial filename and press TAB to fill unique match
> Type TAB TAB to list out all multiple matches.
• Ksh
> Vi mode: Type \
> EMACS mode: Type ESC ESC
> EMACS mode: Type ESC= to list multiple match
• Csh, Tcsh
> Type ESC to fill unique match.
> Type CTRL+D to list out all multiple matches.

Sun Proprietary/Confidential: Internal Use Only


Shell Features: Job Control (1)
• Background and Foreground processes or jobs
• Foreground: Execute a command and wait for completion,
prompt does not come back till command completes.
• Background: Execute a command and continue doing other
things since prompt is available, E.g.:
> prmpt> find /home -name “mfile” > results &
[1] 566
> prmpt>
> prmpt> jobs --> display current background jobs
> [1] + Running

Sun Proprietary/Confidential: Internal Use Only


Shell Features: Job Control (2)
• Continued...
> prmpt> fg --> bring job to foreground
> find /home -name “mfile” > results
• Sending foreground job to background
> CTRL+Z stop foreground job
> Execute bg to continue job execution in background
• Exit codes are important.

Sun Proprietary/Confidential: Internal Use Only


Shell Features: Redirection
• Input/Output Redirection using “<” and “>”.
• Send and receive output/input to or from files.
> ls -l > /tmp/list
> cat < /tmp/list
• Stdout, Stdin and Stderr are standard concepts of
UNIX. Stderr can also be redirected.
> ls -junk 2>/tmp/err
> ls -lR / > /tmp/biglist 2>&1 – Redirect stdout and
stderr to the same file.

Sun Proprietary/Confidential: Internal Use Only


Shell Features: Redirection
• Input/Output Redirection using “<”, “>”, “<<” and
“>>”.
• Send and receive output/input to or from files.
> ls -l > /tmp/list
> cat < /tmp/list
• Stdout, Stdin and Stderr are standard concepts of
UNIX. Stderr can also be redirected.
> ls -junk 2>/tmp/err
> ls -lR / > /tmp/biglist 2>&1 – Redirect stdout and
stderr to the same file.
Sun Proprietary/Confidential: Internal Use Only
Shell Features: Pipes
• Input/Output Redirection using “|”.
• Send and receive output/input between multiple
commands, E.g.:
> ls -l | grep doc
• THE Most Powerful feature in UNIX. Build up
complex functionality from simpler commands.
ls -l grep doc

<stdout> <stdin>

Sun Proprietary/Confidential: Internal Use Only


Shell Features: Here Document
• An extension of Input Redirection: Automatically create a temporary
file, with some predefined content and provide that via input
redirection to a command.
• Example:
cat << THEEND
This is a multi-line message used to
illustrate the usage of a strangely named
Shell feature called “Here Document”
THEEND
• This will output the entire text as-is
• Can be used to dynamically generate scripts in installers.

Sun Proprietary/Confidential: Internal Use Only


Shell Variables
• Variables are of three types: Environment variables, ordinary
variables and special variables
• Environment variables are set using “export” command: export
PS1 makes PS1 an environment variable
• Environment variables are passed to child processes. In a C
program, you can use the “getenv()” function to retrieve the
value of an environment variable.
• Ordinary variables are valid only in the current shell or shell
script context.
• Shell variables are always untyped strings.

Sun Proprietary/Confidential: Internal Use Only


Common Shell Variables
• HOME – The user's home directory
• PS1 – The Shell prompt appearance
• PATH – Contains the “:” separated list of pathnames to search for a
command.
• DISPLAY – If you are using a GUI desktop, this contains the X11 display
number.
• LANG – Identifies the current language (for localization).
• SHELL – Name of the shell that is executing.
• TERM – The type of the current terminal (e.g. xterm, vt100, ansi)
• HOST – Name of the machine where this shell is running.
• PAGER – Comand to use for multi-page output: less, more

Sun Proprietary/Confidential: Internal Use Only


Special Shell Variables
• $? - Exit code of the previous command
• $$ - Process ID of the current shell
• $# - Number of positional parameters
• $* - Complete list of command line arguments to the shell or shell
script
• $0 - Name of the current executable: either shell binary or script
• $! - Process ID of the last command executed in background
• $1 .. $9 – Arguments/parameters to shell or function

Sun Proprietary/Confidential: Internal Use Only


Commonly Used Commands
• pwd, Present Working Directory – What is my current directory
• ls, List – List out files in the current or given directory
• ps, Process Status – Display processes for current or all users
• echo – Print a line onto the terminal
• cat – Print the contents of a file or the stdin to the terminal
• who – List out all the logged in users on the system
• find – Least understood, most useful. Search for files matching a given name or
pattern. KDE has nice GUIs for find.
• more, less – Display lots of scrolling text as multiple pages.
• man – Need help ? Execute “man <command name>”
• id – Display the name and numeric id of the current user.
• grep/egrep – Match regular expression patterns

Sun Proprietary/Confidential: Internal Use Only


Shell Scripting (1)
• Shell commands can be grouped together into a file and
executed at once.
• Shell also provides control-flow:
> If .. then ... elif .. then ... fi
> While .. do ... done
> for ... do ... done
> case ... esac
> functions
> break
> Exit
• Spaces are dreadfully important in shell scripts

Sun Proprietary/Confidential: Internal Use Only


Shell Scripting (2)
• Conditional Statement
> E.g.
ls /dir | grep doc
if [ $? -eq 0 ] <-- This is a test expression
then
echo “Doc files found”
fi
> Test expressions are used everywhere in shell scripts.
> To learn more: man test
> Consider the example carefully all the spaces and newlines are
required!
> Var assignment: TESTVAR=”Value” NOTE: No spaces
around “=”

Sun Proprietary/Confidential: Internal Use Only


Shell Scripting (3)
• Loops
count=0
while [ $counter -lt 10 ]
do
echo “Counter: $count” <- Variable substitution
count=`expr $count + 1` <- Arithmetic expr
done
> Note the backticks: `...` Execute command and return
command output
> ksh supports inline arithmetic so to increment counter:
count=$((count + 1))
> Command substitution in ksh is similar:
list=$(ls /)

Sun Proprietary/Confidential: Internal Use Only


Shell Scripting (4)
• Loops
> Eg2, list all the files but not directories:
for name in `ls /`
do
if [ -f /$name ] <- Note test for file
then
echo “File Name: $name”
fi
done
> The for loop iterates through a list of values
> So this is also valid:
for id in user1 user2 user3
do ...

Sun Proprietary/Confidential: Internal Use Only


Shell Scripting (5)
• Case
> E.g.:
user=`whoami`
case $user in
user1*) echo “Welcome user1”
;;
user2*) echo “You are not welcome!”
;;
esac
> Case is similar to the switch statement in C
> Case takes a value and matches with several patterns

Sun Proprietary/Confidential: Internal Use Only


Shell Scripting (6)
• Case
> E.g.:
user=`whoami`
case $user in
user1*) echo “Welcome user1”
;;
user2*) echo “You are not welcome!”
;;
esac
> Case is similar to the switch statement in C
> Case takes a value and matches with several patterns

Sun Proprietary/Confidential: Internal Use Only


Shell Scripting (7)
• Functions
askyn () {
ANSWER=""; msg=$1
try=0; yn="x"
while [ "$yn" != "y" -a "$yn" != "Y" -a "$yn" != "n" -a "$yn" != "N" ]
do
if [ $try -eq 0 ]; then
/usr/bin/echo "$msg (Type y or n and press <enter>): \c"
else
/usr/bin/echo "Please enter either y or n: \c"
fi
read yn
try=`expr $try + 1`
done
ANSWER=$yn
}

> Guess what the above function does!


> Function parameters are positional parameters: $1 .. $9

Sun Proprietary/Confidential: Internal Use Only


References
• Unix Programming Environment by Kernighan & Pike
• Unix Manual (man pages)
• http://www.sun.com/bigadmin/
• http://www.opensolaris.org/
• http://www.belenix.org/
• Free packages and utilities for Solaris/OpenSolaris
> http://blastwave.org/
> http://www.sunfreeware.com/
• http://www.sun.com/bigadmin/scripts/
• http://forum.vtu.ac.in/ ---To reach us.

Sun Proprietary/Confidential: Internal Use Only


Sun Proprietary/Confidential: Internal Use Only
Next session

UNIX software and programming

Presenter: Tirthankar Das

Sun Proprietary/Confidential: Internal Use Only


Thanks !

You might also like