LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-06-2014, 07:35 AM   #1
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209
Blog Entries: 1

Rep: Reputation: Disabled
Using variable array in shell script


I want to increase my variable array an display it in a for loop:
this is my code, but it doesn't work
count[$i]='expr ${count[i]}+1'
echo ${count[$i]}
 
Old 01-06-2014, 07:55 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Not sure why you need an array for this but have a look at this:
Code:
#!/bin/bash

for i in {1..5}
do
  count[$i]=$i       # fill count[X] with X
  (( count[$i]++ ))  # add one to count[X]
done

# show array content
echo ${count[1]}
echo ${count[2]}
echo ${count[3]}
echo ${count[4]}
echo ${count[5]}
Example run:
Code:
$ ./foo.sh
2
3
4
5
6
 
1 members found this post helpful.
Old 01-06-2014, 10:59 PM   #3
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Not sure why you need an array for this but have a look at this:
Code:
#!/bin/bash

for i in {1..5}
do
  count[$i]=$i       # fill count[X] with X
  (( count[$i]++ ))  # add one to count[X]
done

# show array content
echo ${count[1]}
echo ${count[2]}
echo ${count[3]}
echo ${count[4]}
echo ${count[5]}
Example run:
Code:
$ ./foo.sh
2
3
4
5
6
Thank you, I have a condition to do it.
for i in {1..15}
do
cut -d';' --fields="$i" tehrasht.txt>m.txt
while read LINE
do
if [ $LINE == '1' ];then
((count["$i"]++))
fi
done < m.txt
echo ${count[$i]}
count[$i]=0
done

Now, It show the output, but before it,the error is shown:
./obu.txt: line 6: [: too many arguments

Last edited by massy; 01-06-2014 at 11:10 PM.
 
Old 01-06-2014, 11:09 PM   #4
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by massy View Post
Thank you, I have a condition to do it.
for i in {1..15}
do
cut -d';' --fields="$i" tehrasht.txt>m.txt
while read LINE
do
if [ $LINE == '1' ];then
((count["$i"]++))
fi
done < m.txt
echo ${count[$i]}
count[$i]=0
done

Now, It show the output, but before it,the error is shown:
./obu.txt: line 6: [: too many arguments
 
Old 01-07-2014, 03:21 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by massy View Post
for i in {1..15}
do
cut -d';' --fields="$i" tehrasht.txt>m.txt
while read LINE
do
if [ $LINE == '1' ];then
((count["$i"]++))
fi
done < m.txt
echo ${count[$i]}
count[$i]=0
done

Now, It show the output, but before it,the error is shown:
./obu.txt: line 6: [: too many arguments
You need to put variables between quotes otherwise you might get unexpected results.

Have a look at this:
Code:
#!/bin/bash

for i in {1..15}
do
  cut -d';' --fields="$i" tehrasht.txt > m.txt
  while read LINE
  do
    if [ "$LINE" == "1" ]  # $LINE needs to be quoted.
    then
      (( count["$i"]++ ))
    fi
  done < m.txt
  echo ${count[$i]}
  count[$i]=0
done
I'm assuming (don't know what tehrasht.txt holds) that LINE contains a value that has spaces or something like it in it.

BTW: It doers look like you are trying to do this the hard way, but without knowing what the content of tehrasht.txt is and what you are attempting to do I cannot give you any hints.
 
Old 01-09-2014, 04:36 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
One way to check the program in detail
Code:
#!/bin/bash
set -xv

<rest of your code here>
The set cmd prints the before & after versions of the code and var contents
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use a variable to index an array in Bash shell script? anotheremily Programming 3 06-22-2012 09:46 PM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
create a global associative array variable inside a function of a bash script konsolebox Programming 3 07-14-2009 06:08 AM
passing array and variable to function in bash script ajaypitroda Programming 2 07-07-2009 11:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:57 PM.

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
Open Source Consulting | Domain Registration