additional unix commands. 222 lecture overview multiple commands and job control more useful unix...
TRANSCRIPT
Additional UNIX Commands
222
Lecture Overview
Multiple commands and job control
More useful UNIX utilities
333
Executing Multiple Commands
Several commands can be executed in a single line, by separating them with ';'s:
To run a set of commands in a sub-shell, enclose them within brackets:
set a = /home ; echo $a/demo
/home/demo
( set a = /home ; echo $a/demo )
444
Executing Commands in a Sub-shell – Example
pwd
/home/demo
(set p = /etc; cd $p ; pwd ; ls -l passwd)
/etc-rw-r--r-- 1 root root 15692 Dec 3 23:12 passwd
pwd
/home/demo
echo $p
p: Undefined variable.
555
The sleep Command
The sleep command just does nothing for the given number of seconds:
Can be useful for interactive scripts
We will use it to demonstrate job control
sleep number
666
Job Control
A job is any command line that can be executed by the shell
Multiple jobs can be run concurrently The shell allows you to manage jobs:
Place a job in the background Move a job to the foreground Suspend a job Kill a job
777
Running Jobs in the Background
Typing any command, followed by the '&' symbol, will cause the job to be run in the background The job number is printed out for reference
When a job is running in the background, you may continue to type new commands
A message is displayed when the job is complete
888
Running Multiple Jobs
The jobs command will list all current job
sleep 10 &
[1] 3215
(sleep 5 ; pwd ; sleep 5) &
[2] 3216
jobs
[1] + Running sleep 10[2] - Running ( sleep 5; pwd; sleep 5 )
999
Running Multiple Jobs
After 5 seconds, the following is printed:
After a few more seconds:
And finally:
/home/demo
[1] Done sleep 10
[2] Done ( sleep 5; pwd; sleep 5 )
101010
Suspending or Killing the Foreground Job
Pressing Ctrl-Z while some job is running in the foreground will stop its execution The job stops, but is not dead The suspended job will show up in the jobs
output, with a status of suspended
Pressing Ctrl-C will completely terminate (or 'kill') the foreground job
The kill command will kill a specific job
111111
Moving Jobs Between Background and Foreground
The bg command starts running the specified job in the background The job number should be preceded by a '%'
The fg command is used to move any background job to the foreground The job will start to run in the foreground,
whether it was running or suspended
121212
Jobs – Example
We type the following two commands, and press Ctrl-Z immediately after each one:
sleep 10
Suspended
(sleep 5 ; pwd ; sleep 5)
Suspended
jobs
[1] + Suspended sleep 10[2] - Suspended ( sleep 5; pwd; sleep 5 )
131313
Jobs – Example
Now, we start running the second job in the background:
To move the first job to the foreground:
bg %2
[2] ( sleep 5; pwd; sleep 5 ) &/home/demo
fg %1
Sleep 10
141414
Lecture Overview
Multiple commands and job control
More useful UNIX utilities
151515
More Useful Utilities
The UNIX operating system has many other commands
We will briefly review several additional ones now, but you are encouraged to investigate and learn: More commands Additional options and uses for the commands
that we have learned
161616
Simple Loop – repeat
The repeat command executes any command multiple times
For example:
repeat count command
repeat 3 echo Hello
HelloHelloHello
171717
Date and Time – date
The date command can be used to print the current date and time:
The format argument can be used to control the display of the current time
It may contain literal characters, or special sequences starting with '%'
date [options] [+format]
181818
date – Examples
For example, '%A' is the weekday, '%Y' is the current year, etc.
date +%D
12/04/05
date +"%A, %B %d"
Sunday, December 04
date +"Date: %A, %B %d, %Y; Time: %l:%M"
Date: Sunday, December 04, 2005; Time: 4:26
191919
Getting User Information
Several UNIX commands allow us to get information about other users currently logged-on to the system: who – Shows who is logged on w – Shows who is logged on, and also what they
are doing finger – Provides various information about
users currently logged-on
202020
Splitting Data Streams
The tee command takes input from the standard input, and writes it both to the standard output, and to a list of files:
The following will list all files in the current directory into the file 'all.txt' , and also print a count of all '.c' files in the directory
tee [options] files
ls | tee all.txt | grep '.c' | wc -l
212121
The find command
The find command searches for files whose name matches a pattern
The most common use of find:
Finds all files that match pattern in directory or in any of its sub-directories
find has many other options and uses
find directory -name pattern
222222
find – Example
find can execute any command
One common use is to execute the grep command on all files:
This will traverse the current directory and all of its sub-directories, and search for the string 'Hello' in each simple file
find . -type f -exec grep -H "Hello" '{}' \;