1

I have read the contents of a folder and store them in an array. And need to pass this array to a script. How can I store and pass the array and read that array??

#!/usr/bin/ksh

cd /path/applications-war
arrayWar=( $(ls /path/applications-war))

I need all the contents under this folder into an array (@arrayWar). I will login into another box and call a script. I need to pass this array to the script.

/usr/bin/ssh -t -t username@machinename /path/myscript.sh @arrayWar

Inside myscript.sh, I want to compare the passed array @arrayWar with ServicesArray.

#!/bin/ksh
 @arrayWar = $1
 ServicesArray=('abc.war' 'xyz.war')
   for warfile in @arrayWar
     do
       if  echo "${ServicesArray[@]}" | fgrep  "$warfile"; then
            echo "$warfile matches"
       else
            echo "$warfile not matched" 
       fi
    done
5
  • really, ksh with spaces around your = sign? Don't you get an error message? Good luck. Commented May 8, 2013 at 14:26
  • @fedorqui: My issue is not with white spaces. It is to pass elements into an array and compare two arrays. Commented May 8, 2013 at 14:34
  • You cannot pass arrays in shells; you can only pass the contents of an array as a sequence of positional arguments. Commented May 8, 2013 at 14:36
  • @chepner How can I pass the contents of an array? Commented May 8, 2013 at 14:40
  • @sravs448 "${arrayWar[@]}" (double quotes essential) expands to the contents of that array. Commented May 8, 2013 at 14:48

2 Answers 2

2

Here's your script, which takes a variable number of files as arguments:

#!/bin/ksh
ServicesArray=('abc.war' 'xyz.war')

for warfile in "${@##*/}"
  do
   if  echo "${ServicesArray[@]}" | fgrep  "$warfile"; then
        echo "$warfile matches"
   else
        echo "$warfile not matched" 
   fi
 done

You call your script like this (note that using ls is not recommended):

arrayWar=( /path/applications-war/* )
/usr/bin/ssh -t -t username@machinename /path/myscript.sh "@{arrayWar[@]}"

You can also dispense with arrayWar, and pass the list of files directly

/usr/bin/ssh -t -t username@machinename /path/myscript.sh /path/applications-war/*
Sign up to request clarification or add additional context in comments.

5 Comments

If I pass just the list of files directly, will "$@" will have the list for comparing?
Yes. From your script's perspective, the two are equivalent. ${arrayWar[@]} and /path/applications-war/* are expanded by the shell to identical lists of words.
I tried /path/applications-war/* . But its showing all as not matched, even though the list contains the elements in ServiceArray. Because its listing as \path\applicatipns-war\abc.war and \path\applications-war\xyz.war. And its comparing that with abc.war and xyz.war. Hence everything is printing as not matched. How can get only abc.war and xyz.war or do we need to change the way we are doing the grep ??
I have edited the above code to get only abc.war and xyz.war into an array. But I am getting an error near the awk statement. line 7: syntax error near unexpected token ('`
My apologies; I forgot that the bare wildcard would include the full path name, where calling ls gives only the base name.
0

As chepner pointed out, you can't pass the array, but there are a couple of ways around that. The first would be to pass them as a series of positional arguments, I believe the limit on those is 9. If you have more than 9 items in that array, or you would like to do this in a more permanent manner, you could also fairly easily write this in BASH (I'm not familiar with ksh, I did a quick Google and the syntax appears fairly similar). I'll use the output of ls in this example

\#!/bin/bash


\# Lets say this gives it 'myFile' and 'testFile'
ls > myArrayFile

\# Need to change IFS to accurately split the list, this splits by newline
IFS=$’\x0a’

\# Set your array
compareArray=('myFile' 'testFile' 'someOtherStuff')

\# Standard loop to compare arrays

for genItem in `cat myArrayFile`;
do
    for staticItem in $compareArray;
    do
    if $genItem == $staticItem;
        then
        echo "$genItem is in the static array"
    fi
done
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.