- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: How to define a list of values within a shell ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2010 09:05 AM
03-04-2010 09:05 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2010 09:26 AM
03-04-2010 09:26 AM
SolutionWhy 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...
- Tags:
- here doc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2010 09:30 AM
03-04-2010 09:30 AM
Re: How to define a list of values within a shell script
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2010 04:39 AM
03-05-2010 04:39 AM
Re: How to define a list of values within a shell script
You can remove this evil cat by:
while read LINE; do
echo "I saw '${LINE}'"
done <<-EOF!
entry1 of 5
...
EOF!
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2010 05:22 AM
03-05-2010 05:22 AM
Re: How to define a list of values within a shell script
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2010 10:56 AM
03-05-2010 10:56 AM
Re: How to define a list of values within a shell script
Dennis, great refinement!
Steven, I also incorporated awk to help tame the unruly string entries in my list.
Thanks to you all,
Ray