12. Bash Scripts#

We’ll start today in the top level folder for class

ls
2022-09-19			github-inclass-brownsarahm
courseutils			kwl-brownsarahm
fall2022			test

12.1. Bash has programming constructs#

We can create variables

NAME="Sarah"

and use them with a $

echo $NAME
Sarah

If we forget it, it treats it as a literal

echo NAME
NAME

We can also make loops

for name in "sarah" "mark" "linda" "david"
do
echo $name
done
ls

To run a command and make it a variable we can use $(command). For example:

for file in $(ls); do echo $file; done
2022-09-19
courseutils
fall2022
github-inclass-brownsarahm
kwl-brownsarahm
test

12.2. Searching files#

grep searches files

This searches all of the files in the test/ repo for “version”

grep "version" test/*
test/test.txt:version 1
test/test.txt:version 2

It finds two occurrences, prints the line, and tells us where it found each one

To have something to search we’ll clone the course website

git clone https://github.com/introcompsys/fall2022.git
2022-09-19			github-inclass-brownsarahm
courseutils			kwl-brownsarahm
fall2022			test

I set it up in the _review/, _prepare, and _practice folders so that there is the syntax to denote the files that need to be created:

```{index} file.md
```

To find them all this way, we can grep for “index”

grep "index" fall2022/_review/*
fall2022/_review/2022-09-19.md:```{index} terminal
fall2022/_review/2022-09-21.md:```{index} branches.md
fall2022/_review/2022-09-26.md:```{index} abstraction.md
fall2022/_review/2022-09-28.md:```{index} gitlog.txt
fall2022/_review/2022-10-03.md:```{index} gitstory.md
fall2022/_review/2022-10-05.md:```{index} gitplumbingreview.md
fall2022/_review/2022-10-12.md:```{index} numbers.md
fall2022/_review/2022-10-12.md:```{index} hexpeak.md
fall2022/_review/2022-10-17.md:```{index} test_repo_map.md

We can use awk to pull out a subset of these results. Remember a pipe | takes the stdout of one command and sends it to the in of the next.

grep "index" fall2022/_review/* | awk '{print $2}'
terminal
branches.md
abstraction.md
gitlog.txt
gitstory.md
gitplumbingreview.md
numbers.md
hexpeak.md
test_repo_map.md

Now we have just the files as a list.

Try awk '{print $1}' instead to understand awk better

12.3. Check if a file exists#

We can see if a file exists with test

test -f kwl-brownsarahm/branches.md

but it doesn’t return the value, so we combine it with if

if test -f kwl-brownsarahm/branches.md
> then
> echo "exists"
> fi
exists

Note that if is ended with fi

12.4. Checking if the your KWL has all the files it needs#

We’ll save this longer one in a script file

nano checker.sh

Then we can enter the following content:

for file in $(grep "index" fall2022/_review/* |awk '{print $2}')
do
if ! test -f kwl-brownsarahm/$file; then
echo $file
fi
done

For reference, we can look at what files are there

ls kwl-brownsarahm/
README.md	branches.md	check.sh	terminal

And then use the checker to denote the missing ones

bash checker.sh
abstraction.md
gitlog.txt
gitstory.md
gitplumbingreview.md
numbers.md
hexpeak.md
test_repo_map.md

12.5. Review today’s class#

  1. Update your KWL Chart learned column with what you’ve learned

  2. Make a contribution to your group repo and do a peer review of a team member’s PR.

  3. Use the gh cli, grep, and bash to create group_contributions.md to your KWL repo with a list of all of your PRs. Append your history from creating this to the bottom of the file after ## Commands

  4. Move your checker script into your kwl repo and update the paths so that it still works

12.6. Prepare for Next Class#

  1. On Windows, install Putty ( we will use this Monday)

  2. Get ready for class by creating networking.md in your KWL repo with notes about what you know about networking

  3. Add an issue on your KWL repo titled “Self Reflection 10/24” (there will be time on Monday for this, but giving you warning so you have time to think about it). Tag @brownsarahm on this issue.

1. Are you where you want to be in this course?
1. Have you been getting feedback on your work?
1. If not, what could help you get back on track?
1. What if any concepts are you most struggling with?

12.7. More Practice#

  1. Make your script form class a nested loop to check for all 3 types of activites (Review Today’s Class, Prepare for Next Class, and More Practice)

  2. Make a script that gets the updates to the course site and creates a single todo-YYYY-MM-DD.md file that has the review, prepare, and practice tasks in it for each date that does not already exist in a todo/ folder outside of your KWL repo. Save the script as gathertasks.sh

12.8. Questions after Class#

12.8.1. Can I add another language like C to git bash?#

Bash is a shell scripting language. A shell is an interface to your computer’s operating system. A terminal is how we access it. We will see that we can use a terminal to call a c compiler.

12.8.2. Does git rely on scripts to do low-level commands for higher ones?#

git is written mostly in C but there are some shell scripts in its source code

12.8.3. How could I make the checker check a branch on my repo that isn’t currently the selected branch?#

The easiest way is to switch branches and check again. You might be able to traverse the git repo, but that is not straight forward.

12.8.4. what is one thing I should try and automate with bash#

For now see the More practice