Presentation is loading. Please wait.

Presentation is loading. Please wait.

8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.

Similar presentations


Presentation on theme: "8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming."— Presentation transcript:

1 8 Shell Programming Mauro Jaskelioff

2 Introduction 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 If then else For loops –Booleans - test –Controlling input from within the shell

3 Environment Variables 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

4 Using Environment Variables 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

5 Using Environment Variables Environment variables can be used by any program public class EnvDemo1 { public static void main(String args[]) { String s = System.getenv(“PATH"); System.out.println(s); }

6 Assigning Environment Variables at the Prompt 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. export VAR=value To add something to your PATH: export PATH=$PATH:new stuff

7 Your PATH Variable 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)

8 UNIX Command Line In UNIX you type commands at the keyboard and the system responds Every operating system has some sort of command interface 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.

9 What is a Shell Script? 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).

10 Common Shell Script Components 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

11 Writing and Running a Shell Script 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../myscript.sh –sh filename if you haven’t made it executable –. filename or source filename if you want to execute it without creating a sub-shell

12 Making your File Executable ls -l tells you if files are readable, writable and/or executable and by whom You can change these permissions by using chmod [zlizmj@unnc-cslinux ~]$ ls -l Foo -rw-r--r-- 1 zlizmj Domain U 0 Mar 12 10:23 Foo [zlizmj@unnc-cslinux ~]$ chmod who?what filename

13 Chmod Revisited who is one of u (user), g (group) or o (other) –can also have a (all) ? is one of + (add a permission) or - (remove a permission) what is one of r (read permission), w (write permission) or x (execute permission)

14 Examples of chmod 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)

15 A Simple Shell Script Each command appears on a separate line #!/bin/sh ls echo "done " #This is a comment

16 The Simple Shell Script in Action [zlizmj@unnc-cslinux 1]$ ls done.sh [zlizmj@unnc-cslinux 1]$ chmod +x done.sh [zlizmj@unnc-cslinux 1]$./done.sh done.sh done [zlizmj@unnc-cslinux 1]$

17 Input and Output 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

18 Input and Output: An Example [zlizmj@unnc-cslinux 1]$ cat firstarg.sh #!/bin/sh #Print first argument echo $1 [zlizmj@unnc-cslinux 1]$./firstarg.sh hello world hello

19 Control Structures 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

20 Conditionals NOTE: the else is optional… if …. then …. else …. fi

21 Example Using Conditionals #!/bin/sh if javac $1 then echo "compilation done" else echo "compilation failed" fi

22 Booleans 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”

23 Some test inputs 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 –(greater or equal) -gt –(greater) if test -f $FILE –if $FILE exists and is a normal file

24 More test inputs 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: man test

25 For Loops NOTE: There are more complex forms of loop in Bash. for IDEN in list do …. done

26 A Simple For Loop Generally this script will look in the current directory: If you want it to look elsewhere, you need to put in the full path #!/usr/bin/sh # This is list_shell.sh for IDEN in *.sh do echo "$IDEN" done

27 The For Loop in Action $./list_shell.sh done.sh echo_three.sh edit_shell.sh list_shell.sh new.sh simple.sh

28 Your.bash_profile is a shell script # set up personal bin directories PATH=$HOME/bin:$PATH: EDITOR=emacs export PATH EDITOR

29 Variables and Shell 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

30 Interacting with the User To get input use read followed by a variable $./interact.sh please type: hello computer #!/bin/sh # This is interact.sh echo "please type:" read ANS echo $ANS

31 Variables and read 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

32 More Complex read Example #!/bin/sh # This is interact2.sh echo "please type:" read COMMAND ARGUMENTS for ARG in $ARGUMENTS do $COMMAND $ARG done

33 The Example in Action $./interact2.sh please type: cat interact.sh simple.sh #!/bin/sh echo "please type:" read ANS echo $ANS #!/bin/sh echo $1

34 Controlling Input from Within the Shell << 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

35 Example of Input Control $./interact3.sh please type: hello goodbye hello #!/bin/sh # This is interact3.sh./interact2.sh <<EOF echo hello goodbye hello EOF

36 Summary Environment Variables Running Shell Programs Command Line Arguments If-then-else and for loops. Controlling Input and Output.


Download ppt "8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming."

Similar presentations


Ads by Google