UNIX: passing stuff to a shell function

I have users that print files to selected printers. Instead of creating one function for each printer I would like to have just one and passing the files to print as well as the wanted printer. The following code does not work, of course.
I'm expecting that $1 is the list of files to be printed and $2 the printer; but $1 and $2 are the first two files in $list2.
Using $@ will carry all variables including also the $printer2, but that it is not useful.
Can anyone point me to the correct solution?

printer2=some1printer1
list2=`ls /spooldocs/sch/sch* |sort`
Print $list2 $printer2

Print(){
  for job in $1 ; do
    if [[ $job == *503374*.pcl ]]; then
      lp -d$2 $job
      lp -d$2 $job
    fi
    lp -d$2 $job
    lp -d$2 $job
    lp -d$2 $job
    lp -d$2 $job
  done
}
Print "$list2" $printer2
1 Like

That simple!
It works.
I made a lot of tries but not that one.
Thanks.

1 Like

Hi.

Glad to hear that you have a solution that works for you.

If I were doing this, I would make the printer the first argument, followed by the list of files. Then you would not need to remember to enclose the list in quotes for the call to the function Print, you could still loop through the files in the list, etc.

Best wishes ... cheers, drl

Given that (without options) ls produces output sorted by pathname, why use:

list2=`ls /spooldocs/sch/sch* |sort`

instead of:

list2=`ls /spooldocs/sch/sch*`

which produces the same output unless one or more of the filenames expanded from the pattern /spooldocs/sch/sch* contains a <newline> character and avoids the need to invoke the sort utility?

Given that the shell will expand the pattern /spooldocs/sch/sch* in a for statement into a list of matching pathnames sorted by pathname and without losing track of the boundaries between pathnames even if some of those pathnames contain <space>, <tab>, or <newline> characters, why use:

list2=`ls /spooldocs/sch/sch* |sort`

instead of:

list2=/spooldocs/sch/sch*

which with your code will produce the same results without the need to create a subshell for the command substitution, without the need to invoke ls , without the need to invoke sort , and without the possibility of splitting pathnames that contain whitespace characters?

1 Like

Would it not also be simpler to use the -n flag of lp to print multiple copies?

@Don Cragun & rbatte1

there is always room for improvement and learning.

Thank you for that.
I'm following your advices.