# Call same function using 2 different arguments

**URL:** <https://community.unix.com/t/call-same-function-using-2-different-arguments/376252>\
**Category:** Shell Programming and Scripting\
**Created:** [September 11, 2019, 2:48pm UTC](https://community.unix.com/t/call-same-function-using-2-different-arguments/376252 "2019-09-11T14:48:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbak](https://community.unix.com/letter_avatar/mbak/32/6_449c533f586939b6e5f246632a46421f.png) [@mbak](https://community.unix.com/u/mbak)\
**Post date:** [September 11, 2019, 2:48pm UTC](https://community.unix.com/t/call-same-function-using-2-different-arguments/376252/1 "2019-09-11T14:48:55Z")

</div>

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!

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [September 11, 2019, 4:03pm UTC](https://community.unix.com/t/call-same-function-using-2-different-arguments/376252/2 "2019-09-11T16:03:32Z")

</div>

What exactly do you want to achieve that above doesn't deliver? What problems do you run into? Errors? Messages?

---

<div class="post-metadata">

**Author:** ![mbak](https://community.unix.com/letter_avatar/mbak/32/6_449c533f586939b6e5f246632a46421f.png) [@mbak](https://community.unix.com/u/mbak)\
**Post date:** [September 11, 2019, 5:37pm UTC](https://community.unix.com/t/call-same-function-using-2-different-arguments/376252/3 "2019-09-11T17:37:02Z")

</div>

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.

```nohighlight
#./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>

```

---

<div class="post-metadata">

**Author:** ![joker](https://community.unix.com/user_avatar/community.unix.com/joker/32/10678_2.png) [@joker](https://community.unix.com/u/joker)\
**Post date:** [September 11, 2019, 6:42pm UTC](https://community.unix.com/t/call-same-function-using-2-different-arguments/376252/4 "2019-09-11T18:42:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [September 12, 2019, 5:03am UTC](https://community.unix.com/t/call-same-function-using-2-different-arguments/376252/5 "2019-09-12T05:03:38Z")

</div>

> [@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?

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [September 12, 2019, 4:32pm UTC](https://community.unix.com/t/call-same-function-using-2-different-arguments/376252/6 "2019-09-12T16:32:59Z")

</div>

There are several ways to tackle this (looping through the arguments)

```nohighlight
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:

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![mbak](https://community.unix.com/letter_avatar/mbak/32/6_449c533f586939b6e5f246632a46421f.png) [@mbak](https://community.unix.com/u/mbak)\
**Post date:** [September 13, 2019, 1:23am UTC](https://community.unix.com/t/call-same-function-using-2-different-arguments/376252/7 "2019-09-13T01:23:37Z")

</div>

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.
