Find the answer to your Linux question:
Results 1 to 3 of 3
if I copy the echoed line and paste it right back into the cli, it runs just fine. The script errors out though. What am I doing wrong? THE SCRIPT ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2012
    Posts
    20

    bash script execcuting lpr command fails but echoed cmd works


    if I copy the echoed line and paste it right back into the cli, it runs just fine. The script errors out though.

    What am I doing wrong?

    THE SCRIPT
    Code:
    #!/bin/bash
    
    #is arg 1 null?
    if [ -z "$1" ]; then
    	echo "usage: $0 {document} {num_copies}. num_copies is optional"
    else
    	#default printer name follows colon
    	def_printer=$(lpstat -d | grep -o ':.*' | grep -o '[^:\\s]\+')
    	num_copies=1
    	doc_path="$1"
    
    	#is arg 2 populated
    	if [ -n "$2" ]; then
    		num_copies=$2
    	fi
    	
    	#the document should be a regular file
    	if [ -f "$doc_path" ]; then
    		echo "using printer $def_printer to print $num_copies copie[s] of $doc_path"
    	else
    		echo "could not find $doc_path"
    		exit 1
    	fi
    
    	echo lpr -P "$def_printer" -# "$num_copies" "$doc_path"
    	lpr -P "$def_printer" -# "$num_copies" "$doc_path"
    
    fi
    THE OUTPUT
    Code:
     ~/scripts $ ./print_document.bsh ~/test.print 
    using printer  HP-Officejet-6500-E710n-z to print 1 copie[s] of /home/kevin/test.print
    lpr -P  HP-Officejet-6500-E710n-z -# 1 /home/kevin/test.print
    lpr: The printer or class does not exist.

  2. #2
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    174
    One thing to check, is there a leading space in the value of the "def_printer" variable? If so the command in the script would include the leading blank as the argument to the -P flag, since it's in double quotes. But when you cut-n-paste it would get ignored.

    To check you could change the "echo" line to this,

    echo "lpr -P '$def_printer' -# '$num_copies' '$doc_path'"

    and the output will include single quotes around the flag variable. The reason I think that might be it is because the output of the current echo looks like it has 2 spaces between the "-P" and "HP-Office...".

  3. #3
    Just Joined!
    Join Date
    Jan 2012
    Posts
    20
    cnamejj you were correct!!
    the output of the echo was : lpr -P ' HP-Officejet-6500-E710n-z' -# '1' /home/kevin/test.print'

    the final script
    Code:
    #!/bin/bash
    
    #is arg 1 null?
    if [ -z "$1" ]; then
    	echo "usage: $0 {document} {num_copies}. num_copies is optional"
    else
    	#default printer name follows colon
    	def_printer=$(lpstat -d | grep -o ':.*' | grep -o '[^:\\s]\+' | sed 's/^ *//g')
    #	def_printer=$(echo $def_printer)
    	num_copies=1
    	doc_path="$1"
    
    	#is arg 2 populated
    	if [ -n "$2" ]; then
    		num_copies=$2
    	fi
    	
    	#the document should be a regular file
    	if [ -f "$doc_path" ]; then
    		echo "using printer $def_printer to print $num_copies copie[s] of $doc_path"
    	else
    		echo "could not find $doc_path"
    		exit 1
    	fi
    
    	echo "lpr -P '$def_printer' -# '$num_copies' $doc_path'"
    	lpr -P "$def_printer" -# "$num_copies" "$doc_path"
    
    fi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •