LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2007, 07:27 AM   #1
bhandu
LQ Newbie
 
Registered: Mar 2006
Posts: 22

Rep: Reputation: 15
Exclamation error in creating array


first is file and contains records with fields separated by ":"
i want to create array of the second field from file first.
But when I tried using below command it gives me error

pass1=(`cut -d ":" -f2 first`)


can anyone help me?
 
Old 07-10-2007, 07:44 AM   #2
kiruthika
LQ Newbie
 
Registered: Jul 2007
Posts: 3

Rep: Reputation: 0
can you eb a bit more explanatory?
 
Old 07-10-2007, 07:55 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Code:
pass=(`cut -d ":" -f3 /etc/passwd`)
for ((i=0;i<${#pass[@]};i++)); do echo "${pass[$i]}"; done
25
104
102
1
2
40
...
 
Old 07-10-2007, 08:57 AM   #4
lakris
Member
 
Registered: Sep 2004
Location: Stockholm, Sweden
Distribution: Ubuntu, RedHat, SuSe, Debian, Slax
Posts: 102

Rep: Reputation: 15
Quote:
Originally Posted by bhandu
first is file and contains records with fields separated by ":"
i want to create array of the second field from file first.
But when I tried using below command it gives me error

pass1=(`cut -d ":" -f2 first`)


can anyone help me?
What exactly is it that doesn't work?
try
Code:
pass1=(`cut -d ":" -f2 first`);echo ${pass1[@]}
Are You sure You are using the right type of quotes? Copy the text from here to command line and try again.
Does the file first exist?
What is the error message?
 
Old 07-10-2007, 02:28 PM   #5
bhandu
LQ Newbie
 
Registered: Mar 2006
Posts: 22

Original Poster
Rep: Reputation: 15
error

It still gives error that
unexpected '( '
 
Old 07-10-2007, 02:41 PM   #6
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,969
Blog Entries: 1

Rep: Reputation: 208Reputation: 208Reputation: 208
Try:
Code:
pass1=$(`cut -d ":" -f2 first`)
 
Old 07-10-2007, 05:43 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Check if bash is your default shell.
 
Old 07-12-2007, 12:05 AM   #8
lakris
Member
 
Registered: Sep 2004
Location: Stockholm, Sweden
Distribution: Ubuntu, RedHat, SuSe, Debian, Slax
Posts: 102

Rep: Reputation: 15
Quote:
Originally Posted by jschiwal
Check if bash is your default shell.
Good point.

bhandu, type echo ${BASH_VERSINFO[@]}

what does it say?
 
Old 07-12-2007, 12:18 AM   #9
lakris
Member
 
Registered: Sep 2004
Location: Stockholm, Sweden
Distribution: Ubuntu, RedHat, SuSe, Debian, Slax
Posts: 102

Rep: Reputation: 15
Quote:
Originally Posted by custangro
Try:
Code:
pass1=$(`cut -d ":" -f2 first`)
Not quite,
the $() construct is the same as backticks, so You can write either
Code:
pass1=(`cut -d ":" -f2 first`)
or
Code:
pass1=$(cut -d ":" -f2 first)
Handy for when You can't find the backtick on a foreign keyboard
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime error using cin.getline() with array of char AzMan Programming 2 03-26-2007 11:26 AM
creating array in c++ with string index mohtasham1983 Programming 3 03-11-2007 05:01 PM
Problem with superblock when creating a software raid 5 array halfjack Linux - Server 0 10-18-2006 09:06 AM
Creating array of GTKWidgets - is this possible? woodywellhung Programming 1 05-05-2004 04:44 PM
mysql fetch array error slam Linux - Software 1 04-28-2003 04:30 PM


All times are GMT -5. The time now is 06:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration