- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Arrays in 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
12-15-2003 09:50 AM
12-15-2003 09:50 AM
I want to put some values in a array in my script and check them for a condition.
Ex.
***************
arr[0]=value1
arr[1]=value2
arr[2]=value3
var=value3
while(arr.length)
if ($var == arr[x])
then
do somthing
fi
***************
Can someone help me!
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2003 10:01 AM
12-15-2003 10:01 AM
Re: Arrays in shell
arr[1]=value2
arr[2]=value3
var=value3
typeset -i10 I=0
while [[ ${I} -lt ${#arr[*]} ]]
do
if [[ "${arr[${I}]}" = "${var}" ]]
then
echo "Do something"
fi
(( I += 1 ))
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2003 10:08 AM
12-15-2003 10:08 AM
Re: Arrays in shell
Thanks for the reply. Can you explain few more things. How do I put the values in the array and what does "typeset -i10 I=0 " this line do. Is it min and max values of array?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2003 10:21 AM
12-15-2003 10:21 AM
Re: Arrays in shell
the typeset defines the variable l as 10 character integer and initializes it with zero. The index of a one dimensional array is limited to 1023. The assignment, you choose is ok. Where you get your data from, is up to you.
greetings,
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2003 10:34 AM
12-15-2003 10:34 AM
Re: Arrays in shell
I am reading the values from a file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2003 10:55 AM
12-15-2003 10:55 AM
SolutionTo read the values into a file is also quite simple:
typeset -i10 KNT=0
INFILE="/var/tmp/myfile"
cat ${INFILE} | while read arr[${KNT}]
do
(( KNT += 1))
done
var="Two"
typeset -i10 I=0
while [[ ${I} -lt ${KNT} ]]
do
if [[ "${arr[${I}]}" = "${var}" ]]
then
echo "Do something"
fi
(( I += 1 ))
done
Assignments to array elements are just like assignments to a simple variable.