Environment variables - How to use and assign them
- Your PATH variable
Introduction to shell programming - Permissions and making your file executable
- Input to and Output from shell scripts
- Control structures
- Booleans - test
- Controlling input from within the shell
Environment variables are pieces of information used by the shell and by other programs Environment variables are pieces of information used by the shell and by other programs Useful for customising your working environment and for shell programming Some examples: - PATH - the directories the system searches to execute commands
- TERM - The type of terminal (most commonly xterm and vt100)
- HOME - Your home directory
- PS1 – The format of the prompt
Conventionally written using all capitals Conventionally written using all capitals To use, precede by a $ symbol E.g. to find the value of a variable - echo $VAR
- [zlizmj@unnc-cslinux ~]$ echo $HOME
- /home/domain/zlizmj
Variables are expanded by the shell before the command is executed
Environment variables can be used by any program
VAR=value VAR=value The change is only visible in the current shell Child processes don’t automatically inherit the environment variables from their parent. We use export to let child processes get the changed value. To add something to your PATH: - export PATH=$PATH:new stuff
Your PATH tells UNIX where to look for executables Your PATH tells UNIX where to look for executables You will have a PATH already set up by default. - You can alter it or add to it
When you type the name of a program, the shell will look for it in your PATH. Each directory on your PATH should be separated by a colon If you change your PATH in one window it is changed only in that window (since PATH is just an environment variable)
In UNIX you type commands at the keyboard and the system responds In UNIX you type commands at the keyboard and the system responds In UNIX this is a separate program. There are different versions of this program to suit the taste of the user. We use bash (Bourne again shell) - … but it is possible to program in other shells and use your existing shell to interpret it
In this lecture you will learn about Bourne shell scripting (sh), a predecessor of bash.
The shell is a command interpreter The shell is a command interpreter - It reads commands and then executes them
It can work interactively or from a text file A shell program is simply a text file which contains commands you would normally type at the prompt The only difference is that the commands are executed in a sub-shell (a child process is created).
The first line (for bourne shell) is usually The first line (for bourne shell) is usually #!/bin/sh # is also used for comments Hence, this line is a special kind of comment: it tells the shell which program to use to execute the commands in the file
Create the text file (using Emacs) Create the text file (using Emacs) Make it executable (optional): chmod u+x filename Run it: - filename - only works if file is executable and your PATH is set correctly!
- /Path/to/executable/filename if filename is executable.
- sh filename if you haven’t made it executable
- . filename or source filename if you want to execute it without creating a sub-shell
ls -l tells you if files are readable, writable and/or executable and by whom ls -l tells you if files are readable, writable and/or executable and by whom You can change these permissions by using chmod
who is one of u (user), g (group) or o (other) who is one of u (user), g (group) or o (other) ? is one of + (add a permission) or - (remove a permission) what is one of r (read permission), w (write permission) or x (execute permission)
chmod o-r coursework - prevents others from reading your file
chmod u+x shellscript - makes the file executable by you alone
chmod a+x directory - makes the directory accessible by everyone (a = all user, group and others)
chmod +x directory - We can omit the a (it’s the default)
Each command appears on a separate line Each command appears on a separate line
The first argument to a shell script is called $1 The second argument to a shell script is called $2 ….etc… Shell uses echo like Java’s println
Control structures are built in syntax for controlling the order in which execution happens Control structures are built in syntax for controlling the order in which execution happens Common structures are conditionals (if-then-else) and loops (for loops) Keywords should appear at the start of a line
if needs something true or false if needs something true or false Often this means you want to compare things This is more complicated in shell than in most languages Need to use test if test $1 -ge $2 - succeeds if the first argument is “greater than or equal to the second”
if test $1 = $2 if test $1 = $2 - if $1 is equal to $2 (for strings)
if test $1 -eq $2 - if $1 is equal to $2 (for numbers)
-ge -gt if test -f $FILE - if $FILE exists and is a normal file
You don’t have to use “test” You don’t have to use “test” - if [$1 -ge $2]
- Is just syntactic sugar for
- if test $1 -ge $2
To learn more about test:
NOTE: There are more complex forms of loop in Bash.
Generally this script will look in the current directory: Generally this script will look in the current directory:
# set up personal bin directories # set up personal bin directories PATH=$HOME/bin:$PATH: EDITOR=emacs export PATH EDITOR
You can use environment variables in shell scripts just like you can at the command line. You can use environment variables in shell scripts just like you can at the command line. - Environment variables are typically written in upper case and are accessed using a dollar symbol
You might also want to use other variables in shell scripts - These are typically written in lower case and are accessed using a dollar symbol
To get input use read followed by a variable
read can have more than one argument read can have more than one argument - e.g. read COMMAND ARGUMENTS
It will bind the first word of input to the first variable and bind the rest to the second This acts like a list or array – so can be used with for
<< tells a command to use input from within a shell script << tells a command to use input from within a shell script Syntax is command << end where end is some string which will tell the command to stop taking input (<< EOF is most common) This is useful when testing programs – you can automatically run them on sample input
Environment Variables Running Shell Programs Command Line Arguments If-then-else and for loops. Controlling Input and Output.
Dostları ilə paylaş: |