I ended up figuring it out. It helps to read the logs after you run your script. haha :)
I know you solved this, but just to give you some ideas... you can use the IP address of the printer to delete it if you know the address. There is most likely an easier way to do this, but this is what I did back in the day and it worked:
#!/bin/bash
oldPrinter=$(/usr/bin/lpstat -s | grep 10.75.12.75 | awk {'print $3'} | sed s'/.$//')
if [[ ${oldPrinter} ]]; then
${lpa} -x ${oldPrinter}
fi
If you know the name of the printer, but not the upper/lower of the name, or if there are spaces in the name, you can use wildcards to delete.
#!/bin/bash
printers=$(lpstat -p | awk '{print $2}' | sed '/^$/d')
for i in "${printers[@]}"
do
if [[ ${i} == *"printer_name"* ]] || [[ ${i} == *"printer name"* ]]; then
lpadmin -x ${i}
fi
done
And I eventually came up with using a function and an array of printers for our scripts:
#!/bin/bash
function remove_printers {
# looping function to remove all printers passed
printer=("$@")
printers=$(/usr/bin/lpstat -p | awk '{print $2}' | sed '/^$/d')
echo "*** List: $printers"
for i in "${printer[@]}"
do
for j in "${printers[@]}"
do
if [[ ${j} == *"$i"* ]]; then
echo "*** Removing: $j"
/usr/sbin/lpadmin -x ${j} 2>/dev/null
fi
done
done
}
printer_array=(
"Printer 1"
"Printer 2"
)
remove_printers "${printer_array[@]}"
I ended up figuring it out. It helps to read the logs after you run your script. haha :)
Can you provide me with the script that you were using? I know the name and the IP address of my printer that I am trying to remove but not having any luck with removing just one printer. I can get the script to remove all to work, but would like to just remove the ones I need to.
Can you provide me with the script that you were using? I know the name and the IP address of my printer that I am trying to remove but not having any luck with removing just one printer. I can get the script to remove all to work, but would like to just remove the ones I need to.
@mramola either of the scripts I shared below would work.
For the first script, replace the IP address in the grep statement with the IP of the printer. If that IP address is found on the system it will get removed via the if/then statement. The 'oldPrinter' variable is running 'lpstat -s' to get the list of printers and their corresponding IP addresses. It then uses grep to look for the one specific IP you want.
The second script, change the "printer_name" variable in the if/then statement to be the name of the printer you are trying to remove. The *"pritner_name"* is looking for anything with "printer_name" in it and *"printer name"* is looking for anything with "printer name" in case someone added that printer with a space in the name.
The third script, just replace the entries in the "printer_array" with the name of the printer(s) that you want to remove.