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
}
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.
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?