working with arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting working with arrays
# 1  
Old 08-23-2007
Question working with arrays

Hi,

I'm writting a script to assign sm values to arrays and work on the values stored in array later. But i'm not able to work properly to use these arrays. intialising is one of the main problem as i'm not sure of array element count (they can increase/decrease). Copying the script. In case of any doubts please let me know.

count=0
total=`wc -l hk_mrm_jobs.txt`
echo $total
count=24
if [ $count -eq $total ]
then
echo $count
k=0
for j in `cat hk_mrm_jobs.txt`
do
fortime=`autorep -j $j | grep EQDEL | awk '{print $5}'` #getting a value from string and assign to array
myarray[$k] = $fortime
echo $k
k=`expr $k + 1`
done
fi
done
# 2  
Old 08-23-2007
Please, use CODE tag when posting :
Code:
count=0
total=`wc -l hk_mrm_jobs.txt`
echo $total
count=24
if [ $count -eq $total ]
then
   echo $count
   k=0
   for j in `cat hk_mrm_jobs.txt`
   do
      fortime=`autorep -j $j | grep EQDEL | awk '{print $5}'` #getting a value from string and assign to array
      myarray[$k] = $fortime
      echo $k
      k=`expr $k + 1`
   done
fi
done

The last done statement is not associated with a while : Syntax error.
Are you sure that your input file contains 24 records ?

You for loop seems to be correct.
Another way to code this loop :
Code:
while read j
do
   myarray[${#myarray[*]}]=$(autorep -j $j | awk '/EQDEL/ {print $5}')
done < hk_mrm_jobs.txt


Jean-Pierre.
# 3  
Old 08-23-2007
one more thing

Code:
wc -l filename

returns o/p like this
Code:
rkumar@bdc4reteaix1w: /home/rkumar >wc -l restart_sshd.sh
       2 restart_sshd.sh

So, if you want only the count you should filter that out. Else, the subsequent if statement wont work. And,
Quote:
myarray[$k] = $fortime
There should not be spaces while assigning. It should be
Code:
myarray[$k]=$fortime

# 4  
Old 08-23-2007
Question Thanks

Thanks for the reply... Will take care to use code tag in future

But getting the error on running the script.. using While loop code


try.sh: syntax error at line 3: `myarray[${#myarray[*]}]=$' unexpected
# 5  
Old 08-23-2007
Quote:
Originally Posted by ranj@chn
Code:
wc -l filename

returns o/p like this
Code:
rkumar@bdc4reteaix1w: /home/rkumar >wc -l restart_sshd.sh
       2 restart_sshd.sh

So, if you want only the count you should filter that out. Else, the subsequent if statement wont work.
The following syntax can be use (avoid the need of filtering output):
Code:
total=`wc -l < hk_mrm_jobs.txt`



Quote:
Originally Posted by raman1605
Thanks for the reply... Will take care to use code tag in future

But getting the error on running the script.. using While loop code


try.sh: syntax error at line 3: `myarray[${#myarray[*]}]=$' unexpected
Try to replace the $(...) by `...`
Code:
while read j
do
   myarray[${#myarray[*]}]=`autorep -j $j | awk '/EQDEL/ {print $5}'`
done < hk_mrm_jobs.txt

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Disk Space Utilization in HTML format working in one environment and not working on the other

Hi Team, I have written the shell script which returns the result of the disk space filesystems which has crossed the threshold limit in HTML Format. Below mentioned is the script which worked perfectly on QA system. df -h | awk -v host=`hostname` ' BEGIN { print "<table border="4"... (13 Replies)
Discussion started by: Harihsun
13 Replies

2. Shell Programming and Scripting

Working web service call not working with curl

Hello, Newbie here, I have a perfectly well working web service call I can issue from chrome (PC Windows 10) and get the results I want (a dimmer being turned on in Fibaro Home Center 2 at level 40) I am not allowed to post urls but the below works with http and :// and... (3 Replies)
Discussion started by: abigbear
3 Replies

3. Shell Programming and Scripting

Automating pbrun /bin/su not working, whenever manually it is working using putty

I am trying to automate a script where I need to use pbrun /bin/su but for some reason it is not passing thru the pbrun as my code below. . ~/.bash_profile pbrun /bin/su - content c h 1 hpsvn up file path I am executing this from an external .sh file that is pointing to this scripts file... (14 Replies)
Discussion started by: jorgejac
14 Replies

4. Red Hat

Nslookup working but ping not working at windows client

Hi Team we have created a DNS server at RHEL6.2 environment in 10.20.203.x/24 network. Everything is going well on linux client as nslookup, ping by host etc in entire subnet. We are getting problem in windows client as nslookup working as well but not ping. all the firewall is disabled and... (5 Replies)
Discussion started by: boby.kumar
5 Replies

5. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

6. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

7. UNIX for Advanced & Expert Users

Awk expressions working & not working

Hi, Putting across a few awk expressions. Apart from the last, all of them are working. echo a/b/c | awk -F'/b/c$' '{print $1}' a echo a/b/c++ | awk -F'/b/c++' '{print $1}' a echo a/b/c++ | awk -F'/b/c++$' '{print $1}' a/b/c++ Request thoughts on why putting a '$' post double ++... (12 Replies)
Discussion started by: vibhor_agarwali
12 Replies

8. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

9. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

10. Shell Programming and Scripting

Arrays display - not working

Hi, I could not make this one working. Any help is appreciated. I am trying to display days & months. The code is failing at line 13 " for i in ${{$filea}} ". Please help. #----------------------------------- #!/bin/ksh # DAY="mon tue wed thu fri sat sun" MONTH="jan feb mar apr may june... (2 Replies)
Discussion started by: vanniarp
2 Replies
Login or Register to Ask a Question