Simple Scripts and Environment Variables

From docwiki
Jump to: navigation, search


Motivation

One of the main advantages of using CLI instead of GUI is that within the CLI it is much easier to automate tasks. All that is needed is to put the commands that you would normally execute in a text file and tell the system that it is executable. This is not meant to be a full fledged course in shell scripting but should only tell you the very basics.

Building a Simple Script

With your text editor of choice generate a text file with the name myscript.sh (in the example below I write vim - but you can use other text editors as well)

$ vim myscript.sh
$ chmod +x  myscript.sh
$ ./myscript.sh

The above creates the script. (For what you type in the script see below), and sets it to be executable (chmod +x). After that you can run the script. When you want to start a script which is not in the PATH where you would normally have your executable files then you need to give the path name. In our case we take the . as the current directory.

What could be in your script?

#!/bin/bash
echo hello world
echo today is $(date)

The first 2 characters of every script need to be #! (pronounced: hash-bang) and after that the path to the executable which processes the script. So you can write your scripts in other languages simply by changing the interpreter there. E.g. python, perl, etc..

Here is a slightly more complex example:

#!/bin/bash
# a loop over 3 words
for k in ene mene muh ; do
  echo $k
done

Where k is a variable. Here it loops over 3 words. You could also loop over all png files in the directory by using for k in *.png. If you just say for k ; do you would loop over the command line arguments given to the script.

Environment Variables

We have seen in the last example that a variable has been used in the shell. We can assign strings to variables and then access them by referencing them with $NAME or ${NAME}:

$ v=bla
$ zaehler=17
$ echo v is $v and the counter is $zaehler
$ let zaehler=zaehler+1
$ echo test > file${v}-${zaehler}.txt

This would create a file with the name filebla-18.txt The let is useful for simple integer arithmetic.

By default these variables are only seen within this shell. If you start a program you can give it a set of variables that it sees in its environment. All variables that are declared exportable will be passed in the environment of every program that you start from within this shell. You can see the envirnonment with the env command:

$ env
$ export zaehler
$ export BLA=xyz
$ env
$ echo BLA ist $BLA, zaehler ist $zaehler


The first export exports the zaehler variable form the example above. The 2nd defines a new variable bla to be exported and assigns it a value. Of course you can still access the variables from your own shell, as verified by the echo statment.

There are a lot of useful environment variables the shell uses. Investigate them by using the env command and see what is there.

$ echo $PATH


PATH tells the system where to search for executable files when you type a command. The directory entries are separated by colon :.

Exercises

  • try to write a short script with a few commands. Make it executable and execute it.
  • try to write scripts that loop over arguments and call the script with arguments.
  • look at your environment variables.