Visit The New, Modern Unix Linux Community


Call same function using 2 different arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Call same function using 2 different arguments
# 1  
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!

Code:
cat backup.sh
#!/bin/bash

function usage {
	  echo "USAGE: $(basename $0) <host1> <host2>"
	  exit 1
}

function backup_run () {
ssh -q <host> "test -x /tmp/abc"
if [ $? -eq 0 ]; then
	echo  "ssh -q <host> /tmp/abc"
fi
}

if [[.....]]; then
	backup_run <host1>
else
	backup_run <host2>
fi

# 2  
What exactly do you want to achieve that above doesn't deliver? What problems do you run into? Errors? Messages?
# 3  
Kindly excuse my first post. Here is my actual script and I looked around to see if I could edit the first post. I'm trying to find how to pass 2 arguments to the script which in turn passes those 2 arguments to the function. I can make it work for one argument using $1 within the function.

Code:
#./backup.sh <host1> <host2>
# cat backup.sh
#!/bin/bash

function usage {
	  echo "USAGE: $(basename $0) <host1> <host2>"
	  exit 1
}
if [[ $# != 2 ]]; then
  usage
fi

function backup_run () {
SITE=$(ssh -q $HOST cat /serverinfo | awk '{print $2}' 2>/dev/null)
echo $SITE
HOST=$(ssh -q $HOST hostname|cut -c1,2,3,4 >/dev/null)
echo $HOST

#Setting SITE based on region
case $SITE in
  AAA)
    region="AAA"
    ;;
  BBB)
    region="BBB"
    ;;
  *)
    echo "FAIL: Incorrect region"
    exit 1
    ;;
esac

#OS Version
REL=$(ssh -q $HOST rpm -q redhat-release-server 2>/dev/null)

# Print KEY value based on host, site and os version
if [[ ${HOST} == "PROD" ]]; then
  KEY="${SITE} NEW ${REL}"
else
  KEY="${SITE} OLD ${REL}"
fi
}

backup_run <host1>
backup_run <host2>

# 4  
use "$@" (= all parameters, quoted). There is a lot further interesting to know about parameters. see man bash

Last edited by stomp; 09-11-2019 at 07:52 PM..
# 5  
Quote:
Originally Posted by mbak
... passes those 2 arguments to the function. I can make it work for one argument using $1 within the function.
...
So you succeeded, calling backup_run $1? And your next step would be backup_run $2, then? How about for looping across all positional parameters, using $@ as proposed by stomp?
# 6  
There are several ways to tackle this (looping through the arguments)

Code:
while [ $# -gt 0 ]
do
     backup_run "$1"
     shift
done

The above uses shift which throws away each $1 argument after it's used.

Or these solutions leave the positional arguments alone and simply loop thru them:

Code:
for arg
do
    backup_run "$arg"
done

for arg in "$@"
do
    backup_run "$arg"
done

for((pos=1 ; pos <= $# ; pos++))
do
    backup_run "${!pos}"
done

edit: I have updated to allow calling the script with arguments that contain white space
# 7  
Thank you for all the responses.
I tested using for loop which worked out well and ended up using backup_run $1 followed by backup_run $2 as I had to call function for each argument at 2 different locations within the script.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #375
Difficulty: Easy
In December 2008, the Austin Group published a new major Unix revision, known as POSIX:2008 (formally: IEEE Std 1013.1-2008).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to call Oracle function with multiple arguments from shell script?

Dear All, I want to know how can i call oracle function from shell script code . My oracle function have around 5 input parameters and one return value. for name in *.csv; do echo "connecting to DB and start processing '$name' file at " echo "csv file name=$x" sqlplus -s scoot/tiger <!... (2 Replies)
Discussion started by: Balraj
2 Replies

2. 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

3. Shell Programming and Scripting

How to funnel stdout into call arguments?

This command successfully gives me the clearcase views I want in stdout, one line per view name. Each view name appears to have no spaces. cleartool lsview | grep -i `uname -n` | grep -v "ViewStorage\\|vgr_tools_sv" | cut -f 3 -d ' ' How can write a cygwin bash "for" loop that will iterate... (4 Replies)
Discussion started by: siegfried
4 Replies

4. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

5. 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

6. UNIX for Dummies Questions & Answers

write() system call arguments

Hi, I'm trying to understand the arguments from this system call, can someone help me figure it out? write(1, "/home/nick/11sp/fred\n", 27/home/nick/11sp/fred) = 27 for argument 1, i know it is a file descriptor which specifies standard output. Argument 2, i believe is "what is to be... (4 Replies)
Discussion started by: l flipboi l
4 Replies

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

8. Shell Programming and Scripting

How to call arguments with variable in a script??

Hello, I was wondering if it were possible to call arguments passed to a script using a variable. For example: sh script.sh yes no good bad x=$# while do echo (last argument, then second last etc until first argument) let x=($x-1) done should print out bad good no (4 Replies)
Discussion started by: VanK
4 Replies

9. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

10. Programming

can we send arguments to system() call

Hi friends my C code is int main() { system("cp <source> <destination>"); } my question is how to set variables for <source> and <destination> how can we pass it to system() call. can you suggest me thankyou kingskar (6 Replies)
Discussion started by: kingskar
6 Replies

Featured Tech Videos