Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # After the driver is installed, add the printer device through the script
- # Custom printer model
- VAR_NAME="DP30"
- # Supplier ppd file storage location
- VAR_VENDER="detong"
- # Default printer model, the model read from USB
- VAR_NAME_DEF="DP30"
- # ppd folder
- VAR_PPD_FILE="/usr/share/cups/model/${VAR_VENDER}/${VAR_NAME}.ppd"
- # VAR_PPD_FILE="${VAR_VENDER}/${VAR_NAME}.ppd"
- # Note: The printer name cannot have spaces
- # Add printer device name
- VAR_PRINTER_NAME="${VAR_NAME}-Label-Printer"
- VAR_PRINTER_NAME_DEF="${VAR_NAME_DEF}-Label-Printer"
- # Note: The return value of the function can only be an integer, generally 0 means success, others means failure;
- # Define a function return value variable
- uri_device=""
- # Function: Get usb device uri
- # usb device format: usb://DeTong/XXXX%20Label%20Printer?serial=048353482034
- getDeviceUri(){
- echo "getDeviceUri()"
- echo lpinfo -v
- items=$(lpinfo -v)
- echo $items
- ITEM=""
- # Traverse all USB ports
- for item in $items
- do
- # echo $item "|" grep "usb://DeTong/${VAR_NAME_DEF}%20Label%20Printer"
- ITEM=$(echo $item | grep "usb://DeTong/${VAR_NAME_DEF}%20Label%20Printer")
- # echo "ITEM = $ITEM"
- if [ "$ITEM" != '' ]; then
- break;
- fi
- done
- uri_device=$ITEM
- echo "return: $uri_device"
- }
- # Function: Add printer device
- # param1: printer name
- # param2: usb device uri
- # param3: ppd file
- addPrinter() {
- echo "addPrinter()"
- echo "PrinterName = $1"
- echo "DeviceUri = $2"
- echo "ppd = $3"
- # lpadmin -p XXXX-Label-Printer -E -v usb://DeTong/XXXX%20Label%20Printer?serial=048353482034 -P /usr/share/cups/model/detong/XXXX.ppd
- # -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):
- # detong/XXXX.ppd DeTong XXXX Label Printer
- # So the new printer adding instruction is:
- # lpadmin -p XXXX-Label-Printer -E -v usb://DeTong/XXXX%20Label%20Printer?serial=048353482034 -m detong/XXXX.ppd
- # lpadmin parameter introduction:
- # -p: printer name
- # -P: The full path of the ppd file (deprecated)
- # -v: DeviceUri, usb device
- # -m: The name of the ppd file obtained through lpinfo -m
- if [ "$1" = '' ]; then
- echo "The printer name is not set"
- elif [ "$2" = '' ]; then
- echo "No USB device detected"
- elif [ "$3" = '' ]; then
- echo "No PPD file specified"
- else
- echo lpadmin -p $1 -E -v $2 -P $3
- lpadmin -p $1 -E -v $2 -P $3
- fi
- }
- # Function: Delete printer device
- # $1: Printer name, eg: XXXX-Label-Printer
- removePrinter(){
- echo "removePrinter($1)"
- if [ "$1" != '' ]; then
- echo lpadmin -x $1
- lpadmin -x $1
- else
- echo "The name of the printer to be deleted is not specified"
- fi
- }
- main(){
- echo "main()"
- # 1. Get the deviceUri of the usb device
- getDeviceUri
- # 2. If the printer is not connected, exit the installation, it will not be used after installation, it is meaningless
- if [ "$uri_device" = '' ]; then
- echo "The printer of the corresponding model is not detected!"
- uri_device="usb://DeTong/${VAR_NAME_DEF}%20Label%20Printer"
- # return;
- fi
- # 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)
- if [ "$VAR_NAME" != "$VAR_NAME_DEF" ]; then
- # Mainly for Ubuntu system
- removePrinter $VAR_PRINTER_NAME_DEF
- fi
- # 3. Add printer
- addPrinter $VAR_PRINTER_NAME $uri_device $VAR_PPD_FILE
- # 4. Set the default printer
- lpoptions -d $VAR_PRINTER_NAME
- }
- # Execute the main function
- main
Add Comment
Please, Sign In to add comment