If you get tired of typing the same thing over and over and over again, or want to spend less time doing math in your head when you are trying to figure out what the results of a console command mean, that is where simple aliases and scripts come in. Below are some of my favorite simple console shortcut scripts. To see some of my favorite aliases, see this page.
Some of you may have no idea how to write a script and say, "Oh no, not something else that I have to figure out." So, let me try to explain a little how easy it really is to write a short script. Those of you who already know scriptwriting can skip to the example scripts by clicking here.
First, I will assume you know how to enter some rudimentary bash
commands from a console or terminal. If you don't use or want to learn how to use the command line at all, then this page is not for you. If you want to learn how to write a simple script, but do not yet already know how to enter commands, then I suggest reading my Linux for newbies page and, if you do not have a console installed yet on your Zaurus, my console tutorial page.
So, what is a script? It really is just a series of commands that you want your shell (usually bash
on the Zaurus) to run. And it is a whole lot easier to enter the name of a shell script from the command line than it is to have to keep entering those commands over and over and over again.
Writing your own scripts is not all that hard unless you want to have them do a lot of complicated stuff. In fact, while I think it would be more appropriate to just write an alias
to do it, you can even write a script that contains just one command.
For beginners, you may just want to try putting the sample short scripts I have posted here into files and learning how to run them, before you try to write your own scripts.
So, how do you turn your own commands into a script? What you need to do to write a simple basic shell script is have a list of commands you want to run, one after the other, sequentially. You type them into a file, let's call it "myfirstscript.sh," usually with just one command per line, using your favorite text editor. Then you put the following line into the script as the very first line:
#!/bin/bash
And it is also wise to enter the following as the very last line:
exit
When you get done, you will need to make sure that the shell knows it is a command file and not a common text file, so you enter the "chmod" command:
bash-2.05# chmod 700 myfirstscript.sh
And then to run the script you enter:
bash-2.05# ./myfirstscript.sh
So, let's say you want to run the "route" and "ifconfig" commands to check your online status. Your script, called online.sh, would look like this:
#!/bin/bash
route
ifconfig
exit
And you would enter the following to turn it into an executable file the first time, and then run it:
bash-2.05# chmod 700 online.sh
bash-2.05# ./online.sh
For another, more detailed example of how to install a script, see this part of my writeup about rgrep.
If your script does not run, and the reason why is not obvious, then bring it back to the forums or chat and our wonderful Linux community should be able to help. Just be sure you post a complete copy of the script somewhere, along with it's output, so we can help you figure out what went wrong more easily.
There are some commands that will not work the same way in a script that they do on the command line because the "working directory" is different. But, rather than try to learn all the possible problems that can occur and how to prevent them, I recommend just working on your first script. Then, if any problems occur because of shell, directory, or syntax issues, ask for help learning about them, one issue at a time. I think that is a lot easier than trying to learn how to prevent all possible problems.
Okay, now on to some examples of useful short scripts you can try out yourself. For explanations and examples of beginner shell scripts written by other authors, you can also see the Further Reading section below.
The first example of a short script on this page, my "see"
script, checks on the
current "at"
jobs on my Zaurus, and tells me their filenames and the scheduled times.
It calls Kyle Davenport's "udate" command
that converts unix epoch time to
a standard calendar date and time format.
The "udate" command is available for download here. The command is handy if you are doing calendar math on the Zaurus or other Linux machine, such as in writing alarm scripts.
Anyhow, here is the script to display "at"
jobs and their associated times. Note that the second and third lines start with a pound sign (#), which turns those lines into comments that the shell will ignore.
Note that other distributions may place "cron" jobs in a different directory or use different file naming conventions for the scheduled jobs.
So, you might need to edit the script to make it compatible with your particular operating system:
#!/bin/bash
a script to displayed scheduled
#
# see:"at" jobs
echo
ls /var/spool/at/1* | while read FILE
do echo -n `basename $FILE` " "
udate `basename $FILE [0-9]* `
done
exit
Sample output from "see"
is as follows:
bash-2.05# see
1177084440.5863 Fri Apr 20 08:54:00 2007
1177084620.5931 Fri Apr 20 08:57:00 2007
1177113600.3438 Fri Apr 20 17:00:00 2007
bash-2.05#
If you want a more elaborate, more detailed listing of scheduled "at"
jobs, check out Kyle's fancier atq script.
The next example of a short script, the "fb" script (for "free bytes"), tells me how much space is left in internal memory for my sl5500. For Zaurii running ROM 1.12, change "block1" to "block3". If you are running some other distribution, you will need to pick some unique identifier to use that will enable the script to identify the correct line to use in running the calculations:
#!/bin/bash
#
free=(`df | grep block1`)
free=$((${free[1]} - ${free[2]}))
echo There are $free kbytes free
exit
And here is some sample output:
bash-2.05# fb
There are 442 kbytes free
bash-2.05#
The next short script is one called "apdf", which I wrote when I was always mistyping the name of the "qpdf" application, and want to always see
how much free space is left after the application has finished.
It also sends all "standard error"
to /dev/null
(the Linux trash) instead of to my screen:
#!/bin/bash
#
echo Loading qpdf.....
echo
/mnt/card/Programs/qpdf 2> /dev/null
free
exit
Here is a fun short script that makes for less deciphering if you want to see the parent ID for a particular process, of which there is only one instance running. Did I say "fun?" Well, it is a lot more fun than studying the long output from "ps ax". It will only work properly if you have a fully featured version of ps on your Zaurus or Pi. If your "ps" command is the busybox version, you may want to consider installing a fuller featured version in order to take better advantage of the power available when using the command in your console or in a script such as the following.
#!/bin/bash
ps -C $1 -o " %c %p %P"
echo Parent is:
ps -p `ps -C $1 --noheader -o " %P "` --noheader -o " %c %p "
exit
Usage is "parent COMMANDNAME". And here is some sample output:
bash-2.05# parent qpe
COMMAND PID PPID
qpe 4832 284
Parent is:
qpe.sh 284
bash-2.05#
Note that this version of "parent" only works properly if there is only one copy or instance of the command or application of interest running. When I can, I will try to figure out how to get it to work properly for multiple instances, and update this page accordingly.
I have hunted around, and all my other scripts are much, much longer. To see a list of the other ones that I have written webpages about, then click here.
If you want a quick refresher or tool for learning basic commands or how to use the text editor, "vi," then I highly recommend http://networking.ringofsaturn.com/Unix/learnUNIXin10minutes.php, which was originally at freeengineer.org/learnUNIXin10minutes.html. If you are a beginner with "vi," I recommend using it in "command" rather than in "visual mode", as that is a lot less confusing to start with. In fact, I still do most of my editing in "vi" using command rather than visual mode.
Here are some simple webpages by other authors that teach how to write a short shell script, and can give you more examples:
First Shell Script (previously at http://steve-parker.org/sh/first.shtml, now offline, link below is archived copy)
Archive.org copy of http://shellscript.sh/first.html
Anatomy of a shell script (original page offline, link to Wayback Machine's copy is below)
Wayback Machine's copy of http://www.securitydocs.com/library/3424
Basic Linux Shell Scripting
http://www.2000trainers.com/linux/linux-shell-scripting-part3
If you are interested in a more classic, thorough treatment of shell scripting, then read the following:
Basic Shell Programming
http://www.lowfatlinux.com/linux-shell-script.html
Advanced Bash-Scripting Guide
http://tldp.org/LDP/abs/html/
Revised November 7, 2018