1 of 36

A continuation of bash��Michelle Greene, Ph.D.

2 of 36

Let’s get some data

Use ssh to log into your remote node.

$ git clone –b summer_2022 https://github.com/vedb/summer_workshop

3 of 36

Warm up

  • Use your terminal to navigate to summer_workshop/Day2/Afternoon/texts
  • Display the contents of this folder

4 of 36

Review

  • cat: display the contents of a file
  • less: display the first part of a file with scrolling
  • wc: “word count”
    • wc –l: count lines
    • wc –w: count words
    • wc –m: count characters

5 of 36

Wildcard (*)

Try the following in the texts folder:

ls s*

ls *.txt

With a partner, discuss how the * character might work.

6 of 36

Wildcard (*)

Try the following in the texts folder:

ls s*

ls *.txt

With a partner, discuss how the * character might work.

Exercise: In 1 line, get the word count of each novel in texts.

7 of 36

Wildcard (*)

Try the following in the texts folder:

ls s*

ls *.txt

With a partner, discuss how the * character might work.

Exercise: In 1 line, get the word count of each novel in texts.

wc –w *.txt

8 of 36

> And >>: Send content to a file

  • >: create file and write content
  • >>: create file if needed, append content if file exists

9 of 36

10 of 36

> And >>: Send content to a file

  • >: create file and write content
  • >>: create file if needed, append content if file exists

  • What will be the output? (Predict with partner and then try)

wc –w *.txt > books.txt

echo myBookList >> books.txt

echo myBookList > books.txt

11 of 36

What if you want to sort your content?

Exercise:

Create a file called books.txt that is a list of all books in texts along with their word counts.

Use sort to create a new file called sorted_books.txt that sorts these books in order of length.

12 of 36

Is there a way to chain commands together?

13 of 36

Example of a pipe

wc –l *.txt | head –n 3

14 of 36

Example of a pipe

wc –l *.txt | head –n 3

15 of 36

Example of a pipe

wc –l *.txt | head –n 3

Get the number of lines in each text file

16 of 36

Example of a pipe

wc –l *.txt | head –n 3

Get the number of lines in each text file

Display the first three items

17 of 36

Exercise with partner:

Let’s say that you want to get a list of the three shortest books in the texts directory. Create a command that uses two pipes to accomplish this goal.

18 of 36

Exercise with partner:

Let’s say that you want to get a list of the three shortest books in the texts directory. Create a command that uses two pipes to accomplish this goal.

wc –w *.txt | sort | head –n 3

19 of 36

Group exercise: what is the output?

cat books.txt | head –n 5 | tail –n 3 | sort -r > new_books.txt

20 of 36

5-minute break

21 of 36

Loops: why?

Let’s say that you are worried that you’ve mis-labeled these book files. How would you check the first line of each?

22 of 36

Loops: why?

Let’s say that you are worried that you’ve mis-labeled these book files. How would you check the first line of each?

head –n 1 dracula.txt

head –n 1 frankenstein.txt

23 of 36

Anatomy of a loop

for thing in list_of_things

do

operation_using $thing

done

24 of 36

Anatomy of a loop

for thing in list_of_things

do

operation_using $thing

done

25 of 36

Anatomy of a loop

for thing in *.txt

do

operation_using $thing

done

26 of 36

Anatomy of a loop

for thing in *.txt

do

head –n 1$thing

done

27 of 36

Where’s the error?

for book in *.txt

do

head –n 1*.txt

done

28 of 36

Where’s the error?

for book in *.txt

do

head –n 1$book

done

29 of 36

Exercise in pairs

Create a backup of each book in your texts folder using a loop. Each backup should have the phrase “backup-” before the original filename.

Hint: remember that cp creates a copy of each file.

30 of 36

Exercise in pairs

Create a backup of each book in your texts folder using a loop. Each backup should have the phrase “backup-” before the original filename.

Hint: remember that cp creates a copy of each file.

for book in *.txt; do cp $book backup-$book; done

31 of 36

Conditional statements

Execute simpleScript.sh

(you might want to do this a few times to get a sense of its behavior)

32 of 36

Conditional statements

Execute simpleScript.sh

(you might want to do this a few times to get a sense of its behavior)

echo -n "Please enter a whole number: "

read VAR

echo Your number is $VAR

if [ $VAR -gt 100 ]

then

    echo "This number is greater than 100"

else

    echo "This number is less than 100"

fi

echo Thanks for playing!

33 of 36

Conditional statements exercise

With a partner, deconstruct the function of this bash script.

for book in *.txt

do

words="$(cat $book | wc -w)"

if [ $words -lt 100000 ]

then

echo $book

fi

done

34 of 36

Grep: finding content within files

grep monster dracula.txt

Term to find

Where to search

35 of 36

Grep: finding content within files

grep monster dracula.txt

Term to find

Where to search

Exercise: Write a loop that will count the number of lines containing the word “monster” in each of the books in texts.

Hint: you will need to use a pipe.

36 of 36

Grep: finding content within files

Exercise: Write a loop that will count the number of lines containing the word “monster” in each of the books in texts.

Hint: you will need to use a pipe.

for book in *.txt

do

echo "$book $(grep monster $book | wc -l)"

done