shell_handout.pdf

(20 KB) Pobierz
Overview
By the end of this lecture you should...
What are shell scripts?
Creating and running shell scripts.
Shell Programming 1
Know what a shell program is
Using Variables.
Be able to run a shell program
The .profile file.
Be able to write simple shell programs
Input and Output.
You can download all the sample programs from in this lecture from the course home
page.
Louise Dennis
–1–
Dept. Computer Science and IT
Louise Dennis
–2–
Dept. Computer Science and IT
Louise Dennis
–3–
Dept. Computer Science and IT
What is a Shell Script
UNIX Command Line
The shell is a command interpreter – it read commands and then executes them.
Why use Shell?
In UNIX you type commands at the keyboard and the system responds.
It can work interactively or from a text file.
Every operating system has some sort of command interface.
A shell program is simply a text file which contains commands you would normally
type at the prompt.
It is always there!!
In UNIX this is a separate program. Shells are different versions of this program.
Anything you can type in a shell script you can type at the command line.
Originally there were two shells, sh and csh .
Major differences between shell scripts and other files:
First line is usually
#!/usr/bin/sh
– NB. # is also used for comments.
It is normally executable.
Lots of legacy code is written in Shell.
sh (by Bourne) was best for programming – writing shell scripts which executed a
sequence of commands.
Its concepts underpin many other scripting languages
csh (by Joy) was best for interactive work
Lots of UNIX applications make more sense if you know a bit of Shell.
bash (Bourne again shell) incorporates combines aspects of both and is a successor
of sh .
MOST IMPORTANT: A shell script is some “glue” with which you stick together
other program.
Louise Dennis
–4–
Dept. Computer Science and IT
Louise Dennis
–5–
Dept. Computer Science and IT
Louise Dennis
–6–
Dept. Computer Science and IT
801954430.107.png 801954430.118.png 801954430.129.png 801954430.139.png 801954430.001.png 801954430.012.png 801954430.023.png 801954430.033.png 801954430.044.png 801954430.055.png 801954430.064.png 801954430.065.png 801954430.066.png 801954430.067.png 801954430.068.png 801954430.069.png 801954430.070.png 801954430.071.png 801954430.072.png 801954430.073.png 801954430.074.png 801954430.075.png 801954430.076.png 801954430.077.png 801954430.078.png 801954430.079.png 801954430.080.png 801954430.081.png 801954430.082.png 801954430.083.png 801954430.084.png 801954430.085.png 801954430.086.png 801954430.087.png
 
The Simple Shell Script in Action
Using a Shell Script
A Simple Shell Script
[lad@bartok examples]$ ls
done.sh
[lad@bartok examples]$ chmod u+x done.sh
[lad@bartok examples]$ done.sh
done.sh
done
[lad@bartok examples]$ sh done.sh
done.sh
done
[lad@bartok examples]$
Create the text file.
#!/usr/bin/sh
Make it executable (optional): chmod u+x filename
ls
echo "done"
Each command appears on a separate line.
Run it
sh filename
filename – only works if file is executable and your PATH is set correctly.
Louise Dennis
–7–
Dept. Computer Science and IT
Louise Dennis
–8–
Dept. Computer Science and IT
Louise Dennis
–9–
Dept. Computer Science and IT
Quick Quiz
Variables and Assignment
Some Fundamentals of Programming (Revision)
Shell commands are the same as UNIX commands so:
1. Write a shell script to print a file called shell1.pdf
2. Write a shell script to change the name of a file called shell1.pdf to a file called
simple shell.pdf
3. Write a shell script to list the files a directory and redirect the output to a file
ls.txt
4. Write a shell script to make the file, done.sh executable.
Answers handed to me at the end of the lecture with your name and CS login on them
will be eligable for the course prize.
Answers to the quiz will go up on the website after the lecture.
Assigned by = – there must be NO SPACES round the = symbol.
TMP FILE=/tmp/junk
Assignment – giving a variable a value
Input and Output.
Called by $VarName
lpr $TMP FILE
Conditionals – if-then-else and case
Loops – for, while and do
For small programs things like objects classes and methods get in the way. Shell should
only be used for small programs.
To set the variable so its available to other shell scripts and at the command line –
i.e. make it an Environment Variable use export :
export TMP FILE
Louise Dennis
–10–
Dept. Computer Science and IT
Louise Dennis
–11–
Dept. Computer Science and IT
Louise Dennis
–12–
Dept. Computer Science and IT
801954430.088.png 801954430.089.png 801954430.090.png 801954430.091.png 801954430.092.png 801954430.093.png 801954430.094.png 801954430.095.png 801954430.096.png 801954430.097.png 801954430.098.png 801954430.099.png 801954430.100.png 801954430.101.png 801954430.102.png 801954430.103.png 801954430.104.png 801954430.105.png 801954430.106.png 801954430.108.png 801954430.109.png 801954430.110.png 801954430.111.png 801954430.112.png 801954430.113.png 801954430.114.png 801954430.115.png 801954430.116.png 801954430.117.png 801954430.119.png 801954430.120.png 801954430.121.png 801954430.122.png 801954430.123.png 801954430.124.png 801954430.125.png 801954430.126.png 801954430.127.png 801954430.128.png 801954430.130.png
 
Some Details
Your .profile is a shell script
Input and Output
If you want to use whitespace then use "
MESSAGE="Program ends OK"
# set up personal bin directorys
PATH=$HOME/bin:$PATH:
EDITOR=emacs
LASER=het
export PATH TERM EDITOR LASER
DEFTERM=vt100
ASKTERM=false
The first argument to a shell script is called $1 .
If you actually need a $ sign use n $
echo "A meal in USA costs n $"
The second argument to a shell script is called $2 .
... and so on.
Similarly you can us n instead of quotes
MESSAGE=Program n ends n OK
Shell uses echo like Java’s println
Louise Dennis
–13–
Dept. Computer Science and IT
Louise Dennis
–14–
Dept. Computer Science and IT
Louise Dennis
–15–
Dept. Computer Science and IT
More Simple Shell Programs
Special Input Variables
Input and Output: An Example
#!/usr/bin/sh
$# Records the number of arguments passed to the shell, not counting the first
command.
simple.sh a b c sets $# to 3.
One of its primary uses is to check that enough arguments have been specified
#!/usr/bin/sh
echo $#
echo $1
[lad@bartok examples]$ count_input.sh
0
[lad@bartok examples]$ sh count_input.sh hello goodbye hello
3
[lad@bartok examples]$ sh count_input.sh hello goodbye hello pink
4
$* All the arguments given to the shell
Useful in loops when you want to do something to every input.
[lad@bartok bin]$ simple.sh hello
hello
Louise Dennis
–16–
Dept. Computer Science and IT
Louise Dennis
–17–
Dept. Computer Science and IT
Louise Dennis
–18–
Dept. Computer Science and IT
801954430.131.png 801954430.132.png 801954430.133.png 801954430.134.png 801954430.135.png 801954430.136.png 801954430.137.png 801954430.138.png 801954430.140.png 801954430.141.png 801954430.142.png 801954430.143.png 801954430.144.png 801954430.145.png 801954430.146.png 801954430.147.png 801954430.148.png 801954430.149.png 801954430.002.png 801954430.003.png 801954430.004.png 801954430.005.png 801954430.006.png 801954430.007.png 801954430.008.png 801954430.009.png 801954430.010.png 801954430.011.png 801954430.013.png 801954430.014.png 801954430.015.png 801954430.016.png 801954430.017.png 801954430.018.png 801954430.019.png 801954430.020.png 801954430.021.png 801954430.022.png
 
Examples of semi-colons and backlashes
Sequences at Work
Sequences of Commands
#!/usr/bin/sh
#!/usr/bin/sh
Commands are generally terminated by end-of-line.
echo $1
echo $2; echo \
$3
emacs $1
chmod u+x $1
Several commands can be on one line, separated by semi-colons.
To spread a command over more than one line, end the first line with an escape n .
[lad@bartok examples]$ edit_shell.sh new.sh
[lad@bartok examples]$ ls -lt
total 52
-rwxr--r--
Leading white space (e.g. tabs) is ignored.
[lad@bartok examples]$ sh echo_three.sh red green blue
red
green
blue
Arguments to commands are separated by white space.
1 lad
staff
8 Nov 26 16:10 new.sh
[lad@bartok examples]$
Louise Dennis
–19–
Dept. Computer Science and IT
Louise Dennis
–20–
Dept. Computer Science and IT
Louise Dennis
–21–
Dept. Computer Science and IT
Examples
#!/usr/bin/sh
Summary
Quotes
FILE=hello1.java
echo ’ls *.sh $FILE’
echo "ls *.sh $FILE"
echo ls *.sh $FILE
echo ‘ls *.sh $FILE‘
Variables: $NAME
Single Quotes Treat as a string.
Double Quotes evaluate variables
No Quotes evaluate variables and wild cards
Back Quotes treat as a command
Assignment: =
Input Arguments: $1 , $# , $* .
Output: echo .
[lad@bartok lad]$ quotes.sh
[lad@bartok examples]$ quotes.sh
ls *.sh $FILE
ls *.sh hello1.java
ls edit_shell.sh list_shell.sh make_executable.sh quotes.sh hello1.java
edit_shell.sh hello1.java list_shell.sh make_executable.sh quotes.sh
Sequences of commands.
Louise Dennis
–22–
Dept. Computer Science and IT
Louise Dennis
–23–
Dept. Computer Science and IT
Louise Dennis
–24–
Dept. Computer Science and IT
801954430.024.png 801954430.025.png 801954430.026.png 801954430.027.png 801954430.028.png 801954430.029.png 801954430.030.png 801954430.031.png 801954430.032.png 801954430.034.png 801954430.035.png 801954430.036.png 801954430.037.png 801954430.038.png 801954430.039.png 801954430.040.png 801954430.041.png 801954430.042.png 801954430.043.png 801954430.045.png 801954430.046.png 801954430.047.png 801954430.048.png 801954430.049.png 801954430.050.png 801954430.051.png 801954430.052.png 801954430.053.png 801954430.054.png 801954430.056.png 801954430.057.png 801954430.058.png 801954430.059.png 801954430.060.png 801954430.061.png 801954430.062.png 801954430.063.png
 
Zgłoś jeśli naruszono regulamin