Register
It is currently Fri Apr 18, 2014 5:16 pm

Setting up a script to automate adding printers in CUPS


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Dec 27, 2012 10:02 am   

Joined: Thu Dec 27, 2012 9:58 am
Posts: 1
Good afternoon. :)

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!


Top
 Profile  
 PostPosted: Thu Dec 27, 2012 7:36 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
hi,

this is why indentation is so important!
take great care of it.

instead of many if [ $option_oki = number ]
use a case

and indent seriously.
the keyword that ends a compound command has to be in the same column that the keyword that starts the same compound command.
for example:
Code:
if
then
   if
   then
      if
      then
      fi
   fi
fi
this way you usually quickly see where it's wrong.


Top
 Profile  
 PostPosted: Sun Dec 30, 2012 7:09 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
It seems to be missing some syntax in the case statement...

Code:
case $option in
      "1")  clear


The "1" case should finised with ";;"
Code:
case $option in
  "1") echo one
  ;;
  "2") echo two ;;
esac


I also suggest changing the large number of multiple "if"s into a case statement.
I also suggest that dialog or similar will give a nicer UI.
Brief dialog howto;
http://unstableme.blogspot.co.uk/2009/12/linux-dialog-utility-short-tutorial.html

Lastly running bash with a -x switch can give some debug info which can help.

DW


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP