Skip to main content
Question

Bash script question - variable as a list?


Forum|alt.badge.img+7

We're transitioning over to a new printing system and I need to remove printers on all macOS machines. The printers are all named by site, for example Miami HP 504, Dallas HP 653 etc. 

What's a good way to delete the printers in bulk by using the site names? I have a script that works for one site at a time. I don't want to just remove all printers as there may be personal printers installed. 

7 replies

jamf-42
Forum|alt.badge.img+17
  • Esteemed Contributor
  • 744 replies
  • May 10, 2024

bash arrays would be the answer maybe? 

https://linuxhandbook.com/bash-arrays/


howie_isaacks
Forum|alt.badge.img+23
  • Esteemed Contributor
  • 773 replies
  • May 10, 2024

This may work. I was playing around with using a for loop. It just deleted the printers from my Mac. You would add at list of your printer names to the "printers" variable.

#!/bin/zsh printers=("Printer1" "Printer2") for printer in ${printers[@]}; do /usr/sbin/lpadmin -x "$printer" done

 


jamf-42
Forum|alt.badge.img+17
  • Esteemed Contributor
  • 744 replies
  • May 10, 2024
howie_isaacks wrote:

This may work. I was playing around with using a for loop. It just deleted the printers from my Mac. You would add at list of your printer names to the "printers" variable.

#!/bin/zsh printers=("Printer1" "Printer2") for printer in ${printers[@]}; do /usr/sbin/lpadmin -x "$printer" done

 


yup.. that works and if there are 'a lot' you could always pull in the list from a CSV


howie_isaacks
Forum|alt.badge.img+23
  • Esteemed Contributor
  • 773 replies
  • May 14, 2024
jamf-42 wrote:

yup.. that works and if there are 'a lot' you could always pull in the list from a CSV


I'm curious how you would do this from a CSV. I think I know how and when I have time I will try it later.


jamf-42
Forum|alt.badge.img+17
  • Esteemed Contributor
  • 744 replies
  • May 14, 2024
howie_isaacks wrote:

I'm curious how you would do this from a CSV. I think I know how and when I have time I will try it later.


foo=$(cat path/to/text.csv)


Forum|alt.badge.img+7
  • Author
  • Contributor
  • 77 replies
  • May 14, 2024

I ended up doing:

#!/bin/bash

lpstat -a | cut -d " " -f1 | grep "Miami_" | while read MIAMI
do
echo "-- Miami printers detected attempting to delete:" $MIAMI
lpadmin -x $MIAMI
sleep 1
done

 

and just repeat for each site . . .


dlondon
Forum|alt.badge.img+14
  • Honored Contributor
  • 375 replies
  • May 16, 2024

You could potentially make a loop around what you've done there Chris and then use a Here Doc.  Here's an example script for Here Doc's

#!/bin/bash # here_doc_test.bash SITES=$(cat <<EOF Miami_ Orlando_ Austin_ Phoenix_ EOF ) echo $SITES for i in $SITES do echo $i done

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings