0

I am having a script in which I am declaring arrays like below:

declare -a Pool=("Terrace|HouseTerrace_")
declare -a Library=("lib1 lib2 lib3")
declare -a Name=("Guru/Raju Albert Deepak")
declare -a Email=("[email protected],[email protected] [email protected] [email protected]")
Sub=("Media Rotation")
i=0
while [ $i -lt ${#Pool[@]}  ]
do
command blah balh blah
i=`expr $i + 1`
done

Now what i want is that instead of declaring values in aaray here i should import it from file (either text or csv ) or is there any better way perfroming this task. I don want to give access to script file to users.

3
  • Look into the mapfile bash builtin. Commented Nov 29, 2019 at 4:47
  • @GURUSINGH: I don't understand what you are going to do here, all five of your 'arrays' consist of a single element only, so what's the point in declaring them as arrays? Commented Nov 29, 2019 at 8:30
  • @ user1934428 I am novice.give me better way.. Commented Nov 29, 2019 at 8:33

1 Answer 1

0

This is a partial answer, to help kick-start the OP effort.

The bash "readarray" can be used to load an array from a file. For example they code declare -a email=("[email protected]" "[email protected]" ...) can be replaced with

readarray email < email.txt

Where email.txt is (using posted data)

[email protected]
[email protected]
[email protected]
...

Another suggest improvement will be to use for item in instead of while loops to iterate over arrays. Instead of i=0 ; while [ $i -lt ${#Pool[@]} ] ; do ... ; done

for p in "${pool[@]}" ; do
   command $p
done
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Dash-o but my requirement is that for Pool "Terrace", select Library "lib1", select the name "Guru/Raju" and drop a mail to "[email protected],[email protected]". and again then second pool name,second lib,second name ...this way.It's like pick every first argument from each array and run the script.
The answer is giving you a template as the requirement are not very detailed. If you need a specific entry to include multiple lines with comma separator, put that line into the text file. I strongly suggest that you will read one of the 'intro to bash programming' documents.

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.