[SOLVED] Bash Script for Adding Printers In RHEL 6.7
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Last edited by chris_carr; 04-11-2016 at 06:54 AM.
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".
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.