Exercise sheet 1 To be corrected in tutorials in the week from 23/10/2017 to 27/10/2017

Similar documents
Chapter 4. Unix Tutorial. Unix Shell

Linux Shell Script. J. K. Mandal

9.2 Linux Essentials Exam Objectives

Introduction to Linux for BlueBEAR. January

Introduction: What is Unix?

A Brief Introduction to the Linux Shell for Data Science

LING 408/508: Computational Techniques for Linguists. Lecture 5

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Exercise 1: Basic Tools

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

1 Installation (briefly)

Introduction to Linux

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Using the Zoo Workstations

LOG ON TO LINUX AND LOG OFF

Introduction to remote command line Linux. Research Computing Team University of Birmingham

Week - 01 Lecture - 04 Downloading and installing Python

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Practical Session 0 Introduction to Linux

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Introduction to Unix - Lab Exercise 0

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Intro to Linux. this will open up a new terminal window for you is super convenient on the computers in the lab

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

Command Line Interface The basics

Lec 1 add-on: Linux Intro

CS Fundamentals of Programming II Fall Very Basic UNIX

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

Introduction to UNIX I: Command Line 1 / 21

Introduction to Linux

When you first log in, you will be placed in your home directory. To see what this directory is named, type:

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

Linux Exercise. pwd answer: We call this directory (into which you get when you log in) your home directory.

Chapter-3. Introduction to Unix: Fundamental Commands

Getting Started With UNIX Lab Exercises

Introduction to the UNIX command line

This tutorial will guide you how to setup and run your own minecraft server on a Linux CentOS 6 in no time.

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

Introduction to Linux

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

Part I. Introduction to Linux

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Mills HPC Tutorial Series. Linux Basics II

Perl and R Scripting for Biologists

Mills HPC Tutorial Series. Linux Basics I

COMS 6100 Class Notes 3

mkdir Phys338 s2017 Draw the tree of the directories and files for this step and all the following cd Phys338 s2017

Linux at the Command Line Don Johnson of BU IS&T

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

Shell Programming (Part 2)

1 Getting Started with Linux.

Introduction to Linux Workshop 1

EE516: Embedded Software Project 1. Setting Up Environment for Projects

Tutorial 1: Unix Basics

Linux File System and Basic Commands

Basic Linux (Bash) Commands

Useful Unix Commands Cheat Sheet

CISC 220 fall 2011, set 1: Linux basics

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

One of the hardest things you have to do is to keep track of three kinds of commands when writing and running computer programs. Those commands are:

Unix basics exercise MBV-INFX410

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Introduction to Linux Environment. Yun-Wen Chen

Using echo command in shell script

Overview LEARN. History of Linux Linux Architecture Linux File System Linux Access Linux Commands File Permission Editors Conclusion and Questions

Introduction to the Linux Command Line

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

Introduction to Unix and Linux. Workshop 1: Directories and Files

Basic Survival UNIX.

CENG393 Computer Networks Labwork 1

Vi & Shell Scripting

GNU/Linux 101. Casey McLaughlin. Research Computing Center Spring Workshop Series 2018

AMS 200: Working on Linux/Unix Machines

CHE3935. Lecture 1. Introduction to Linux

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Laboratory 1 Semester 1 11/12

CS Unix Tools. Lecture 2 Fall Hussam Abu-Libdeh based on slides by David Slater. September 10, 2010

Helpful Tips for Labs. CS140, Spring 2015

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

*nix Crash Course. Presented by: Virginia Tech Linux / Unix Users Group VTLUUG

INTRODUCTION TO LINUX

Introduction To. Barry Grant

Open up a terminal, make sure you are in your home directory, and run the command.

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Projects and Environment Introduction

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

CS 460 Linux Tutorial

Lab Working with Linux Command Line

Lab 3a Using the vi editor

Exploring UNIX: Session 3

Unix Introduction to UNIX

CS/CIS 249 SP18 - Intro to Information Security

Essential Linux Shell Commands

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

Transcription:

Einführung in die Programmierung für Physiker WS 207/208 Marc Wagner Francesca Cuteri: cuteri@th.physik.uni-frankfurt.de Alessandro Sciarra: sciarra@th.physik.uni-frankfurt.de Exercise sheet To be corrected in tutorials in the week from 23/0/207 to 27/0/207 Exercise [Playing with the shell] In this exercise we will play around with some basic shell commands: mkdir, touch, chmod, rmdir, rm, pwd, cat. Moreover, remember that ls and cd are always your friends. When working with Bash or similar shells, the <Tab> key, which completes text, and the <Up> and <Down> arrows, which navigate through the command history, are very handy. Play a little bit with them during the exercise to find out how they can help you. (i) Open a terminal in Linux. (ii) Use the passwd command to change the password for your account. You will be first prompted for your old password, then you will be prompted twice for a replacement password. The second entry is compared against the first and both are required to match in order for the password to be changed. From your next login to your account, you will need to use the new choosen password. (iii) Create one directory in your home, let us call it ex, and make inside it the following directories: bin, etc, physics and src, such that $ ls ex/ bin etc physics src Do you need to cd to ex/ to do so? Check man mkdir to look for some option that allows you to skip the cd step. (iv) Go inside directory ex/src/ (you can always check where you are with the pwd command) and make three directories: c, c++ and python. Then go to directory c. We will use the command touch to create some files. First, execute touch Makefile, then touch makefile and last repeat touch Makefile. What does it happen? How many new files do you expect to have after running these three commands? Remember: the shell is case-sensitive. Can ls help us finding which file we created/modified last? Find at least two ways you can sort files in folders with ls by checking man ls. Finally, go to ~/ex/ and remove directory src/ and all its content. You can use rm or rmdir to complete this task. Perhaps some option is needed (check the man page in case of doubt). (v) Next, go to ~/ex/physics/ and create a new directory called.secret.project, and go inside it. Then, use your favorite editor (emacs, xemacs, vi, vim, nano, kate, gedit, etc.) to open a file called readme.text and write something inside it. Save the file and exit. Now, cd to ~/ex/physics/ and do ls. Where is the directory.secret.project, and the file readme.text? If you have troubles, check the man page of the ls command. (vi) Change the working directory to ~/bin and do touch my.fake.program. Can you execute it via./my.fake.program? Use ls -l to find out if and by who my.fake.program can be read, written, executed. You can use chmod to make it executable, for instance with $ chmod +x my. fake. program At this moment you can execute it (check the permissions in the first column of ls -l again). Now run./my.fake.program. What is the content of the file? What is the result of executing it? Put some content in the file using echo. For instance Here and in all the sheet, the $ is not part of the command and has not to be typed. It indicates only the end of the prompt (ask your tutor if you do not know what the prompt is).

$ echo " Hello world!" > my. fake. program $ echo " Goodbye." >> my. fake. program What is the contents of the file now? Check it with cat. Note the usage of >> in the second command above, instead of the symbol > in the first command. What happens if you execute both echo lines with >? What is the content of the file afterwards? Now, put some new content to the file, and execute it $ echo " cal " > my. fake. program $ echo " date " >> my. fake. program $ echo "ls" >> my. fake. program $./ my. fake. program Can you explain the output? Again, feel free to check the man page of any command whenever you need. Exercise 2 Gnuplot, as mentioned in the lectures, is a command-based, graphical plotting program for Linux which, in addition to being very handy when one needs a quick and dirty plot, is also extremely customisable, and can, with sufficient practice, be used to generate professional looking plots. (i) First, open Gnuplot by typing gnuplot in the terminal. Although a bit minimalistic, Gnuplot comes with extensive documentation through the help command. (ii) Start off with typing help plot which will tell you how to use the plotting function. Following the first example of the documentation, you can plot a simple curve by typing plot sin (x)*x **2 Gnuplot has a set of standard functions you can use when plotting, some of which are summarised below: exp () log () ** sin () cos () tan () asin () acos () atan () cosh () sinh () tanh () (a) First try to plot the Gaussian distribution with 0 mean value and unit standard deviation: 2π e 2 x2 () (b) Gnuplot should have been able to plot that quite nicely, but lets tidy it up a little. () Give the plot labels, such as x at the x-axis and P at the y-axis (2) Restrict the plot to only plot the ranges x [ 5, 5] and y [0, 2 ] (3) Plot tics along the x-axis at intervals of, and along the y-axis at intervals of 0 (4) Plot special named tics at the points x =.96 and x =.96, and name them 95% (5) Give the plot a title, e.g. Gaussian distribution ( x = 0, var = ) (6) Set the sample rate for the function to make the plot smoother All of the above tasks can be with Gnuplots set [option] function. You should read the help section (by typing help set [option]) on some of the following options and learn how to use them. You can use the replot function between each call to set to have your changes displayed, instead of re-writing the plot command. xlabel xrange xtics title samples key 2

(c) Lastly, let us change Gnuplots terminal and output so that we can save the plot we have made as an image file. The terminal setting tells Gnuplot which kind of output it should generate, e.q. gif, png, jpg, pdf, tex and so on (you might have noticed during startup that Gnuplot set the output to wxt, so if you ever need to switch back, you do not need to close and re-open Gnuplot). The output setting tells Gnuplot which file to save to. Read the documentation for the two settings, and save your plot in a filetype of your own choice. In this exercise we only covered styling the plot borders. Gnuplot offers much more functionality for which you can refer to the web 2. (iii) Close Gnuplot for a while. If you do not know how to do it, check help exit. (iv) Now let us try to plot points from, pairs of x and y coordinates, with some vertical error bars yerrorbars. (a) First we need to have a file (call it datafile) from which Gnuplot will be able to read our data. It will look like a table having as many rows as points we want to draw and 3 columns. Let us put as first column the x coordinate of our points, the y coordinates of our points will be listed in the second column and the third column will contain our errors on y. If you like, you can manually edit the file using your bash knowledge (echo-ing and appending row by row) or simply by copying numbers in a text editor of your choice. The file content should look like this -.0 0. 99990 0. 02686-0.8 0. 4099 0. 0464-0.6 0. 28373 0. 04399-0.4 0. 022666 0. 000070-0.2 0. 00660 0. 000740 0.0 0. 000232 0. 0002 0.2 0. 00863 0. 00338 0.4 0. 08073 0. 002906 0.6 0. 274 0. 02907 0.8 0. 40828 0. 06060.0 0. 996238 0. 07804 (b) You can also download the file data using the command wget which can get some options and expects some url (always check the corresponding man entry). The url you need is https://th.physik.uni-frankfurt.de/~cuteri/data. (c) Optional. Now you have two files (even if you will need one). Are you sure they are the same? You can check it with the diff command. (d) Open Gnuplot again and let us plot our data using: plot " data " using : 2: 3 with yerrorbars Can you understand the argument :2:3 of the using specification? You could use it to flip x and y... (e) Can you see a trend in those points? Can you guess a function? Try by running replot <function> to check if your guess was right! (f) For the most curious. How do you explain the output of the following command? plot " data " using :( $2 **0.25) :(0.25* $3 **0.25) with yerrorbars replot sqrt (x **2) 2 Ask google or have a look to gnuplot.sourceforge.net/demo_5.. 3

Exercise 3 [Installing GNU/Linux] Having a GNU/Linux system at hand is probably a very good idea in this course. It is flexible and powerful, and its most fashionable distributions (Fedora, Debian, Ubuntu, Suse...) bring by default many tools for programming. And provide easy access to much more software through their package managers. This first exercise is a reminder of what was said in the lecture. The simplest GNU/Linux installation is probably on a clean computer - without any other operating system. But it is usually possible to make GNU/Linux coexisting with other operating systems. Many distributions have online documentation to help you in such a task. Another option is to use some kind of virtualization software. In that case, you install some program like VirtualBox or Xen (or many others) in your current operating system. The general idea is that those programs simulate computers, therefore you can create a new virtual computer inside the virtualization program and install whatever operating system you like without affecting the installation of the current real operating system. You are free to choose the method you like more. Exercise 4 [Read my mind] In this exercise we want to implement a very easy game to challenge the pc. We want the pc pick up a number and we want to guess it until we discover it. Our program, then, should give us a feedback about our answer and wait for a new input, exiting once the correct answer has been given. Let us discover together something more about the shell and the bash language. For each command used in the following, you can use the linux manual to get more information on it (google is also a good friend in this). (i) If you open a file, write inside some bash code, save and close it, then you can execute with the bash command. Let us do it. Move yourself to a suitable folder, create a new file called readmymind.bash, and write in it the following code. echo " Which number am I thinking? [ 0, 32767] " Save and exit, then run it giving the following command. bash readmymind. bash This will be the beginning of our code. (ii) Next element to learn is how to let the computer choose its number. It should be randomly (we do not want to cheat). Try to write echo $RANDOM on the command line and press return. Do it again. And again. It looks like what we need. Nice! (iii) Now we need to store this random number somewhere in our script. It is possible to put it in a variable which we will call answer. And the operation of storing some content in it is via the = operator, which must not be separated by spaces. Something like answer = $RANDOM added to your script will do the job (without spaces around the = sign!). (iv) We should also be able to give a number to the program. Or, said differently, we should let our program wait for a number that should be stored then in a variable. This is via the read command. Writing something like read myanswer will make the program stop and wait for an input. Once we type something on the keyboard and we press return, what we typed will be stored in the myanswer variable and the program will continue. (v) Since we will hardly win at the first attempt, we need some kind of repetition in our code, a so-called loop. Here we will use a loop, whose syntax reads while...; do... 4

and we have to fill out the dots. Next to while, we should put a condition, which, if satisfied, allows the content of the loop to be executed. A way to read the loop may be: While the condition is true, do what is written inside. The condition in our script will be while read myanswer ; do... (vi) A last thing we need is how to compare numbers. We need three cases. (a) The number we guessed is equal to the answer: we won! (b) The number we guessed is less than the answer: we should guess a larger one. (c) The number we guessed is greater than the answer: we should guess a smaller one. In words we would say: If my number is equal to the answer, I won; otherwise if it is larger I need to guess a smaller one; otherwise I need to guess a larger one. In bash this becomes Some remarks: if [ $myanswer - eq $answer ]; then echo " You won!" break elif [ $myanswer - gt $answer ]; then echo - n " Too large! Try again : " else echo - n " Too small! Try again : " fi (a) with the $-symbol, the content of a variable is obtained; (b) -eq checks if what is left it equal to what is right; (c) -gt checks if what is left it greater than what is right; (d) blank spaces around the brackets enclosing the if condition are mandatory; (e) what does the option -n of echo do? All together your file readmymind.bash should contain something like the following code. echo " Which number am I thinking? [ 0, 32767] " answer = $RANDOM echo -n " Try to guess : " while read myanswer ; do if [ $myanswer - eq $answer ]; then echo " YOU WON!" break elif [ $myanswer - gt $answer ]; then echo - n " Too large! Try again : " else echo - n " Too small! Try again : " fi Try to run it and... have fun!! 5