LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 06-23-2010, 05:06 PM   #1
ohijames
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Rep: Reputation: 0
Incrementing an array element in Shell scripting


ch[1]=0; ch[2]=0; ch[3]=0; ch[4]=0; ch[5]=0; ch[6]=0; ch[7]=0; ch[8]=0; ch[9]=0; ch[10]=0;ch[11]=0; ch[12]=0; ch[13]=0; ch[14]=0; ch[15]=0 ch[16]=0; ch[17]=0; ch[18]=0; ch[19]=10; ch[20]=0;

I have an array ch and I want to increment each element in my array for the following if statement. I'm not sure I have the right array increment syntax but I have tried it in different ways ant it doesn't seem to work.
I tried ch[$1]++, ch[$1]+1, ch[$1++], ch[$1]+=1, ch[$1]=ch[$1]+1 none of these seem to work.


# while loop reading from read.txt for check list 1 - 15
for i in `seq 15`
do
a=`grep "${cl[$i]}" $file`
status=$?
if [[ $status = 0 ]];
then
echo -n -e "1 "
let ch[$i+1]
let k++
else
echo -n -e "0 "
fi
done

for l in `seq 20`
do
echo -n -e "${ch[$l]} "

done
 
Old 06-23-2010, 06:06 PM   #2
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Code:
((ch[$i]++))
 
Old 06-23-2010, 06:06 PM   #3
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Enclose your code in [ code] [/ code] tags makes it a lot more readable (not so bad in this case, but helps if you have a lot of important indenting.)

Code:
#!/bin/bash
ch[1]=0
ch[2]=0
ch[3]=0
ch[4]=0
ch[5]=0
for i in $(seq 5); do
  for j in $(seq 5); do
    ch[$i]=$((${ch[$i]} + $j))
    echo "i: $i, j: $j, ch[\$i]: ${ch[$i]}"
  done
done
This code should explain it fairly well.

edit: The ABS has a lot of good information on using arrays in bash and the specifics of how to reference them: http://tldp.org/LDP/abs/html/arrays.html

Last edited by rweaver; 06-23-2010 at 06:12 PM.
 
  


Reply

Tags
array, elements, shell scripting


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
Shell scripting array question ulver Programming 5 06-19-2009 08:09 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 02:47 AM
Bash Shell Scripting - using ls into array aliasofmike Programming 5 11-05-2007 04:00 PM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 06:25 PM
C++ Six Element Array petercool Programming 2 08-20-2003 12:08 PM


All times are GMT -5. The time now is 05:59 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration