Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 16th, 2011, 11:01 AM
akaballa123 akaballa123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 23 akaballa123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 5 m 10 sec
Reputation Power: 0
[Bash] Help with include array value in unix command

Hi there, I have created an array in which I store multiple names. I thne run a for loop to iterate through 2 names in the array. I then run another script and input the current name as an argument to that script. my problem is that during the first iteration, the script works fine, but during the second iteration, the second name does not get passed to the script, but the first one gets passed again.

say I have two inputs: apples, oranges stored in jBPMarray.

in for loop:

in first iteration: i want to run

Code:

/usr/local/report.sh apples


in second report I want to run:

Code:

/usr/local/report.sh oranges


instead the argument apples get used twice.

here is my code. I think there is something wrong with my syntax inside the for loop

Code:

#!/usr/bin/bash


jBPMarray=("$@")

echo -n "Specify number of reports to generate per hour : "
read num_reports_per_hr

echo -n "Specify a timestamp e.g. ( 01/01/2011 ) : "
read time_stamp;

output_dir=""
rpt_file_name=""
process_itr=${#jBPMarray[@]} 
echo "process_itr: "
echo $process_itr
index_itr=0

while [ "$process_itr" -ne 0 ]
do
    echo "current iteration: ${process_itr}"
    for ((j=0;j<=($num_reports_per_hr - 1);j++))
    do 
      echo "current number: "
      echo $j
      "/usr/local/report.sh" "$jBPMarray["$j"+"$index_itr"]"
      echo "Current Process:" 
      echo ${jBPMarray[$j]} 
      process_itr=$(($process_itr - 1));
      echo "process_itr: "
      echo $process_itr
      if [ "$process_itr" -eq 0 ]
      then 
            break;
      fi
      
    done
    
    echo "Sleeping"
    sleep 10
    index_itr="$index_itr"+"$num_reports_per_hr"

done



Any advise would be well appreciated. Plz Help! Thanks!!!

Reply With Quote
  #2  
Old September 16th, 2011, 11:02 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 50 m 50 sec
Reputation Power: 1485
You seem to be over-complicating things ...
Try (untested!):
Code:
#!/usr/bin/bash

echo -n "Specify number of reports to generate per hour : "
read num_reports_per_hr

echo -n "Specify a timestamp e.g. ( 01/01/2011 ) : "
read time_stamp;

output_dir=""
rpt_file_name=""
index_itr=0

for process_itr in $*
do
    echo "current iteration: ${process_itr}"
    for ((j=0;j<=($num_reports_per_hr - 1);j++))
    do 
      echo "current number: "
      echo $j
      /usr/local/report.sh "${process_itr}"
      echo "Current Process:" 
    done
    echo "Sleeping"
    sleep 10
done
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Last edited by SimonJM : September 18th, 2011 at 07:05 AM. Reason: Remove quotes from around $*

Reply With Quote
  #3  
Old September 19th, 2011, 08:48 AM
akaballa123 akaballa123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 23 akaballa123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 5 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by SimonJM
You seem to be over-complicating things ...
Try (untested!):
Code:
#!/usr/bin/bash

echo -n "Specify number of reports to generate per hour : "
read num_reports_per_hr

echo -n "Specify a timestamp e.g. ( 01/01/2011 ) : "
read time_stamp;

output_dir=""
rpt_file_name=""
index_itr=0

for process_itr in $*
do
    echo "current iteration: ${process_itr}"
    for ((j=0;j<=($num_reports_per_hr - 1);j++))
    do 
      echo "current number: "
      echo $j
      /usr/local/report.sh "${process_itr}"
      echo "Current Process:" 
    done
    echo "Sleeping"
    sleep 10
done


Oh yea i realized how complicated my code was. I made the necessary changes thanks to you help! .

I am have some trouble with a regex mattern matching then string removal.

Basically i have a variable with the following string:

Code:

 rpt_file="mucm_turnover-survey_${LAST_BUS_DAY}.csv< FeedName>"


I would like to remove the substring < FeedName>.

I did some research and found out that the best way to remove a part of your string is to use the following syntax: ${string#substring}.

I tried incorporating that syntax, but am failing continuously in removing the substring. I am not sure if there is something wrong with my pattern or the syntax or both ( yikes!). here is my code:

Code:

read ADDR1 ADDR2 ADDR3 ADDR4 <<<$(IFS="\/"; echo $output_dir)
    rpt_file="$ADDR4"  # stores the string "mucm_turnover-survey_${LAST_BUS_DAY}.csv< FeedName>" 
    rpt_file=${rpt_file#<.*>}

    echo "report file name: "
    echo $rpt_file


Plz help!!

Reply With Quote
  #4  
Old September 19th, 2011, 11:02 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 50 m 50 sec
Reputation Power: 1485
Try:

Code:
rpt_file=${rpt_file%<*>}

Reply With Quote
  #5  
Old September 19th, 2011, 12:29 PM
akaballa123 akaballa123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 23 akaballa123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 5 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by SimonJM
Try:

Code:
rpt_file=${rpt_file%<*>}


Oh wow it worked!! So simple yet effective. Didnt realize it would make a difference to start from back of the string as opposed to starting from the front.

Reply With Quote
  #6  
Old September 19th, 2011, 05:03 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 50 m 50 sec
Reputation Power: 1485
Quote:
Originally Posted by akaballa123
Oh wow it worked!!

Less surprise please!!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > [Bash] Help with include array value in unix command

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap