silenius

Untitled

Aug 20th, 2021 (edited)
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.80 KB | None | 0 0
  1. #!/bin/sh
  2. # After the driver is installed, add the printer device through the script
  3.  
  4. # Custom printer model
  5. VAR_NAME="DP30"
  6. # Supplier ppd file storage location
  7. VAR_VENDER="detong"
  8. # Default printer model, the model read from USB
  9. VAR_NAME_DEF="DP30"
  10. # ppd folder
  11. VAR_PPD_FILE="/usr/share/cups/model/${VAR_VENDER}/${VAR_NAME}.ppd"
  12. # VAR_PPD_FILE="${VAR_VENDER}/${VAR_NAME}.ppd"
  13. # Note: The printer name cannot have spaces
  14. # Add printer device name
  15. VAR_PRINTER_NAME="${VAR_NAME}-Label-Printer"
  16. VAR_PRINTER_NAME_DEF="${VAR_NAME_DEF}-Label-Printer"
  17.  
  18. # Note: The return value of the function can only be an integer, generally 0 means success, others means failure;
  19. # Define a function return value variable
  20. uri_device=""
  21.  
  22. # Function: Get usb device uri
  23. # usb device format: usb://DeTong/XXXX%20Label%20Printer?serial=048353482034
  24. getDeviceUri(){
  25.     echo "getDeviceUri()"
  26.     echo lpinfo -v
  27.     items=$(lpinfo -v)
  28.     echo $items
  29.     ITEM=""
  30.     # Traverse all USB ports
  31.     for item in $items
  32.     do
  33.         # echo $item "|" grep "usb://DeTong/${VAR_NAME_DEF}%20Label%20Printer"
  34.         ITEM=$(echo $item | grep "usb://DeTong/${VAR_NAME_DEF}%20Label%20Printer")
  35.         # echo "ITEM = $ITEM"
  36.         if [ "$ITEM" != '' ]; then
  37.             break;
  38.         fi
  39.     done
  40.     uri_device=$ITEM
  41.     echo "return: $uri_device"
  42. }
  43. # Function: Add printer device
  44. # param1: printer name
  45. # param2: usb device uri
  46. # param3: ppd file
  47. addPrinter() {
  48.     echo "addPrinter()"
  49.     echo "PrinterName = $1"
  50.     echo "DeviceUri = $2"
  51.     echo "ppd = $3"
  52.     # lpadmin -p XXXX-Label-Printer -E -v usb://DeTong/XXXX%20Label%20Printer?serial=048353482034 -P /usr/share/cups/model/detong/XXXX.ppd
  53.     # -P has been enabled, and may need to be replaced by -m later, but -m must be through the pad file found in lpinfo -m. The PPD file of the German Tong series found is (relative to "/usr/share/cups /model/" path):
  54.     # detong/XXXX.ppd DeTong XXXX Label Printer
  55.     # So the new printer adding instruction is:
  56.     # lpadmin -p XXXX-Label-Printer -E -v usb://DeTong/XXXX%20Label%20Printer?serial=048353482034 -m detong/XXXX.ppd
  57.     # lpadmin parameter introduction:
  58.     # -p: printer name
  59.     # -P: The full path of the ppd file (deprecated)
  60.     # -v: DeviceUri, usb device
  61.     # -m: The name of the ppd file obtained through lpinfo -m
  62.     if [ "$1" = '' ]; then
  63.         echo "The printer name is not set"
  64.     elif [ "$2" = '' ]; then
  65.         echo "No USB device detected"
  66.     elif [ "$3" = '' ]; then
  67.         echo "No PPD file specified"
  68.     else
  69.         echo lpadmin -p $1 -E -v $2 -P $3
  70.         lpadmin -p $1 -E -v $2 -P $3
  71.     fi
  72. }
  73. # Function: Delete printer device
  74. # $1: Printer name, eg: XXXX-Label-Printer
  75. removePrinter(){
  76.     echo "removePrinter($1)"
  77.     if [ "$1" != '' ]; then
  78.         echo lpadmin -x $1
  79.         lpadmin -x $1
  80.     else
  81.         echo "The name of the printer to be deleted is not specified"
  82.     fi
  83. }
  84.  
  85. main(){
  86.     echo "main()"
  87.     # 1. Get the deviceUri of the usb device
  88.     getDeviceUri
  89.     # 2. If the printer is not connected, exit the installation, it will not be used after installation, it is meaningless
  90.     if [ "$uri_device" = '' ]; then
  91.         echo "The printer of the corresponding model is not detected!"
  92.         uri_device="usb://DeTong/${VAR_NAME_DEF}%20Label%20Printer"
  93.         # return;
  94.     fi
  95.     # 2. When the customized model is inconsistent with the default model, delete the printer added by default (it is wrong to add the default printer PPD file)
  96.     if [ "$VAR_NAME" != "$VAR_NAME_DEF" ]; then
  97.         # Mainly for Ubuntu system
  98.         removePrinter $VAR_PRINTER_NAME_DEF
  99.     fi
  100.     # 3. Add printer
  101.     addPrinter $VAR_PRINTER_NAME $uri_device $VAR_PPD_FILE
  102.     # 4. Set the default printer
  103.     lpoptions -d $VAR_PRINTER_NAME
  104. }
  105. # Execute the main function
  106. main
Add Comment
Please, Sign In to add comment