#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    23
    Rep 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!!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,481
    Rep Power
    1752
    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
    Last edited by SimonJM; September 18th, 2011 at 08:05 AM. Reason: Remove quotes from around $*
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0
    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!!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,481
    Rep Power
    1752
    Try:

    Code:
    rpt_file=${rpt_file%<*>}
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,481
    Rep Power
    1752
    Originally Posted by akaballa123
    Oh wow it worked!!
    Less surprise please!!!
    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

IMN logo majestic logo threadwatch logo seochat tools logo