Operating System - HP-UX
1827703 Members
2983 Online
109967 Solutions
New Discussion

Re: How to define a list of values within a shell script

 
SOLVED
Go to solution
Raymond E. Lilly
Frequent Advisor

How to define a list of values within a shell script

Suffering from a severe lack of imagination today.

I have a script that loops through an input file. I want to eliminate the input file and define a list directly in the script itself.

Here is the loop I'm using today:

while read -r input
do
some statements using $input
done < file

My input file list looks like this:
Entry1
Entry2
Entry3
EntryN (can have more or less lines)

I can get LIST="Entry1,Entry2,Entry3,EntryN" to work using a 'for' loop with 'echo' and 'cut', but my list Entrys can be longish making the line difficult to edit.

I'd like to define the list with one line per entry, but can't come up with a way to load it into a variable and loop through it.

Entry1
Entry2
Entry3
EntryN

What is the best way to do this?

Ray
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to define a list of values within a shell script

Hi Ray:

Why not create a here-doc within your script? Consider:

# cat mysh
#!/usr/bin/sh
HEREDOC=/tmp/sh.$$
trap 'rm ${HEREDOC}' EXIT
cat <<-EOF! > ${HEREDOC}
entry1 of 5
entry2 of another line
entry3 of 5
entry4 of still more
entry5 of yet more
EOF!
while read LINE
do
echo "I saw '${LINE}'"
done < ${HEREDOC}
exit 0

...

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: How to define a list of values within a shell script

Shalom Ray,

I do not totally understand your input.

I would think this statement or a variation might help:

var=$(echo $input | awk '{print $1}')

Thing is you don't tell us what triggers the end of an entry so its hard to provide specific code.

awk can do a lot of things.

awk -F:

Will let you print or put in variables data separated by :

Lots of possible data formats here.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dennis Handly
Acclaimed Contributor

Re: How to define a list of values within a shell script

>JRF: cat <<-EOF! > ${HEREDOC}

You can remove this evil cat by:
while read LINE; do
echo "I saw '${LINE}'"
done <<-EOF!
entry1 of 5
...
EOF!
James R. Ferguson
Acclaimed Contributor

Re: How to define a list of values within a shell script

Hi (again):

> Dennis: You can remove this evil cat

That's very nice! I never thought to try that with a here-document. Not only does your method eliminate the extra 'cat' process, but it obviates the need for the trap to remove the temporary file. Very nice indeed. Thanks for sharing!

Regards!

...JRF...
Raymond E. Lilly
Frequent Advisor

Re: How to define a list of values within a shell script

James, that was exactly what I needed.

Dennis, great refinement!

Steven, I also incorporated awk to help tame the unruly string entries in my list.

Thanks to you all,
Ray