User Tools

Site Tools


labs:bash

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
labs:bash [2017/02/10 12:36]
thulrich
labs:bash [2020/08/31 21:03] (current)
Line 1: Line 1:
 ====== Bash & Git ====== ====== Bash & Git ======
-In this fachpraktikum ​you are supposed to see the basic of bash. The goal is that you know the type of problems for which it is the right tool. You will work on a command line. Despite the ancient feeling, a lot of problems can be solved much more efficiently than with a GUI. +In this lab session, ​you'​re ​supposed to learn the basics ​of bash and git
-For every command, you can find help using ''​man <​command>''​ (you can leave this screen by pressing ''​q''​). We also expect you to use Google whenever you encounter a problem. +
-Have a look at this [[https://​learncodethehardway.org/​unix/​bash_cheat_sheet.pdf|Cheat Sheet]]. It contains most of the commands you will need (and quite a lot that you don'​t). +
-You can use [[http://​tldp.org/​HOWTO/​Bash-Prog-Intro-HOWTO-3.html|redirects]] to write the output of a program into a file.+
  
-We provide you with scripts for the first two exercises (exercise1.sh/​exercise2.sh) that check your results.+**Bash** is the standard shell in many Linux distributionsDespite the ancient feeling, some tasks which are very tedious to do on a GUI can be solved pretty quickly on the command lineEspecially if a task is very repetitive, there is a high chance ​that there exists a command line tool which can solve it faster.
  
 +**Git** is a version control system. Version control systems are primarily used in software development to keep track of all changes made by the software developers to any file within the software project. It keeps a version history and allows you to revert changes made to a file and to restore deleted files. Therefore, git is also useful if you work on a set of files on your own, but you want to keep track of every change you make and you want to be able to go back to a previous version if you make a mistake.
  
-===== Exercise 1 =====+===== Useful resources ===== 
 +  * [[https://​learncodethehardway.org/​unix/​bash_cheat_sheet.pdf|Bash Cheat sheet]] 
 +  * [[https://​wiki.ubuntuusers.de/​Shell/​Befehls%C3%BCbersicht/​|List of common shell commands]] (in German) 
 +  * [[https://​rogerdudler.github.io/​git-guide/​|Git - the simple guide]] 
 +  * [[https://​www.atlassian.com/​git/​tutorials/​undoing-changes|Undoing changes with Git]] 
 +  * Type ''​man '' ​ (note the space), followed by the name of a command, to the command line.\\ **Example**:​ ''​man rm''​\\ You will then see a manual page for that specific command. You can leave this screen by pressing the ''​q''​ key. 
 +  * Google 
 +===== Exercise 1: Bash and Git basics ​===== 
 +In this exercise, you will use the most basic commands of git and bash. Make yourself familiar with these tools before we start with the more interesting part in Exercise 2.
  
-As a first task, please checkout ​the necessary files from your git repository ​(you will have to install git first). You can find it under ''​10.0.0.1:/​opt/​git/​ti2fp''​. The username is ''​git''​the password is ''​ti2lab''​. ​To avoid conflicts with your fellow students, please create ​a folder with the name of your nethz account ​and copy the files into this folder. You can now create a new repository ​in this folder. This will allow you to undo any mistakes you make. A short (and simple) introduction can be found [[http://​rogerdudler.github.io/git-guide/​|here]]We urge you to commit your changes after you have successfully solved an exercise.+  - Clone the git repository ''​<​nowiki>​ssh://​git@10.0.0.1/​opt/​git/​ti2fp</​nowiki>''​ into a folder called ''​ti2fp''​. The username is ''​git'' ​and the password is ''​ti2lab''​. ​(You will have to install git first.) 
 +  - Create ​new folder with the name of your nethz account
 +  - Navigate ​into this folder ​and create a new git repository
 +  - Copy the files and subfolders from the ''​ti2fp''​ folder into this folder
 +  - Add these files to the git Index and commit them. 
 +  - If you have worked with the command line before, you can skip the rest of this exercise and proceed to Exercise 2.  
 +  - We strongly recommend that, in the following exercises, you commit your changes every time you solve an exercise. This will allow you to undo any mistakes ​that you make. 
 +  - Create a new file called ''​empty.txt''​. 
 +  ​List all files in that folder. 
 +  - Create a copy of the file ''​empty.txt''​ called ''​still-empty.txt''​. 
 +  - Open the file ''​empty.txt''​ in the text editor called ''​nano'',​ add some text and save the file. 
 +  - Output the content of the file on the command line. 
 +  - The file is not empty anymore, so let's rename it to ''​text.txt''​. 
 +  - Delete the file ''​still-empty.txt''​.
  
 +===== Exercise 2: Extracting data from files =====
 +In this exercise, you will see some basic commands to read data from files. You can save the output of a program to a file by using [[http://​tldp.org/​HOWTO/​Bash-Prog-Intro-HOWTO-3.html|redirects]].
  
-  - Print the content of ''​msft.csv''​ on the terminal. +  ​- Navigate into the ''​csv-files''​ folder. There, you will find a number of ''​.csv''​ files which contain stock prices of different stocks. 
-  - Create a file called ''​allTheData.csv''​ that contains every line from every ''​*.csv''​ file in the main folder+  ​- Print the content of ''​msft.csv''​ on the terminal.  
-  - Unfortunately,​ your colleague, who created these files, did not follow the standard you agreed upon. Instead of a '';''​ he used a ''#''​ to separate the columns. Fix his mistake in ''​allTheData.csv''​+  - Create a file called ''​allTheData.csv''​ that contains every line from every ''​*.csv''​ file in the main folder.  
-  - Count the number of lines in ''​allTheData.csv''​ write it into ''​numberOfLines.txt''​. +  - Count the number of lines in all ''​.csv'' ​files and write them into the file ''​numberOfLines.txt''​. 
-  - Go to the folder ​''​csv-Files''​. In this folder there are dozens of csv files (in subfolders). Write the name of every ''​*.csv''​ file (including ​the path starting from the folder ​''​csv-Files''​) into ''​csvFilesList.txt''​. +  - You want to have a quick look at the data format in your ''​.csv''​ files. Write the first 10 lines of every ''​.csv''​ file into the file ''​firstLines.txt''​. 
-  - Write the content ​of all these files into ''​allCSVFilesContent.txt''​. +  - Your friend is interested in the latest stock prices. ​Write the last 20 lines of every ''​.csv''​ file into the file ''​latestData.txt''​. 
-  - Go to the folder ''​src/​core''​. ​Print every line that contains the string ​''​author'' ​or ''​Author''​. +  - Write the first three columns (Date, Open, High) of ''​msft.csv'' ​into ''​msft2.csv''​. 
-  - Append ​the string ''​%%//​%% Author: ETH Zurich'' ​to every ''​*.java''​ file in this directory. +===== Exercise 3: Searching, pipes and xargs ===== 
-  - Print every **filename** that contains the string ''​%%//%% Author: University of Zurich''​. +In the previous exercise, all tasks could be solved with just one command by passing some parameters ​to this commandIn this exercise, we will learn how to search for files by their name and their contentMoreover, you are supposed to use [[http://tldp.org/​HOWTO/​Bash-Prog-Intro-HOWTO-4.html|Pipes]] and (sometimes) ​the ''​xargs'' ​command to pass on the output of one command to another command.
-  ​Delete those files. +
-  - Go to the folder ​''​src/utils''​. Print every file in this folder that does not contain ''​DO NOT DELETE''​. +
-  - Delete those files.+
  
 +Go to the ''​sourcecode''​ folder. There, you will find hundreds of Java files in different subfolders. It would be tedious to open them all in a text editor if you were looking for something, wouldn'​t it?
  
 +  - Write the name of every ''​.java''​ file (including the path to the files starting from the folder ''​sourcecode''​) into a file called ''​javaFilesList.txt''​.
 +  - Print every line that contains the string ''​author''​ or ''​Author''​. ​
 +  - List the name of every file that does not contain the string ''​java''​ in its filename. Write the output into the file ''​otherFilesList.txt''​.
 +  - Print the filename of every file that contains the string ''​%%//​%% Author: University of Zurich''​. ​
 +  - Delete those files. ​
 +  - Commit your changes. Make sure that git also commits the removal of those files.
 +  - Go to the folder ''​sourcecode/​utils''​. Print every file in this folder that does not contain ''​DO NOT DELETE''​. ​
 +  - Delete those files. ​
 +  - You realize that you did not want to delete these files. Use Git to restore the files that you removed in Exercise 8.
 +  - Use Git to restore the files that you removed in exercise 5.
 +  - The file ''​allSrc.txt''​ (in the folder ''​sourcecode''​) contains a list of files. Write every line of these files that contain the word ''​static''​ into the file ''​static.txt''​. ​
  
-===== Exercise ​=====+===== Exercise ​4: sed and Git branches ​===== 
 +**sed** is a very powerful tool to edit text files on the command line. It is often used for search-and-replace operations. As you will see shortly, you can do a simple search-and-replace operation or delete a couple of lines - all of this can be done in many files at once.
  
-Some bash commands accept only parameters as input. But some also accept input from stdin. Have a look at [[http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-4.html|Pipes]]. In this exercise, we will explore a few examples. +You will also see **[[https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell|Git Branches]]** in this exercise.
-Please change to the folder ''​exercise2''​. +
- +
-  - Write every line that contains ''​import ''​ (note the space), but not ''​bitcoinj''​ in the folder src/core in a file called ''​all_the_imports.txt''​. +
-  - Count the total number of lines that contain "​import"​ in every java file in src/core and write it into ''​importLine.txt''​ +
-  - You know now how many lines there are. But you (should have) noticed that quite a few lines appear more than once. Which line appears the most? Write this line (without spaces in the beginning) into ''​mostImported.txt''​. +
-  - The file ''​allSrc.txt''​ (in the folder src) contains a list of files. Write every line of these files that contain the word ''​static''​ into the file ''​static.txt''​. +
- +
- +
- +
-===== Exercise 3 =====+
  
 +  - Create a git branch called ''​experimental''​.
 +  - Check out your newly created branch.
 +  - Go to the folder ''​core''​.
 +  - Replace the string ''​%%//​ Author: University of Zurich%%''​ with ''​%%//​ Author: ETH Zurich%%''​ in all files in this directory.
 +  - Navigate to the ''​csv-files''​ directory. Remove every odd line from every csv file in this folder.
 +  - Go back to the Git ''​master''​ branch.
 +===== Exercise 5: Bash scripting =====
 In this exercise we advise you to write bash scripts, i.e., *.sh files. They will make life a bit easier. You can find some basic information [[http://​tldp.org/​HOWTO/​Bash-Prog-Intro-HOWTO-5.html|here]] and [[http://​tldp.org/​HOWTO/​Bash-Prog-Intro-HOWTO-6.html|here]]. In this exercise we advise you to write bash scripts, i.e., *.sh files. They will make life a bit easier. You can find some basic information [[http://​tldp.org/​HOWTO/​Bash-Prog-Intro-HOWTO-5.html|here]] and [[http://​tldp.org/​HOWTO/​Bash-Prog-Intro-HOWTO-6.html|here]].
-  - You work together with a colleague who uses Windows. Thus, he cannot checkout the repository (because the files in the folder ''​files with dots''​ contain a '':''​). ​ Replace all the '':''​ with ''​_''​.+  - You work together with a colleague who uses Windows. Thus, he cannot checkout the repository (because the files in the folder ''​files with dots''​ contain a '':''​). ​ Replace all the '':''​ with ''​_''​. ​
   - Write a script that prints out ''​is running''​ if ''​looper''​ is running and ''​is not running''​ otherwise. Note that this process is currently not running.   - Write a script that prints out ''​is running''​ if ''​looper''​ is running and ''​is not running''​ otherwise. Note that this process is currently not running.
   - Update your script such that it starts ''​looper''​ if it is not running.   - Update your script such that it starts ''​looper''​ if it is not running.
   - Update your script such that it continuously monitors ''​looper''​.   - Update your script such that it continuously monitors ''​looper''​.
   - Update your script such that it accepts the names of several programs as parameter, i.e., ''​./​yourScript.sh looper backGround''​ should work.   - Update your script such that it accepts the names of several programs as parameter, i.e., ''​./​yourScript.sh looper backGround''​ should work.
- 
labs/bash.1486726611.txt.gz · Last modified: 2020/08/31 21:03 (external edit)