![]() The -u creates an error if you try to use a variable which doesn't exist. That produced the error, instead of continuing. Using the -e option, an error anywhere in the file will result in the script stopping at the line Produces an error, the script will carry on regardless to the end of the script. Two new features have also been introduced in this file: The line exit simply makes the script end, so that no more code is run.This only runs the code between the then and fi, if $# (the number of options) is not 2. The whole block of code has the form " if then.A special variable $# has been used, which is the number of options that were given by the user.You can copy this code into the start of any of your scripts, and easily modify it to work for that script. The code looks like this: if Įcho "usage: options_example.3.sh filename number_of_lines"Įcho "Prints the filename, and the given first number of lines of the file" This shows anyone how the script should be run (including you!) without having to look at the code inside the script.Ī basic check for this script would be to verify that two options were supplied, and if not then print a help message. Try running the script with different options and see what happens.Ī convention with scripts is that it should output a help message if it is not run correctly. The wrong number of options are given by the user.The previous scripts will have strange behaviour if the input is not as expected by the script. ![]() Do not type the next command into your terminal. The following command is only here so that this notebook finds scripts correctly and the remaining examples work. ![]() If the file does not already exist, then create it and put that line into it. If you are using a Mac, then the file should instead be ~/.bash_profile. ![]() If you want this change to be permanent, ie so that Unix finds your scripts after you restart or logout and login again, add that line to the end of a file called ~/.bashrc. Unix did not find it! The command to run to add the scripts directory to $PATH is: export PATH=$PATH:~/scripts/ Before we add the scripts directory to this list, check what happens if we try to run the script without telling Unix where it is: hello.sh It returns a list of directories, which are all the places Unix will look for a command. We need to add the new scripts directory to that list of directories. Whenever a command is typed into Unix, it has a list of directories that it searches through to look for the command. The final thing to do is change our setup so that Unix can find the script without us having to explicitly say where it is. First, in your terminal, check that the script is saved in the correct place. This script will print Hello World! to the screen when we run it. Type this into the text editor: echo Hello World!Īnd save this to a file called hello.sh in your new scripts directory. Do not try to use a word processor, such as Word! If you don't already have a favorite, try gedit by running the following command: gedit & What text editors are available will depend on your system. Next open a text editor, which you will use to write the script. We will do this now.įirst, open a terminal and make a new directory in your home called scripts, by typing cd ![]() It is traditional when learning a new language (in this case BASH), to write a simple script that says "Hello World!". This has the advantage of reproducibility, so that the same analysis can be run on many input data sets. These commands can be put into a separate file (ie a script), and then run on the input data. However, it is useful to be able to run multiple commands that process some data and produce output. So far, we have run single commands in a terminal. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |