Raspberry Pi for Kids: Making Linux Scripts
You can join Linux commands together to make your own scripts for your Raspberry Pi. You might do this for two reasons. One is if you have a long one-line command with lots of switches that you type over and over. The other is to make your own custom smart command that does a job you need done, but isn’t included in basic Linux.
The simple way to make short one-line commands is to use a special command called alias.
Here are some examples that give hard-to-remember commands easy-to-remember names.
alias dir=“ls” alias copy=“cp” alias del=“rm –i”
So now when you type dir and press enter, you get the ls command instead, and so on for the others.
Linux forgets the aliases you make when you reboot, so you have to put the alias commands in a special file called .bashrc. (The period matters – it tells Linux it’s a hidden file.)
Use cd ~ to move to your home directory, type the following and press Enter.
Then you can use the nano editor to add the commands you want to use. (You can use any Linux command at all.)
You can also put the commands in a file called .bash_profile, which runs the commands when you login.
.bashrc runs the commands when you open a new terminal window – which isn’t always what you want, although it’s fine if you mostly use the desktop.
The other way to make your own Linux commands is to collect them into a file with a .sh extension. The first line of the file must be
The rest of the file can be a list of any Linux commands. Just put the commands one after another, each on its own line. Save the file and set the +x permission so that you can run it.
To use it, type
Obviously, change the filename to whatever you called your file. If you’re not in your home directory, you have to type the full path to the script. (There’s a way to fix this. Search online for “Linux PATH” for details.)
Here’s an amazing thing: In Linux, you can even pass information from one command to another or to a file.
You can learn many, many tricks here. Here are a couple of simple tricks.
To pass the output of one command to the input of another command, use the pipe character |. For example:
ls – Al | less
less is a super-useful command that splits up text so that it fits into pages on your screen. If there’s a lot of text, you get to see it page by page without it scrolling past you. Hold down Control and press Z to quit less.
A particularly useful command is grep, which looks for matching words or letters. Say that you want to see all the files in a folder made in July:
Ls –Al | grep “Jul”
To save the output to a file, use the redirect character >. For example:
ls –Al > myfilelist.txt
You’ve just saved your file listing to a file.
At this point, you can see how you can build some really powerful commands out of the simpler commands included in Linux. The commands are more like a toolkit you can clip together than a finished do-everything solution for working with files.
There’s a lot more you can learn about writing your own scripts. Search online for “Linux shell scripting” for more information and plenty of examples.