A continuation of bash��Michelle Greene, Ph.D.
Let’s get some data
Use ssh to log into your remote node.
$ git clone –b summer_2022 https://github.com/vedb/summer_workshop
Warm up
Review
Wildcard (*)
Try the following in the texts folder:
ls s*
ls *.txt
With a partner, discuss how the * character might work.
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.
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
> And >>: Send content to a file
> And >>: Send content to a file
wc –w *.txt > books.txt
echo myBookList >> books.txt
echo myBookList > books.txt
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.
Is there a way to chain commands together?
Example of a pipe
wc –l *.txt | head –n 3
Example of a pipe
wc –l *.txt | head –n 3
Example of a pipe
wc –l *.txt | head –n 3
Get the number of lines in each text file
Example of a pipe
wc –l *.txt | head –n 3
Get the number of lines in each text file
Display the first three items
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.
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
Group exercise: what is the output?
cat books.txt | head –n 5 | tail –n 3 | sort -r > new_books.txt
5-minute break
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?
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
…
Anatomy of a loop
for thing in list_of_things
do
operation_using $thing
done
Anatomy of a loop
for thing in list_of_things
do
operation_using $thing
done
Anatomy of a loop
for thing in *.txt
do
operation_using $thing
done
Anatomy of a loop
for thing in *.txt
do
head –n 1$thing
done
Where’s the error?
for book in *.txt
do
head –n 1*.txt
done
Where’s the error?
for book in *.txt
do
head –n 1$book
done
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.
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
Conditional statements
Execute simpleScript.sh
(you might want to do this a few times to get a sense of its behavior)
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!
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
Grep: finding content within files
grep monster dracula.txt
Term to find
Where to search
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.
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