Multithreading - Calling user function with xargs in korn shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multithreading - Calling user function with xargs in korn shell
# 1  
Old 05-20-2013
Multithreading - Calling user function with xargs in korn shell

I know there are other ways of accomplishing the below task, but the purpose of this thread is to understand the below code.

I wanted to use xargs with user defined function in korn shell. Am aware, that I could write custom function into a script and place it in FPATH and then call it in xargs, but I wanted to have it all in the same shell script. Bash isnt an option for me at the moment. After 2-3 days of searching, I found the solution which works, but am unable to understand a part of it..

Code:
cat ordernum.txt | xargs -P 25 -n 1 ksh -c '
  function fun { 
    do some processing
  } 
  shift "$1" 
  fun "$@"' 2 1

Can someone help me with the last two lines of the code.

Thanks a ton.

Last edited by luhah; 05-20-2013 at 04:23 AM.. Reason: Please use code tags
# 2  
Old 05-20-2013
shift moves the parameters so that $2 is now $1,etc.
$@
PHP Code:
Inside double quote marks (" "), parameter and command substitution occur and \ quotes the characters \, `, ", and $. A $ in front of a double quoted string will be ignored in the "C" or "POSIX" locale, and may cause the string to be replaced by a locale specific string otherwise. The meaning of $* and $@ is identical when not quoted or when used as a variable assignment value or as a file name. However, when used as a command argument, "$*" is equivalent to "$1d $2d . . .", where d is the first character of the IFS variable, whereas "$@" is equivalent to "$1"  "$2"  . . . . Inside grave quote marks (` `), \ quotes the characters \, `, and $. If the grave quotes occur within double quotesthen also quotes the character ". 
Code:
> cat test.sh
shift "$1"
echo "$@" 2 1

> test.sh 1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 2 1

> test.sh 1
2 1

> test.sh 1 2
2 2 1

# 3  
Old 05-20-2013
I was thinking, it would go into endless loop, as the function "fun" is being declared and at the end it being called as well..Smilie within the function
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling sqlplus from Korn shell heredoc issue

Hi, I am facing an issue wherein some temporary files (here docs) are getting created in /tmp and are not getting deleted automatically. When i check the list of open files with below command i can see one file is getting appended continuously.(In this case /tmp/sfe7h.34p) The output is... (4 Replies)
Discussion started by: Navin_Ramdhami
4 Replies

2. Shell Programming and Scripting

Pass values to case statement in a function korn shell

I'm in the process of writng a function that consists of a case statement is there a way of calling the function and passing a value to it? ie function1 () { case opt1 do ..... opt2 do..... esac } function opt1 I'm aware the syntax is not correct, but you get the general idea. (1 Reply)
Discussion started by: squrcles
1 Replies

3. UNIX for Advanced & Expert Users

Calling PERL from a Korn shell script

I am currently in Afghanistan and do not have access to some of the resources I normally do back in the US. Just accessed this site and it looks promising! Hopefully you will not find my question too much of a waste of your time. I write mostly Korn Shell and PERL on Solaris systems for the... (2 Replies)
Discussion started by: mseanowen
2 Replies

4. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

5. Shell Programming and Scripting

korn shell - export function??

Hi, I have a question. I know that if I want to access any variables defined in a calling script (script1.ksh) in a called script (script2.ksh) I need to export that variable in the calling script to make it available in the called script. But what about the functions in the calling script? ... (1 Reply)
Discussion started by: dips_ag
1 Replies

6. Shell Programming and Scripting

Calling external function in a shell

hi guys, how r u??? please I need you, help me please. I have a shell, in this shell i have this function and another code lines, this function is getting date one day back. the function is in the same shell (FILE 1) Now I need put this function in another file (FILE 2) and calling... (4 Replies)
Discussion started by: acevallo
4 Replies

7. Shell Programming and Scripting

calling a function in Shell script troubleshooting

Some Code After Some code part is executed the control doesnt go to rvin_doxx_scrt.. and the script exits rvin_doxx_scrt() { Some Code } if (som code) ... (4 Replies)
Discussion started by: ultimatix
4 Replies

8. Shell Programming and Scripting

Calling a C-function froma shell script

Hi, I have searched the forum for the query, But i didnt find an exact answer. I have a script(1.sh) and a c program(sample.c) sample.c contains many function definitions.( run(), find(), add() etc). I want to call functions in sample.c from 1.sh and use the return value in 1.sh... (3 Replies)
Discussion started by: jisha
3 Replies

9. UNIX for Dummies Questions & Answers

calling one function from another shell script

i have a function defined in one ksh (ksh 1) i want to use that function in another ksh (ksh 2) i am using . $<directoryname>/<ksh name> i am calling the function defined in ksh 1 in ksh 2 i want the returnstatus from the above operation but it is not executing the function what i... (1 Reply)
Discussion started by: trichyselva
1 Replies

10. UNIX for Dummies Questions & Answers

calling a c function from shell

Is it possible to call a C function from within a shell script. This C function is part of an API. What do I need to make it work from my shell script. Anybody please help. (4 Replies)
Discussion started by: seshagiri
4 Replies
Login or Register to Ask a Question