UNIX: passing stuff to a shell function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX: passing stuff to a shell function
# 1  
Old 02-08-2017
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?

Code:
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
}

# 2  
Old 02-08-2017
Code:
Print "$list2" $printer2

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-08-2017
That simple!
It works.
I made a lot of tries but not that one.
Thanks.
This User Gave Thanks to emare For This Post:
# 4  
Old 02-08-2017
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
# 5  
Old 02-09-2017
Quote:
Originally Posted by emare
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?

Code:
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
}

Given that (without options) ls produces output sorted by pathname, why use:
Code:
list2=`ls /spooldocs/sch/sch* |sort`

instead of:
Code:
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:
Code:
list2=`ls /spooldocs/sch/sch* |sort`

instead of:
Code:
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?
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 02-09-2017
Would it not also be simpler to use the -n flag of lp to print multiple copies?
# 7  
Old 02-09-2017
@Don Cragun & rbatte1

there is always room for improvement and learning.

Thank you for that.
I'm following your advices.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Passing variables from UNIX to Ansible to UNIX shell

I m passing a variable stringg from Unix shell which has value 'Good Day' to ansible and from ansible to a second shell script where it print only Good instead of 'Good Day' passing the variable stringg from unix shell script1.sh echo $stringg ansible-playbook install.yml -i... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

3. Shell Programming and Scripting

Passing sql as parameter to unix function

Hi, I have a function which connects to the db and runs the sql. it works fine when I run it like: function "select empname from emp;" but when I try to pass the sql string to a variable which in turn in fed to the function , it throws error. please advise. Thanks, Arnie. (1 Reply)
Discussion started by: itsarnie
1 Replies

4. Shell Programming and Scripting

Passing a value to stored procedure from unix shell script

Hi Dudes :) I want a unix shell script to pass value to SQL stored procedure. Below is the procedure declare res varchar2(10); begin odm_load_check('PRE_SANITY',res); dbms_output.put_line(res); end; select * from error_log; truncate table error_log; select * from test; (1 Reply)
Discussion started by: shirdi
1 Replies

5. UNIX for Dummies Questions & Answers

Help Passing An Oracle parameter to a unix shell.

I have an Oracle concurrent program that I'm passing a parameter to a unix shell script. An example value of the Oracle parameter is PO_TOP. The Oracle parameter represents the unix env var PO_TOP, meaning, on the unix side there is env var called PO_TOP (ex value: /oradev/apps/po/11.0.3/). My... (7 Replies)
Discussion started by: Mark_Wright
7 Replies

6. Shell Programming and Scripting

SSH - Passing Unix login passwords through shell scripts

Hi All , I need to call a script runscript_B.sh on server A, the runscript_B.sh script locating in server B. The runscript_B.sh in calls another script runscript_A on server A itself. it seend, i need to be connect from Server A to Server B using ssh. I have tryed like this in... (3 Replies)
Discussion started by: koti_rama
3 Replies

7. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

8. Shell Programming and Scripting

Passing the value of Out parm in SP to UNIX Shell Script

I have a simple procedure which accepts two input parms and returns and output count: CREATE OR REPLACE Procedure GET1 ( IN_FOLDER IN VARCHAR2, IN_SUBJECT_AREA IN VARCHAR2, OUT_CNT IN OUT VARCHAR2 ) AS BEGIN ... (2 Replies)
Discussion started by: Hangman2
2 Replies

9. UNIX for Advanced & Expert Users

Passing a unix variable value to a Plsql function

Suppose I have a unix variable called RGNM which is holding a value. Now I want to call a plsql function in my script. THis plsql function takes one IN parameter. I want to pass my UNIX VARIABLE Value to the plsql function. Can i just give it by giving $RGNM in the function after calling sqlplus... (1 Reply)
Discussion started by: cobroraj
1 Replies

10. Shell Programming and Scripting

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (1 Reply)
Discussion started by: csrazdan
1 Replies
Login or Register to Ask a Question