cpelecii prelim lecture 1 part 1 - basic linux shell concepts
Post on 03-Apr-2018
217 Views
Preview:
TRANSCRIPT
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 1/32
Describe the Linux Shells
Use the Linux file-system
Create a Shell Script
Use metacharacters and brace expansion
Use I/O redirection
Use pipes and tees
Group commands
Run background processes
Use Bash Shell job control
Use command line recall and editing
To review basic Shell concepts in order to:
Objectives
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 2/32
What is a Shell ?
User interface to LinuxCommand interpreter
Programming language
Linux Shells:Bash V1 - bash
Public Domain Korn - ksh
Bourne/System V - ash
C compatible - tcshDefault - sh link to bash in RH 6.2
Remote - rsh
Bash V2 - bash2
Standalone - sash
Shells
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 3/32
The file-system comprises directories in a hierarchical structure
usr var
/
tmphome
log
etc
spoolchrisphil
bin sbin
Directories
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 4/32
Command: Argument Function:
mkdir directory Create new directory "directory"
rmdir directory Delete empty directory "directory"
rm -r directory Delete directory "directory" and any sub-directories
cd directory Change working directory to "directory"
ls -ld directory Give a long listing of "directory" - showspermissions, owner, etc.
pwdPrint working directory - where you are in the treenow
mv old new Rename a file or directory - "new" can be a new filename, or a directory in which to place the file
cp old new Copies a file to a new name
ln name copy Creates another name without copying the contents
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 5/32
Definition:
collection of data, located on a portion of a disk.
stream of characters or a "byte stream".
No structure is imposed on an ordinary file by the operatingsystem.
Examples:
Binary executable code – /bin/bash
Text data – /etc/passwd
C program text – /home/phil/prog.cDevice special file – /dev/null
Directory special file – /home
$ file filename – to find out which file type
A File
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 6/32
Should be descriptive of the content
Are case-sensitive
Should use only alphanumeric characters:
UPPERCASE lowercase digits
# . @ - _
Should not begin with "+" or "-" sign
Should not contain embedded blanks or tabs
Should not contain shell "special" characters:
$ | * ? > < / ; & ! ~
[ ] \ ' " ` { } ( )
Linux File Names
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 7/32
A readable text file which can be edited with a text editor
/usr/bin/vi shell_prog
Anything that you can do from the Shell prompt
A program, containing:
System commands
Variable assignments
Flow control syntax
Shell commands
and Comments !
What is a Shell Script?
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 8/32
runs commands in a Shell
$ bash
$ bash -c commands
$ bash -posix
$ exec bash
begins a new Shell,interrupting the current one
starts a POSIX compatible Shell
terminates the current Shell and
replaces with new Shell
waiting Shell -bash
bash
-bash bash
terminated Shell
new Shell
Invoking Shells
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 9/32
run prog in a new Shell
$ . prog
$ bash prog
$ prog
$ exec prog
prog run (sourced) in current Shell environment
run in a new Shell if prog is executable
run in a new Shell to replace the
current one
waiting shell -bash bash
-bash bash
terminated shell
prog
-bash prog
prog
Invoking Scripts
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 10/32
Exact behavior on invocation depends onlogin or non-login
interactive or non-interactive
Invoking the Bash Shell sources:
Each new explicit non-interactive Bash Shell sources the ENV file again
time
/etc/profile Sourced by login Shells
.bash_profile Login Shells source the first file found
.bash_login in this list from the user's home
.profile directory
BASH_ENV file A resource file listed in the ENVor ENV file or BASH_ENV Environment Variablewill be sourced by non-interactiveBash Shells
Bash Shell Configuration Files
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 11/32
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 12/32
range list of all upper case letters
all lower case letters: a, b, c,... z
digits: 0, 1, 2,... 9
spacing characters: tab, space, etc.
Character Equivalence Classes can be used in place of range lists,to avoid National Language collation problems:
Match any number of any characters
Match any single character
Match a single character from the bracketed list
Match any single character except those listed
Inclusive range for a list
*
?
[abc]
[!az]
[a-z]
[[:upper:]][[:lower:]]
[[:digit:]]
[[:space:]]
-
-
-
-
-
--
-
-
Metacharacters that form patterns that are expanded into matching filenamesfrom the current directory
Wildcard Metacharacters
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 13/32
usr var tmphome
log
etc
spoolchrisphil
bin
/
data_file table1shell_progscript3script2diary
Sample Directory
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 14/32
$ rm d*y removes the diary file
$ file script* identifies script2 and script3
$ head script[345] displays the top lines of script3
$ less script[3-6] displays script3 screen by screen
$ tail script[!12] displays the last lines of script3
Now your turn
$ touch ?a*
$ more [st][ah]*
$ lpr [a-z]*t[0-9]
Expansion Examples
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 15/32
Stops normal Shell metacharacter processing, includingmetacharacter expansion
To form strings
"double quotes" group characters into a string,and allow variable andcommand substitution
To form literal strings
'single quotes' remove any special meaningfor the characters within them
For a literal character
\character removes the special meaning
of the character following the \
Quoting Metacharacters
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 16/32
Brace Expansion
Bash allows an additional expansion facility
Brace ( {} ) expansion
Names may be formed by combining the characters withinbraces with those outside – in order
For example, to find all file names ending 1 or 2
$ ls *{1,2}
script2 table1
$ _
Brace expansion may be disabled
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 17/32
Every process has a file descriptor table associated with it
ProcessInput(0) (1)
(2)Output
Error
Standard in - keyboard
Standard out - screen
Standard error - screen
Defaults
User-
defined{{
0<
1>
2>3
File descriptor table
Process I/O
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 18/32
Redirecting standard input from a file: <command < filename
$ mail terry
Subject: Hello
A letter to see if you are still with us.<Ctrl-d>
Cc:
$ _
$ mail -s "Hello" terry < letter
$ _
Input may also be given inline. This is called a HERE document.
$ mail -s "Hello" terry << END
A letter to see if you are still with us.
END$ _
Input Redirection
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 19/32
Redirecting standard output to a file: >
command > filename
$ ls /home/chris
data_file script2 script3 shell_prog table1$ _
$ ls /home/chris > listing
$ _ Redirecting standard error output to a file: 2>
command 2> filename
$ cat /home/chris/printout
cat: /home/chris/printout: No such file or directory
$ _
$ cat /home/chris/printout 2> errors
$ _
Output Redirection
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 20/32
Appending standard output to a file: >>
command >> filename
$ wc -l /home/chris/script3
42 /home/chris/script3$ _
$ wc -l /home/chris/script3 >> line_count
$ _Appending standard error output to a file: 2>>
command 2>> filename
$ wc -c /home/chris/characters
wc: /home/chris/characters: No such file or directory
$ _
$ wc -c /home/chris/characters 2>> errors
$ _
Output Appending
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 21/32
File descriptors can be joined, so that they output to thesame place
What do you think this command does?
command > file 2>&1
Redirects standard error to join with standard out
$ cat Message_file 1>&2
01
2
Association
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 22/32
The built-in Shell command exec allows you to
open
associate
close
file descriptors$ exec n> of Opens output file descriptor n to file "of"
$ exec n< if Opens input file descriptor n to read file "if"
$ exec m>&n Associates output file descriptor m with n
$ exec m<&n Associates input file descriptor m with n
$ exec n>&- Closes output file descriptor n
$ exec n<&- Closes input file descriptor n
Setting I/O or File Descriptors
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 23/32
To open file descriptor 3 for output to Phil's out file and 4 to Phil's err file $ exec 3> /home/phil/out
$ exec 4> /home/phil/err
$ date >&3$ ls /home/chris >&4
To associate output to file descriptor 3 with file descriptor 4
$ exec 3>&4
$ wc -l /home/chris/script3 >&3
$ wc -l /home/chris/table1 >&4
To close file descriptors 3 and 4
$ exec 3>&-
$ exec 4>&-
Setting I/O Descriptor Examples
Pi
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 24/32
Commands can be joined, so one inputs into the next
command1 | command2 | command3
Gives a command pipeline
$ ls /home/robin | sort -r | lpr
sorts the file list into reverse order, and prints it
Pipelines may have a branch using the tee commandduplicates the standard input to the branch and to standard out
$ ls /home/francis | tee raw_list | sort -r | lpr
saves the unsorted list in the file raw_list
ls sort lpr0 0 0 111
222
Pipes
C d G i ith ( )
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 25/32
To combine the output of several commands: ( ) or { }
( command ; command ... )
Runs commands in a Sub-Shell
For root to alter Lynn's files:
# ( cd /home/lynn ; chown lynn:bin d* )
leaves the working directory unchanged on completion
waiting shell -bash
( command ; command )
Command Grouping with ( )
C d G i ith {}
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 26/32
{ command ; command ... ; }
Runs commands in the current Shell
Directory (or environment) changes remain in effectMust leave spaces around the braces
Either have the braces on separate lines
or include a final "; " before the closing brace
# { cd /home/lynn ; chown lynn:bin s* ; }
-bash{ command ; command ; }
Command Grouping with {}
B k d P i
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 27/32
Execute command in the background: &
command &
$ sleep 999 &
Waiting for the end
$ date
Fri Dec 31 11:59:59 EST 1999
$ wait
When all background processes have finished
$ _
Background Processing
Bash Shell Job Control
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 28/32
Bash Shell assigns job numbers to background or suspendedprocesses
The jobs command lists your current Shell processes and their jobids
Ctrl-z suspends the current foreground job
bg runs a suspended job in background
fg brings to foreground a suspended or background job
Jobs can be stopped with the kill command
kill, fg and bg work with the following arguments:
pid process id %job_id job id
%% - or - %+ current job
%- previous job
%command match a command name
%?string match string in command line
Bash Shell Job Control
Job Control Example
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 29/32
$ cc -o RUNME program_in.c
...
After some time running this long compilation...Ctrl-z
[2] + 5692 Stopped (SIGTSTP) cc -o RUNME program_in.c
$ jobs+ [2] Stopped (SIGTSTP) cc -o RUNME program_in.c
- [1] Running sleep 999 &
$ bg %+
[2] cc -o RUNME program_in.c
$ jobs+ [2] Running cc -o RUNME program_in.c
- [1] Running sleep 999 &
$ kill %cc
[2] + 5692 Terminated cc -o RUNME program_in.c
$ fg %1sleep 999
Completing the sleep in the foreground
$ jobs$ _
Job Control Example
Command Line Editing and Recall
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 30/32
Bash allows two modes for command line editing and recall:
Emacs (default)
Vi
In emacs mode
Cursor keys are active
Characters are inserted as you typeOther editing commands are available (see bash man page)
To use the vi mode
$ set -o vi
Then simply press ESC to enter vi editing mode
To use emacs mode
$ set -o emacs
Command Line Editing and Recall
Checkpoint
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 31/32
Checkpoint
1. What type of file is /dev/tty3?
2. How could we find out a file type?
3. How can we get .bashrc to run in an explicit Bash Shell?
4. When listing a directory, how can you list only files that startwith an uppercase letter?
5. How can you make sure error messages are not displayedwhen running a command?
6. How do you make the normal output of a command appear aserror output?
7. How can we group commands in order to redirect the standard
output from all of them?
8. What will kill 1 do?
9. If you have submitted a job to run in foreground, how could you
move it to background?
10. How would you change the command line recall facility?
Summary
7/29/2019 CpELECII Prelim Lecture 1 Part 1 - Basic Linux Shell Concepts
http://slidepdf.com/reader/full/cpelecii-prelim-lecture-1-part-1-basic-linux-shell-concepts 32/32
Linux ShellsHierarchical file-system
File names and types
Shell Scripts
Invoking Shells
Shell metacharacters: expansion and quoting
Brace expansion
< and << input redirection> and >> output redirection
2> and 2>> error redirection
Setting file descriptorsPipes and tees
Command grouping
Background processes
Bash Shell job control and command line editing
Summary
top related