Array with do while and if loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array with do while and if loop
# 1  
Old 11-14-2011
Array with do while and if loop

Hi All,
I am trying to run a do while for an array. And in the do while, I'm trying to get a user response. Depending on the the answer, I go ahead and do something or I move on to next element in the array.
So far I can read the array, but I can't get the if statement to work. Any suggestions will be greatly appreciated!

Code:
#!/bin/bash
set -x
linenum=1
array=
ps -ef | grep myAgent | grep -v grep | awk '{print $8}' | while read line ;
do
    echo "$linenum : $line"
    set -- $line
    echo "$1"
    array[$linenum]=$1
    echo "  Would you like to analyze this instance ${array[$linenum]} (y/n)? "
    read response  ### this does not work
                if [ "$response" = Y ] || [ "$response" = y ] ; then
                      echo "Analysing"
                      sleep 10
                      # do some thing.. code snip ..
                fi
    linenum=$(( linenum + 1 ))
done

Thanks for looking.
Nitin Smilie
# 2  
Old 11-14-2011
Code:

ps -ef | awk '/myAgent/{print $8}' | while read line ;
....
if [ "$response" = Y ] -o [ "$response" = y ] ; then

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 11-14-2011
Because you're in the middle of a pipe chain, standard input is already being used. Your read just grabs the next line printed by awk!

You should tell it explicitly that you want to read from the terminal, like

Code:
read response < /dev/tty

/dev/tty is a special file that is always your current terminal.

---------- Post updated at 02:54 PM ---------- Previous update was at 02:51 PM ----------

Simplifying it like vgersh99 suggests is also a good idea. Reducing the number of things in your pipe chain will make your script much more efficient.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 11-14-2011
here're a couple of alternatives:
Code:

#!/bin/ksh
set -x
linenum=1 
array= 
ps -ef | grep myAgent | grep -v grep | awk '{print $8}' |&
while read -p line
do
....

Or if you're reading from a file and want to prompt a user for input in the middle of a loop:
Code:
#!/bin/ksh

file=read.txt

exec 3< $file

while read -u3 f
do
  printf 'Your input? (y/n) '
  read answer
  if [ "${answer}" = y ] -o [ "${answer}" = Y ]; then
    echo "answer->[${answer}] readFile->[${f}]"
  fi
done

# 5  
Old 11-14-2011
Thanks for the quick replies. I was able to get the read to work.
But for some reason I was not getting inside the if condition loop. I kept getting this error:

Code:
./test_agent.sh: line 15 [: too many arguments

Turns out the if statement cannot handle -o or ||. Strange, but that's a problem for some other day.
My code looks like this now, I thought about awk '/myAgent/{print8}', but I then get an extra line with just awk. So I'll have to do a grep -v awk.

Code:
#!/bin/bash
linenum=1
array=
ps -ef | grep myAgent | grep -v grep | awk '{print $8}' | while read -p line ;
# or this:
# ps -ef | grep -v awk | awk '/myAgent/{print $8}' | while read line ;
#
do
    echo "$linenum : $line"
    set -- $line
    echo "$1"
    array[$linenum]=$1
        echo "  Analyze instance echo ${array[$linenum]} (y/n)? "
                read answer  </dev/tty
                if [ "$answer" = Y ]  ; then
                echo "Analysing"
                # do something
                fi
    linenum=$(( linenum + 1 ))
done

Thanks.
Nitin Smilie
# 6  
Old 11-14-2011
Quote:
Originally Posted by nitin
I thought about awk '/myAgent/{print8}', but I then get an extra line with just awk. So I'll have to do a grep -v awk.
Or, you could just use awk '/[m]yAgent/{print $8}'
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash for loop array

Hi there, A bit new to bash and am having an issue with a for loop. I look for filenames in a specified directory and pull the date string from each meeting a certain criteria, and then would like to make a directory for each date found, like this: search 20180101.gz 20180102.gz 20180103.gz... (5 Replies)
Discussion started by: mwheeler12
5 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Loop and array problem

Hi, I have the following problem that is beyond what I can currently do with bash scripting. In file 1, I have ~ 2500000 values. Note this file is not sorted. 3 19 LABEL_A 3 37 LABEL_B 2 12 LABEL_C 1 15 LABEL_D I have a list of values in "file 2" ~ 25000 unique lines: Note -... (6 Replies)
Discussion started by: hubleo
6 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. Shell Programming and Scripting

Array and Loop Problem

I've got this problem, if I modify an array in the loop and print it, everything is fine as long as I stay in the loop. But, when I print it outside the loop, nothing happens... How can I solve this problem? Here I prepared a sample for you to see my problem; zgrw@Rain:~$ cat test asd 123... (4 Replies)
Discussion started by: zgrw
4 Replies

6. Shell Programming and Scripting

Help with awk in array in while loop

Hi everyone:) I have 2 files - IN & OUT. Example: IN A:13:30 B:45:40 . . . UNLIMITED OUT Z:12:24 Y:20:15 . . . UNLIMITED I want first row of numbers of IN - OUT. Example 13-12 45-20 My code is (2 Replies)
Discussion started by: vincyoxy
2 Replies

7. UNIX for Dummies Questions & Answers

Reading from while loop into an array

Hi I have something like cat $HOME/all_dirs | while read ln_old_dirs do if then echo "$ln_all_old_dirs" fi done As you know that the variable ln_all_old_dirs is not accessable from outside the... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

8. Shell Programming and Scripting

Array in loop is acting up

Hello! I have a question about loops and arrays. I'm trying to go through this: for aa in 01 02 03 OrigNum=$(grep ${Orig} Ba3In2F12.prepos | wc -l) OrigNum=$((${OrigNum} - 1)) echo ${OrigNum} etc It gets stuck on the second line. The error reads: ./asdf: line 30:... (5 Replies)
Discussion started by: RisingSun
5 Replies

9. Shell Programming and Scripting

How to use array values after the loop.

- I m retreving values from database and wish to use those values later in my shell script. I m placing these values in an array da_data but outside loop array is empty.Problem is its treating array as local inside loop hence array is empty outside loop. Plz go through the script and suggest how... (1 Reply)
Discussion started by: Devesh5683
1 Replies

10. Shell Programming and Scripting

Array Declaration and For Loop

I am just stucked in syntax.This is more like a array and for loop problem. I want to use ls -l command and get filezise and filename of all filenames in the directory in an array (say array#1). After 2 minutes of sleep, i want to get the same information in another array (say array#2). The... (4 Replies)
Discussion started by: 33junaid
4 Replies
Login or Register to Ask a Question