Help with script to automate CUPS printer installs


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with script to automate CUPS printer installs
# 1  
Old 12-27-2012
Help with script to automate CUPS printer installs

Good afternoon. Smilie

I'm rather new to bash scripting, and have probably bitten off a bit more than I can chew to be honest, but I work in a service desk-like environment where one of the main complaints is that it takes our staff a long time to set up network printers on our clients' servers. Being the only one who has dabbled in bash scripting previously, I have been given a task to make a script that will automate the creation of a number of entries in CUPS depending on a number of variables (in this case, manufacturer of printer, make of printer and how many printer trays will be installed). I've created a version that works for one make and model of printer, which works without issue.. The issue is where I've tried to give the users a "menu" of which models, manufacturers etc they can choose from.

Here is an initial snippet of the script, which is where I require the most help.. The end goal of this is to have three sections like this, one for each printer manufacturer, where the exact make and number of trays is chosen by the user - this will have the script choose which drivers etc to install.

My syntax might be horrid, and my tabulation maybe moreso, however please bear with me!

Code:
#!/bin/bash

# Declare CUPS driver variables
LJ1300=sog_hp_lj1300.ppd
B4350MF=sog_oki_b4350_mf.ppd
B4350T1=sog_oki_b4350_t1.ppd
B4350T2=sog_oki_b4350_t2.ppd
B4600T1=sog_oki_b4600_t1.ppd
B4600T2=sog_oki_b4600_t2.ppd
B6200T1=sog_oki_b6200_t1.ppd
B6200T2=sog_oki_b6200_t2.ppd
B6200T3=sog_oki_b6200_t3.ppd
B6250T1=sog_oki_b6250_t1.ppd
B6250T2=sog_oki_b6250_t2.ppd
B6250T3=sog_oki_b6250_t3.ppd
B6500T1=sog_oki_b6500_t1.ppd
B6500T2=sog_oki_b6500_t2.ppd
B710T1=sog_oki_b710_t1.ppd
B710T2=sog_oki_b710_t2.ppd
B710T3=sog_oki_b710_t3.ppd
C330=sog_oki_c330dn.ppd
C3400=sog_oki_C3400.ppd
C3600=sog_oki_c3600.ppd
TSP700=sog_star_tsp700.ppd
LJ1300=sog_hp_lj1300.ppd
LJ2605=sog_hp_2605.ppd
LJ4300T1=sog_hp_lj4300_t1.ppd
LJ4300T2=sog_hp_lj4300_t2.ppd
LJ4300T3=sog_hp_lj4300_t3.ppd
LJ4300T4=sog_hp_lj4300_t4.ppd
RICOHMF=sog_ricoh_sp3410dn_mf.ppd
RICOHT1=sog_ricoh_sp3410dn_t1.ppd
RICOHT2=sog_ricoh_sp3410dn_t2.ppd
# ----------------------------------------
# Make sure only root can run the script
if [ "$(id -u)" != "0" ]; then

    echo "
    Only Root can run this script
          Press OK
    "
   exit 1
fi

# ----------------------------------------

clear
    echo '************************************************************************'
    echo '*                  ******* W A R N I N G! *******                      *'
    echo '*                                                                      *'
    echo '*      You are starting the NETWORK printer installation script        *'
    echo '*  This process will install a printer on the destination server       *'
    echo '*  Please make sure this is run on the SERVER and not the CLIENT.      *'
    echo '*                                                                      *'
    echo '*  Do you wish to continue?  (Type the entire word "yes" to proceed.)  *'
    echo '*                                                                      *'
    echo '************************************************************************'
    read -p "Proceed with install? " response
    echo
# --------------------------------
if [ "$response" != "yes" ]; then
    exit 1
    else
    clear

# ----------------------------------------
# Choose make/model of printer

    clear
    echo "**************************************************************************"
    echo "* Select the manufacturer of the printer in question:                        *"
    echo "* [1] OKI                                                                    *"
    echo "* [2] HP                                                                    *"
    echo "* [3] Ricoh                                                                *"
    echo "**************************************************************************"
    read -p "* Please enter option [1 - 3] * " option
    
    case $option in
        "1")  clear
        echo "**************************************************************************"
      echo "* Select the model of the OKI printer:                                  *"
      echo "* [1] B4350                                                                *"
      echo "* [2] B4600                                                                *"
      echo "* [3] B6200                                                                *"
      echo "* [4] B6250                                                                *"
      echo "* [5] B6500                                                                *"
      echo "* [6] B710                                                                *"
      echo "* [7] C330DN                                                            *"
      echo "* [8] C3400                                                                *"
      echo "* [9] C3600                                                                *"
      echo "**************************************************************************"
      read -p "* Please enter option [1 - 3] * " option-oki
        if [ "$option-oki" = "1" ]; then
            read -p "How many paper trays will this printer have? Enter MF/1/2 : " b4350-trays
                if [ "$4350-trays" = "MF" ]; then
                DRV1=$B4350MF
                elif [ "$4350-trays" = "1" ]; then
                DRV1=$B4350MF
                DRV2=$B4350T1
                elif [ "$4350-trays" = "2" ]; then
                DRV1=$B4350MF
                DRV2=$B4350T1
                DRV3=$B4350T2
            fi
        if [ "$option-oki" = "2" ]; then
                read -p "How many paper trays will this printer have? Enter MF/1/2 : " b4600-trays
                if [ "$4600-trays" = "MF" ]; then
                DRV1=$B4600MF
                elif [ "$4600-trays" = "1" ]; then
                DRV1=$B4600MF
                DRV2=$B4600T1
                elif [ "$4600-trays" = "2" ]; then
                DRV1=$B4600MF
                DRV2=$B4600T1
                DRV3=$B4600T2
            fi
        if [ "$option-oki" = "3" ]; then
                read -n "How many paper trays will this printer have? Enter 1/2/3 : " b6200-trays
                if [ "$6200-trays" = "1" ]; then
                DRV1=$B6200T1
                elif [ "$6200-trays" = "2" ]; then
                DRV1=$B6200T1
                DRV2=$B6200T2
                elif [ "$6200-trays" = "3" ]; then
                DRV1=$B6200T1
                DRV2=$B6200T2
                DRV3=$B6200T3
            fi
        if [ "$option-oki" = "4" ]; then
                read -p "How many paper trays will this printer have? Enter 1/2/3 : " b6250-trays
                if [ "$6250-trays" = "1" ]; then
                DRV1=$B6250T1
                elif [ "$6250-trays" = "2" ]; then
                DRV1=$B6250T1
                DRV2=$B6250T2
                elif [ "$6250-trays" = "3" ]; then
                DRV1=$B6250T1
                DRV2=$B6250T2
                DRV3=$B6250T3
            fi
        if [ "$option-oki" = "5"]; then
                read -p "How many paper trays will this printer have? Enter MF/1/2 : " b6500-trays
                if [ "$b6500-trays" = "1" ]; then
                DRV1=$B4600MF
                elif [ "$b6500-trays" = "2" ]; then
                DRV1=$B4600MF
                DRV2=$B4600T1
            fi
        if [ "$option-oki" = "6" ]; then
                read -p "How many paper trays will this printer have? Enter 1/2/3 : " b710-trays
                if [ "$b710-trays" = "1" ]; then
                DRV1=$B710T1
                elif [ "$b710-trays" = "2" ]; then
                DRV1=$B710T1
                DRV2=$B710T2
                elif [ "$b710-trays" = "3" ]; then
                DRV1=$B710T1
                DRV2=$B710T2
                DRV3=$B710T3
            fi
        if [ "$option-oki" = "7" ]; then
                DRV1=$C330
            fi
        if [ "$option-oki" = "8" ]; then
                DRV1=$C3400
            fi
        if [ "$option-oki" = "9" ]; then
                DRV1=$C3600
            fi
        else
            exit 1
        fi
    esac
# ----------------------------------------
# Test output
echo "Test output"
echo "${DRV1}"
echo "${DRV2}"
echo "${DRV3}"
exit 1

When running this, I'm getting a syntax error on line 181 (esac). Have I just messed up on my syntax, or is there something else amiss here?

Many thanks!
# 2  
Old 12-27-2012
You've gotten 17 ifs in your code snippet but 11 fis only, so I guess there sth. wrong with those. Taking a closer look, the if [ "$response" != "yes" ] seems to be missing one, but, more related to the syntax error that you quote, every if [ "$option-oki"...] has an unterminated if - elif - elif construct in it, which will bother bash if it reaches the esac statement. Pls correct and come back with the result.
BTW, did you consider bash's select statement to offer and read a selection? And, I'd prefer to put the respective options every printer offers into an option file that you can print out for selection and that you can easily adapt should a new printer model come in. Plus, why don't you collect e.g. all three-tray-printers into one single question (option_OKI between 1 and 6 -> ask for tray) and generate the printer driver name from the few variables read in answer?

Last edited by RudiC; 12-27-2012 at 01:37 PM..
# 3  
Old 12-27-2012
Hi RudiC,

Thanks for the speedy response. I'll go through my code and amend the unterminated if/elif statments first - am I correct in thinking I'll simply need to add the second fi in after the last elif? Eg:

Code:
if [ "$option-oki" = "1" ]; then
            read -p "How many paper trays will this printer have? Enter MF/1/2 : " b4350-trays
                if [ "$4350-trays" = "MF" ]; then
                DRV1=$B4350MF
                elif [ "$4350-trays" = "1" ]; then
                DRV1=$B4350MF
                DRV2=$B4350T1
                elif [ "$4350-trays" = "2" ]; then
                DRV1=$B4350MF
                DRV2=$B4350T1
                DRV3=$B4350T2
            fi
    fi

If I'm wrong, apologies. I never thought of using select in all honesty, I (at least thought I) knew the if, so I decided to stick in my comfort zone. If it would make more sense using select, I'll look into that.

What do you mean by an options file? Sorry if I'm a bit slow.

Kind regards.
# 4  
Old 12-27-2012
I don't have a ready-to-use solution at hand, but I could imagine a file called OKIprinters contain the following lines with <TAB> separated columns:
Code:
B4350   MF/1/2  sog_oki_b4350_mf.ppd    sog_oki_b4350_t1.ppd    sog_oki_b4350_t2.ppd
B4600   MF/1/2  sog_oki_b4600_t1.ppd    sog_oki_b4600_t2.ppd
B6200   1/2/3   sog_oki_b6200_t1.ppd    sog_oki_b6200_t2.ppd    sog_oki_b6200_t3.ppd
B6250   1/2/3   sog_oki_b6250_t1.ppd    sog_oki_b6250_t2.ppd    sog_oki_b6250_t3.ppd
B6500   1/2     sog_oki_b6500_t1.ppd    sog_oki_b6500_t2.ppd
B710    1/2/3   sog_oki_b710_t1.ppd     sog_oki_b710_t2.ppd     sog_oki_b710_t3.ppd
C330DN  1       sog_oki_c330dn.ppd
C3400   1       sog_oki_C3400.ppd
C3600   1       sog_oki_c3600.ppd

, then a script containing
Code:
$ OKIS=$(cut -f1 OKIprinters)
$ select Pr in $OKIS; do echo $Pr, $REPLY; done
1) B4350
2) B4600
3) B6200
4) B6250
5) B6500
6) B710
7) C330DN
8) C3400
9) C3600
#? 3
B6200, 3

, and you could go on from there, selecting the trays, and then the drivers. If you got a new printer, add a line to your file(s), and it flies.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Printer configuration Migration from Solaris 10 "LP" to Solaris 11 "CUPS"

Need to find a way to import an LP printers.conf file to CUPS. I have some new Solaris 11.1 boxes that need to have 300 printers added. (0 Replies)
Discussion started by: os2mac
0 Replies

2. Red Hat

Samba and Cups :: How to Hide Printer Icons

Hi, I am running Linux box with samba-3.0.33 cups-1.3.7 When I access my home folder through windows and give user credentials, it works well but shows all the printers icons as well. I would like to hide them all from users. I tried changing Browsing On to Browsing Off in... (1 Reply)
Discussion started by: amit_spl
1 Replies

3. UNIX for Dummies Questions & Answers

Query for printer driver in CUPS

I have a RHEL 4 (probably not important, but whatever) machine with a few dozen network printers set up in CUPS. I would like to replicate this on a second machine. With lpinfo I'm able to get the queue name and device information (socket://ipaddress or whatnot), but I haven't get figured out... (0 Replies)
Discussion started by: kknigga
0 Replies

4. SCO

Replace Line Printer Daemon (LPD) with CUPS

hi Howto replace Line Printer Daemon (LPD) with CUPS on SCO 5.0.6? (3 Replies)
Discussion started by: ccc
3 Replies

5. Red Hat

Need a single printer driver added to CUPS

Just got an HP OfficeJet L7680 printer but there are no drivers for it in RHEL5. I just want to add the single driver without installing hplip 3.9.8. It's going to be connected with USB. Is this possible and if so how? (1 Reply)
Discussion started by: deloev
1 Replies

6. UNIX for Advanced & Expert Users

Script that can Automate Printer adding task in HP-UX servers

I want to make a script to automate printer adding task.My inputs are like Printer name : xyz Port number :9001 I should write a script to make the Printer adding task will be automated. Like in manually adding task we are doing through hppi or jetadmin tools. ---------- Post updated at... (2 Replies)
Discussion started by: AnilKPatnaik
2 Replies

7. Linux

mapping of a printer model with a ppd file in CUPS

Hi all, I am currently working on building a GUI to be interfaced with CUPS 1.3.4 package; In my GUI I have a list of printer manufacturers mapped With various printer models ; and for a particular printer model selected I needed to know how to map that model with an Appropriate ppd file; as I... (0 Replies)
Discussion started by: sc3008
0 Replies

8. Shell Programming and Scripting

here document to automate perl script that call script

I am trying to use a here document to automate testing a perl script however when the perl script hits a system(perl subscript.pl) call, input is no longer entered into this subscript. here is my script $ cat test.sh #ksh for testcase do program <<-EOF | tee -a funcscnlog.log y... (3 Replies)
Discussion started by: hogger84
3 Replies

9. Linux

Sharing Printer Linux + XP (cups/samba)

I keep getting a access denied error when I install from the XP wizard my Linux shared printer. CUPS CONFIGURATION FILE <Location /admin> # # You definitely will want to limit access to the administration functions. # The default configuration requires a local connection from a user who #... (2 Replies)
Discussion started by: Alux
2 Replies

10. UNIX for Dummies Questions & Answers

Sharing printer with windows via cups and samba

I want to use my linux box as a print server for my hp printer. I downloaded the linux driver for the printer and installed it. I also configured CUPS and printed a test page with it. Samba is allowing my windows machine to see and install the printer, but when i try to print to it, nothing... (1 Reply)
Discussion started by: Spetnik
1 Replies
Login or Register to Ask a Question