Article 2022-09-09 20:41:16
Ricard Santiago avatar Ricard Santiago

Introduction to Bash exercises

Extract

A bunch of short exercises to introduce or practice with examples of Bash scripts.

Article

Please, to download the CSV file (athletes.csv), visit this link ?


1. Print in BASH
Print: Hello World!



2. Arrays in BASH
Using an iterative structure (for or while) and a single arithmetic operation, it creates a text file called ex1.txt whose content is the first 5 numbers that are multiples of 5 separated by a space.

Solution:
Int variable that is zero and a for loop that iterates checking that the remainder is zero. I then print the int values and create a txt file storing the result.





Read the file created in the previous section and store the content of the file inside an array called mult5, and we add the 5 positions of the mult5 array, displaying the result on the screen.



Other possible solution:



3. Basic analysis of a data set using the Command line
With what command can you know the size of the file in Megabytes? (The output should go to ex3_a.txt).



How many lines without counting the header does the file contain? (The output should go to ex3_b.txt).



With what command can we show "only" the header and the following three lines (the first four lines) of the file? (The output should go to ex3_c.txt).



How can we show “only” lines 130-138 inclusive? (The output should go to ex3_d.txt).



With what command can we show only the third column? (The output should go to ex3_e.txt).



And only the third column but with the ordered values and without repetitions? (The output should go to ex3_f.txt).




4. Creating a report in BASH
From the file attached to the announced athletes.csv, whose name must be respected in the code you write, create a script called script2_4.sh in Bash (without using grep or awk) that receives the nationality and sport code as parameters , and show the number of athletes and the total number of medals (obtained as the sum of the gold, silver, and bronze columns) of said nationality and sport. Specifically, the output should have the following format:
Participants, Medals
x-y
where X is the total number of athletes, and Y the total number of medals.
Here are some examples of its invocation and the output it should
get (respect, spaces, symbols and others because a diff will be used during the correction):
./script2_4.sh POL rowing
Participants, Medals
26, 6






Please, to download the CSV file (athletes.csv), visit this link ?

More related to Introduction to Bash exercises