Building more complex commands using Raspberry Pi terminal

Probably this would be unwise to go through long list of available Unix commands. It is quite long and there is no reason to point out each of them here. You can take a look at some basic ones in following list.

I thing it is more important to learn how to use them, get desired result by building more complex commands. Commands can also be combined in to single line using piping. In this case the output of one command becomes input of next one and so on. Lets go with few examples.

We all know that Raspberry Pi comes with Python installed. So we should expect to find lots of .py files here:

sudo find / -name *.py

this throws us large list of file names:

find_py

Finding and displaying files that way is useless. Viewing is even more painful. Lets say we want simply to count the all .py files. For this we use same command, but instead of throwing list to terminal we feed it to another command that does counting of lines (we get every file in new line):

sudo find / -name *.py | wc -l

and in terminal we get nice number 4485. so we already get more informative output. We can do more interesting tricks here. Lets say we want to count .py programs that contains word “game”. Again we need to use pipe “|” character to combine several commands.

sudo find / -name *.py | grep “game” | wc -l

what we did here is that with find command we found all file names. Then we feed this list to grep command which searches for word “game” in the list and then we simply count those words with wc -l.

We already are doing complex tasks here. Imagine the C or Python code you would write in order to achieve this.

Sometimes you simply need to create a sequence of several programs that would run one after other. This is practically called scripting. For instance we can make nice display of what we did:

echo “Counting word “game” in .py files”; sudo find / -name *.py | grep “game” | wc -l; echo “End”

command_sequence

If you need to output result to file just wrap all lines with brackets “( )” and use “>” symbol indicating that results should be sent to file:

(echo “Counting word “game” in .py files”; sudo find / -name *.py | grep “game” | wc -l; echo “End”) > gamecount

this writes results to file. In order to view its contents you can type

less gamecount

In a mean time you will notice that your commands get longer and complex. Or simply you get tired of typing same commands over and over again. This is where shell scripts come handy. Scripts are nothing more than set of commands to perform complex tasks.

Lets build a simple script that would take care of upgrading system. First of all lets go to out home directory

cd ~pi

Then lets create new directory myscripts and go to it:

mkdir myscripts; cd myscripts

Then we can create a file with name updateall.sh. Use nano editor to edit it.

Sudo nano updateall.sh

raspberry_update_bash_script

then we should modify the script file to be executable:

sudo chmod +x /home/pi/myscripts/updateall.sh

and then we can simply run it by typing

sudo ./updateall.sh

You can also include a reboot command in script in order to take upgrades to actions with simple command:

sudo shutdown -r 1

It restarts Raspberry Pi in 1 minute.

Scripting is main tool of Linux enthusiasts. This is where whole administration, customization is automated. Scripts can have variables, conditions, looks and more. This source might give some clue about this.

Leave a Reply

Your email address will not be published. Required fields are marked *