{ Shell Scripting and Vim. }

Objectives

By the end of this chapter, you should be able to:

  • write simple shell scripts with arguments
  • use vi to open and edit files

Shell Scripting

So far we have learned how to use terminal commands, but our commands are not dynamic. We know exactly what the filenames are or what we might be searching for. Our commands are also not reusable easily; if the filename changes, we have to write the whole command again. To reduce duplication and perform far more sophisticated commands in the terminal, we can write shell scripts using a language called bash. To get started, let's create a file called first.sh and inside place the following

echo "Hello World"

Now if we try to run ./first.sh it will tell us permission denied: ./first.sh. So we need to make this program executable! Let's change permissions to 755 so that anyone can run this file: chmod 755 first.sh. Now we can run ./first.sh!

Let's make a second script called second.sh. Inside of this file, type the following:

echo Hello $1

What do you think that $1 represents? Let's first chmod 755 second.sh and then run ./second.sh - we should just see "Hello". Now let's try ./second.sh World and we should see "Hello World"! What we just did was pass an argument to our script. Using arguments which start with $1 and continue upward are how we can make more dynamic scripts.

Your turn

Write a script called sum.sh which accepts two arguments and echoes the sum of the two numbers. You might need to do some research here.

Here's a solution:

echo $(($1+$2))

More complex scripts

There is much more we can do with shell scripting, including conditional logic, if statements, and much more. But for now, let's try to write some handy scripts to help us with daily tasks. Very commonly we are searching for a process using ps aux | grep "Something". Instead of typing that whole thing, we could make a script that does that for us and passes in an argument. Let's try that out with a script called process.sh. It might look something like this.

ps aux | grep $1

Now this is useful if we want to search for a process! If you would like to use the script frequently, you will want to make sure it is somewhere in your $PATH. You can also remove the .sh extension if you are doing this.

Vim

Vim is a terminal based text editor. It can be quite intimidating at first, but if you spend the time to learn how to use it, you can become an extremely efficient developer. To open up something in vi, simply type vi NAME_OF_FILE. Once you are in vim you can exit without saving by pressing escape + : + q. If that is not working press escape + : + q!. If you want to quit and save a file you can either press shift + Z + Z or escape + : + w + q!.

When you first get into vim you will be in what is called visual mode. This means that your keyboard will be set for navigating and not for inserting characters; if you type letters and do not see anything being output, do not worry! To make insertions, press i to enter insert mode. Here you can make changes to files and type as you normally would. Just remember, if you want to quit out of a file you have to be in visual mode!

You can get more practice by using vimtutor - just type vimtutor into your terminal and you can get started. Alternatively, if you want a more visual tutorial you can check out Vim Adventures.

When you're ready, move on to Advanced Terminal Exercises

Continue

Creative Commons License