Shell script with array parameters

Hello Readers,

This post shows an example to write a shell script that accepts 2 array parameters. The size of array parameters need not be same.

Below script takes 2 array parameters and just prints the elements.


#!/bin/bash
ALPHABETS_ARR=$1
NUMBERS_ARR=$2

echo "printing alphabets..."
for i in ${ALPHABETS_ARR[@]}
do
echo ${i}
# do something glorious here...
done

echo "printing numbers..."
for i in ${NUMBERS_ARR[@]}
do
echo ${i}
# do something glorious here also...
done

exit

Usage:

<dir_of_script>/run_script_array_params.sh 'a b c d' '1 2 3'

Output:


printing alphabets...
 a
 b
 c
 d
 printing numbers...
 1
 2
 3

If you want to run this script via ssh, first check if you can run commands over ssh. You can copy the keys at remote server so that you are not prompted for password during ssh. Run below for check:

ssh <remote_server> date

If date is returned then you can invoke above script like this:

ssh <remote_server> "<dir_of_script>/run_script_array_params.sh 'a b c d' '1 2 3'"

Your’s Truly

Advertisements

About Badal Chowdhary

I am a Software Engineer by profession. I have done SCJP and SCWCD certifications. Like working on cutting edge technologies and frameworks. Driven by challenges and fascinated by technology. I love playing and watching sports: Cricket, Ping Pong, Tennis, Badminton, Racket Ball and Gym.
This entry was posted in Unix and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s