LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash Script for Adding Printers In RHEL 6.7 (https://www.linuxquestions.org/questions/linux-general-1/bash-script-for-adding-printers-in-rhel-6-7-a-4175577002/)

chris_carr 04-08-2016 01:13 PM

Bash Script for Adding Printers In RHEL 6.7
 
Ok,

It has been a while since I wrote a bash script and I need a little help.

I'm charged with migrating printers from our virtual environment to our all-in-one cloud system. I been adding the printers manually via the lpadmin command, but I'm coming up on a huge migration were I have to add about 100 printers, and my wrist hurt just thinking about adding them manually. I have written a script in bash doing a basic for loop, but I can remember how to make the loop cat more than one file.


Code:

#!/bin/bash

Printer_List=printer-list1
Device_List=/root/scripts/printers/deviceURI-list1
PPD_List=ppd-list1

echo
echo

for x in `cat $Printer_List`;do
  echo "Adding Printer For $x"
  lpadmin -p "$x" -E -v "$Device_List" -P "$PPD_List"

  cupsaccept $x
  cupsenable $x
done

echo

Any help would be awesome.

P.S sorry the code format is wrong. I cant remember how you guys make it all pretty.

MensaWater 04-09-2016 12:52 PM

You can cat more than one file simply by giving multiple arguments to cat command (e.g. cat file1 file2 file3 ...)

Since you're doing a variable you can set the variable equal to multiple files:

Code:

#!/bin/bash

Printer_List="printer-list1 printer-list2 printer-list3"

for x in `cat $Printer_List`;do
echo "Adding Printer For $x"
done

You can put code tags around your code by highlighting it and clicking the # icon for "code". (Or manually type left "[" followed by the word "code" closing with "]" at beginning of your code then doing same thing at end of code except you put in "/code" instead of just "code".

chris_carr 04-11-2016 07:49 AM

What you are saying makes sense, but I can not seem to put it into action. How would I do this with a long lpadmin command?

I dont seem to understand how to make the script assign different variables for the different list.


Code:

lpadmin -p "printer_name" -E -v "device_URL" -P "device_ppd"

MensaWater 04-11-2016 09:06 AM

Ah, I misunderstood. On rereading I see you're trying to get separate values from 3 separate files for different variables in your lpadmin. Assuming you have the same number of lines in each file with each having a single column you can use the "paste" command to join them together as 3 separate columns. You'd then use a while loop in combination with the "read" command that sets variables based on input.


Code:

#!/bin/bash

Printer_List=printer-list1
Device_List=/root/scripts/printers/deviceURI-list1
PPD_List=ppd-list1

echo
echo

paste $Printer_List $Device_list $PPD_List |while read name uri ppd
do
  echo "Adding Printer For $name"
  lpadmin -p "$name" -E -v "$uri" -P "$ppd"

  cupsaccept $name
  cupsenable $name
done

echo


chris_carr 04-11-2016 09:20 AM

YES!!!!

That did the trick. Thank you so much for your help. You are a life saver.


All times are GMT -5. The time now is 04:26 AM.