Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Invoke Function WithOut Non-Positional Arguments Post 302883867 by Ariean on Thursday 16th of January 2014 12:16:01 PM
Old 01-16-2014
Invoke Function WithOut Non-Positional Arguments

Hello Everyone,

Is there a way i can pass the arguments as parameters or variables instead of positional arguments to a function, below i am calling the function defined in a script.

Call:
Code:
notify "Error While Generating The List File: ${GEN_PARAM_LIST9} For Feed Data Validation Errors In Main" "${GEN_PARAM_LOG9}" "${LOG_FILE}"

Can I do the call like below, because my call sometimes i would just call with only subject and i don't want to include a log file, sometimes i want to put Cc and sometimes don't want Cc. So I would like to have a better control on how i call or invoke instead of doing positional parameter passing as it would mess up my scripts and lot of maintenance. How do i do that?

Code:
noticy s="Error While Generating The List File: ${GEN_PARAM_LIST9} For Feed Data Validation Errors In Main" L1="${GEN_PARAM_LOG9}"  L2="${LOG_FILE}"


Script:
Code:
### Function To Send An Email
notify()
{
#This Function Arguments Should Follow Below Order Only
# $1 - Subject
# $2 - File(Message Body)
# $3 - File(Message Attachment)

if [ ! -z "$3" ]
then
        /usr/bin/mutt -s "$1" -i "$2" -a "$3" "$EMAIL_LIST"</dev/null
        elif [ ! -z "$2" ]
        then
                /usr/bin/mutt -s "$1" -a "$2" "$EMAIL_LIST"</dev/null
                elif [ ! -z "$1" ]
                then
                        /usr/bin/mutt -s "$1" "$EMAIL_LIST"</dev/null
                        else
                                /usr/bin/mutt -s "Error No Subject/Argument" "$EMAIL_LIST"</dev/null
fi
}


Thank you.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Invoke Perl function from Bash ?

Is it possible to invoke a perl function from a bash script ? There are existing perl scripts with many functions that I want to reuse from a more recent script written in bash. Hence the question. (1 Reply)
Discussion started by: NewDeb
1 Replies

2. Shell Programming and Scripting

no of arguments to function in shell script

Hi, I have a function in shell script fun1{ echo "No.of arguments are..."} this function will be called in same script by passing arguments fun 1 2 3 I want to check the no. of arguments passed to fun1 function in the same functionbefore validation. can any one suggest me. (2 Replies)
Discussion started by: KiranKumarKarre
2 Replies

3. Programming

Error: too many arguments to function 'sigwait'

#include <pthread.h> #include <signal.h> ... sigset_t mask; int err,signo; err=sigwait(&mask,&signo); switch(signo){ case SIGINT: ... } when I compile above code under solaris 10,it raise following error: error: too many arguments to function 'sigwait' I look up signal... (4 Replies)
Discussion started by: konvalo
4 Replies

4. Shell Programming and Scripting

cat arguments to a function

Hi, I've a logging function in bourne shell, flog() which logs the first argument passed to it. How can I pass arguments to this function from a file, like cat filename | sed '...filtering...' | flog or cat filename | sed '...filtering...' | xargs flog Which did not work, after which... (3 Replies)
Discussion started by: Random_Net
3 Replies

5. UNIX for Advanced & Expert Users

Function not called when no arguments is passed

Hi Guys, I am trying to pass arguments to the script i am wrinting. When no argument is passed or wrong argument is passed, the script needs to output the way it needs to be called and exit. Currently, when no arguments is passed, it is not getting exited but goes on assuming those... (3 Replies)
Discussion started by: mac4rfree
3 Replies

6. Shell Programming and Scripting

bash read within function with arguments

I have trouble getting this logic to work #!/bin/bash function assign_var(){ while do read -p "$2 :" $3 done } assign_var '$IPADDRESS' ipaddress IPADDRESS Basicly, i want to make sure that entry is made (i can add more sophisticated checks later), but the idea is to recycle... (11 Replies)
Discussion started by: serverchief
11 Replies

7. Shell Programming and Scripting

Problem while Invoke Shell Script function from Java Program

Hi, I have create a Shell Script, with one function. I want to call the script file in Java Program. It working fine. but the problem is the function in the Shell Script is not executed. Please suggest me, Regards, Nanthagopal A (2 Replies)
Discussion started by: nanthagopal
2 Replies

8. Shell Programming and Scripting

Need to call a function with arguments

I need to call a function within a code with $database and $ service as the arguments How do I proceed ? and how would a function be defined and these two arguments would be used inside the function? calc_pref_avail $database $service Best regards, Vishal (7 Replies)
Discussion started by: Vishal_dba
7 Replies

9. Shell Programming and Scripting

Call same function using 2 different arguments

I have a script that uses 2 arguments. I want to call the function part within this script using these same arguments. Below is what I came up with below script so far, any guidance would be helpful. Thank you! cat backup.sh #!/bin/bash function usage { echo "USAGE: $(basename $0)... (6 Replies)
Discussion started by: mbak
6 Replies

10. UNIX for Beginners Questions & Answers

Positional Parameters Arguments/Variables when using dot (.)

Hi, Is there a special positional variables for when using the dot (.)? Scripts are as below: $: head -100 x.ksh /tmp/y.ksh ==> x.ksh <== #!/bin/ksh # . /tmp/y.ksh 1234 abcd echo "yvar1 = $yvar1" echo "yvar2 = $yvar2" ==> /tmp/y.ksh <== #!/bin/ksh (2 Replies)
Discussion started by: newbie_01
2 Replies
Kwargs(3pm)						User Contributed Perl Documentation					       Kwargs(3pm)

NAME
Kwargs - Simple, clean handing of named/keyword arguments. VERSION
version 0.01 SYNOPSIS
use Kwargs; # just named my ($foo, $bar, baz) = kw @_, qw(foo bar baz); # positional followed by named my ($pos, $opt_one, $opt_two) = kwn @_, 1, qw(opt_one opt_two) # just a hashref my $opts = kw @_; # positional then hashref my ($one, $two, $opts) = kwn @_, 2; WHY
? Named arguments are good, especially when you take lots of (sometimes optional) arguments. There are two styles of passing named arguments (by convention) in perl though, with and without braces: sub foo { my $args = shift; my $bar = $args->{bar}; } foo({ bar => 'baz' }); sub bar { my %args = @_; my $foo = $args{foo}; } bar(foo => 'baz'); If you want to support both calling styles (because it should be mainly a style issue), then you have to do something like this: sub foo { my $args = ref $_[0] eq 'HASH' ? $_[0] : { @_ }; my $bar = $args->{bar}; } Which is annoying, and not even entirely correct. What if someone wanted to pass in a tied object for their optional arguments? That could work, but what are the right semantics for checking for it? It also gets uglier if you want to unpack your keyword arguments in one line for clarity: sub foo { my ($one, $two, $three) = @{ ref $_[0] eq 'HASH' ? $_[0] : { @_ } }{qw(one two three) }; } Did I say clarity? HAHAHAHAHA! Surely no one would actually put something like that in his code. Except I found myself typing this very thing, and That Is Why. EXPORTS
Two functions (kw and kwn) are exported by default. You can also ask for them individually or rename them to something else. See Sub::Exporter for details. kw(@array, @names) Short for "kwn(@array, 0, @names)" kwn(@array, $number_of_positional_args, @names) Conceptually shifts off n positional arguments from array, then figures out whether the rest of the array is a list of key-value pairs or a single argument (usually, but not necessarily, a hashref). If you passed in any @names, these are used as keys into the hash, and the values at those keys are appended to any positional arguments and returned. If you do not pass @names, you will get a hashref (or whatever the single argument was, like a tied object) back. Note that if the single argument cannot be dereferenced as a hashref, this can die. No attempt is made by this module to handle the exception. AUTHOR
Paul Driver <frodwith@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Paul Driver <frodwith@cpan.org>. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.12.4 2011-01-24 Kwargs(3pm)
All times are GMT -4. The time now is 10:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy