- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- bash question
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
01-26-2007 02:59 AM
01-26-2007 02:59 AM
bash question
What is an elegant way to assign multiple variables into an array in bash. For example,
Gien the two records below, I would like to see the output
u[1]= named
u[2]= quagga
v[1]= 25
v[2]= 92
named:x:25:25:Named:/var/named:/sbin/nologin
quagga:x:92:92:Quaggasuite:/var/run/quagga:/sbin/nologin
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2007 03:18 AM
01-26-2007 03:18 AM
Re: bash question
u[1]=`cut -d ":" -f 1 $RECORD`
v[1]=`cut -d ":" -f 3 $RECORD`
To print the value use:
echo ${u[1]}
echo ${v[1]}
You will need a While and a counter to process all lines of the passwd file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2007 03:43 AM
01-26-2007 03:43 AM
Re: bash question
Probably I didn't describe my problem clear. The entry number in the record file is unknown. So using "while" statement is not a good option.
My goal is to use u[i] as a key to lookup its corresponding field v[i]. The lookup operations are used multiple times.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2007 06:30 AM
01-26-2007 06:30 AM
Re: bash question
#!/bin/bash
i=1
while read LINE
do
u[i]=`echo $LINE | cut -d ":" -f 1`
v[i]=`echo $LINE | cut -d ":" -f 3`
let "i++"
done < /etc/passwd
for n in `seq $i`
do
echo "Showing the $n element"
echo "Username: ${u[$n]}, USERID: ${v[$n]}"
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2007 01:54 AM
01-29-2007 01:54 AM
Re: bash question
declare -a u=(named quagga) v=(25 92)
But if you are reading the data from you passwd you could also parse entries from it and assign user and uid arrays in a loop like this
declare -i i=0;while read l;do u[i]=$(echo $l|cut -d: -f1) v[i++]=$(echo $l|cut -d: -f3);done < /etc/passwd
Please, note that index counting starts with 0.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2007 02:33 AM
01-29-2007 02:33 AM