Print Script
Tutorial Lecture
TheCAT
11/19/2014
TheCAT
11/19/2014
Objective: Send a
printer test page to a group of networked printers. Ideally, the test
page has a date and user name. Additionally it should be in color and
span the depth and breadth of the page. This script can then be used
in conjunction with lab checks to ensure that the printers in the
labs are in good working order and so that an individual that
encounters the page can understand its purpose.
Minimum Command
Requirements:
man
lp or lpr
chmod
Optional Commands:
date
cat
convert
lpoptions
& many more
possible
Possible Syntax:
for, do, done
if then else fi
case esac
exit
shift
Tools:
nano/vim/emacs
a .jpg or two
Side note: A print script
can range from 5 lines to 150 or more. You can use any language and
there are no hard and fast rules beyond the objectives – which are
more of a guideline.
First let's look at
what lp and lpr do.
user@machine$ man lp
user@machine$ man
lpr
While I personally
think that lp is a little more versatile offering a lot of options I
actually used lpr for my script – no real rhyme or reason there so
consider either one a viable alternative.
So you can see that
you are ultimately going to have to provide some data to the command
like the name of the printer and the file to print.
But what to do when
there is more than one printer?
Well, we could just
run the command multiple times, each time updating the name of the
printer to reflect the next printer on our list of printers. In fact
that is exactly what we are going to do, but we aren't going to do it
by hand, we are going to put it in a script.
One method for doing
something a lot of times is to put it in a for-loop. In this case each
iteration of the loop corresponds to printing the same file to a new
printer.
You now have the
foundation for the entire script; the rest is just refining the bits
of what we discussed so far. And for that we will work
individually... more on this at the very end. Other questions to ask
yourself
* What are the
printers?
- do these live in
the script?
- do they live in a
separate file?
* What file to use?
- do I create my
own image?
- Text or picture?
* How to get the
date on it?
* How to get a name
on it?
* Do I test all the
printers at once?
But before we start
coding (and after we finish solving the problem) let's start with
something we will need at the top of any script: the magic cookie. No
not that chocolate yummy thing. This is a line at the top of our file
that reads as follows:
#! /bin/bash
This tells kernel to
run the script in a bash shell. So when you run a command from the
command line, there is a fork command that creates and entirely new
shell which is an exact copy of the shell currently running. We want
to make certain that when the script is run, that the new shell is
also a bash shell. This falls under the execve command that follows
(but we now we are way out of scope)...
Lastly let's discuss
chmod. Change file mode allows us to set permissions for a file. On
our systems when you create a new file the permissions default to:
-rw------- meaning only we can read and write to it, but cannot
execute. For a text file this isn't terrible, but since we are
building a script we will want to make it executable. Additionally
there is the consideration of whether we want other people to run our
script. Do you want to let other cats run it? Or anyone at all? For
now let's assume that we want to make it so that only we can run it.
When we are done, we will want to:
chmod 700 my_script
Now let's get to work and start thinking about how to set up what we need based on the previous bullet point questions....
For Loop Syntax
for item in apple banana cherry; do
echo $item
done
for number in {1..9}; do
echo $number
done
for f in `ls`; do
For Loop Syntax
for item in apple banana cherry; do
echo $item
done
for number in {1..9}; do
echo $number
done
for f in `ls`; do
echo $f
done
No comments:
Post a Comment