You've successfully subscribed to MyPad Blog
Great! Next, complete checkout for full access to MyPad Blog
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Calling a bash script with parameters | Demo to get IP address of any domain

Calling a bash script with parameters | Demo to get IP address of any domain

In this post, lets look at solving a recurring task of having to launch bash scripts with custom parameters.

I'll demonstrate this with a sample problem of Getting the IP address of any domain

There are various ways of solving this problem, but my persistent preference is to use the all powerful terminal and keep things simple and minimalistic.

Parameters to a script can be simple passed like below:

bash './shell/custom-shell-script-get-domain.sh' yahoo.com

The script for ./shell/custom-shell-script-get-domain.sh is as follows:

#!/bin/bash

URL=$1
echo "URL is: $URL"

IP=$(dig +short $URL | head -1)
echo "IP of $URL is: $IP"

Calling-a-bash-script-with-parameters-1

Get IPs of multiple domains

Getting the IP of domains is pretty simple too!

The script that loops through the domains called custom-shell-script-get-domains-loop.sh is as follows:

#!/bin/bash

# execute using:
# bash './shell/custom-shell-script-get-domains-loop.sh' 'google.com' 'yahoo.com' 'ft.com' 'wsj.com'

for domain in "$@"
do
  URL=$domain
  echo "URL is: $URL"

  IP=$(dig +short $URL | head -1)
  echo "IP of $URL is: $IP"
done

Execution is pretty simple again!

bash './shell/custom-shell-script-get-domains-loop.sh' 'google.com' 'yahoo.com' 'ft.com' 'wsj.com'

get-ips-of-passed-domains-using-a-bash-script-loop