1. Automating cool things
Why spend 5 minutes doing it manually when you can spend 5 hours automating it?
Shell Navigation
Helpful Notes
- The shell is what we use to send commands directly to the kernel.
- 'Shell' and 'Terminal' are generally the same thing.
- The term
directory
means the same thing as folder. - The
~
symbol represents the home directory in both Linux and Unix (MacOS is UNIX). - Everything in Linux is a file.
- Every command begins with a program name.
- You can learn more about the program using the
man
program.
You can open your terminal in MacOS by searching for it in Spotlight.
For windows users, do Win+X, I to open the terminal.
- It will probably open in the default Powershell/CMD profile, click the dropdown and switch to Ubuntu.
man cd
Press q
to quit. You can put almost any program after the man
command to learn more about it. You can even do man man
.
We recommend typing in the commands by hand rather than copy-pasting, because:
- You'll learn quicker
- You'll be quicker
- You'll retain the information better
- You'll become more comfortable with the terminal
See current location
To see where we currently are, we use pwd
.
pwd
Your output can be different, but will definitely have slashes like this:
/home/yourusername
pwd
means 'print current directory'. This is our current location.
See files in current location
To see what files and directories are around us, we use ls
.
ls
ls
means 'list'.
ls -a
will show us all of our files and directories, including hidden ones.
ls -a
Go to the home directory
To move between directories, we use the cd
command.
cd ~
cd
means 'change directory'. You can use pwd
again to see where you are.
Make a directory called workspace
To create directories, we use the mkdir
command.
mkdir workspace
mkdir
means (make directory) command
Go into the workspace
directory
cd workspace
mkdir hack-the-tunnels-git-workshop
Try the echo
command
echo "Hack the Tunnels"
You will then see the output:
Hack the Tunnels
This command is used to print things, just like print()
in Python, and console.log()
in JavaScript. Replace the text between the double quotes with anything you like and try again :)
man echo
if you want to learn more. You can quit man pages by pressing q
.
Edit the file
Let's open VS Code in this directory so that we can edit the file.
code -r .
The -r
flag means re-use the same VS Code window if one exists. The .
means this current directory.
Tip
Using lowercase letters and hyphens with no spaces for file and folder names will make script automation much easier.
Make a Bash Script
At this point, your file tree (Explorer) in VS Code should look like this:
Open VS Code terminal
You can open/close the VS Code terminal with ⌘/Ctrl + ~.
Edit file.sh
Add the following:
#!/bin/bash
This indicates to Linux/Unix that this is a bash script.
If you see a dot next to filename in the tab, that means it's not saved.
You can save the file with ⌘/Ctrl + S. Always ensure to save before moving on.
Check if echo
works in the VS Code terminal
echo "Hack the Tunnels"
You will then see the output:
Hack the Tunnels
It works, let's add it to the file.
#!/bin/bash
echo "Hack the Tunnels" // [!code highlight]
Run the file
./file.sh
You should see:
bash: permission denied: ./file.sh
Let's fix it by giving it permissions.
sudo chmod +x ./file.sh
Run it again.
./file.sh
You should see:
Hack the Tunnels
Make a cow say "Hack the Tunnels"
brew install cowsay
cowsay "Hack the Tunnels"
__________________
< Hack the Tunnels >
------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
fastfetch
, cmatrix
, and lolcat
Rather than installing programs one by one, we can install multiple in a single command.
brew install fastfetch cmatrix lolcat
When someone asks:
What are your specs?
We show them this.
fastfetch
Output varies on your OS.
Very cool. Now, let's allow ourselves to feel like hackers.
cmatrix
Press q to quit.
Now, let's allow ourselves to feel like hackers but in RGB.
cmatrix | lolcat
Press q to quit.
Let's make a cow say "Hack the Tunnels".
cowsay "Hack the Tunnels"
Let's make a cow say "Hack the Tunnels" but in a RGB.
Notes
You can hit the up arrow key on your keyboard to see past commands.
cowsay "Hack the Tunnels" | lolcat
Change the text inside the quotes and try it a few more times to see what happens :)
Automate away
Notes
Any command you can run in the terminal, you can also write into the file and run.
Let's try cowsay.
#!/bin/bash
echo "Hack the Tunnels"
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
Let's spam cowsay because why not.
#!/bin/bash
echo "Hack the Tunnels"
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
cowsay "Hack the Tunnels" | lolcat // [!code highlight]
Save, and run the file again :)
./file.sh
Bash commands review
Action | Command |
---|---|
Print 'Hello' to the terminal | echo 'Hello' |
See your username | whoami |
Print working directory | pwd |
Create a directory | mkdir your-directory-name |
Change into directory | cd your-directory-name |
Go one directory back | cd .. |
Remove directory | rm -rf your-directory-name |
List contents of directory | ls |
List contents of directory including hidden files | ls -a |
Create file | touch your-file-name |
Remove file | rm your-file-name |