LinuxQuestions.org
Visit Jeremy's Blog.
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 01-27-2016, 12:16 PM   #1
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Rep: Reputation: 2
shell script array subscript problem


Quote:
IDS=$(/bin/ps -ef | /bin/grep -v grep | /bin/grep -i userA | /usr/bin/awk '{print $1 }')
#echo $IDS
echo ==============================
for i in ${IDS[@]}
do

echo $i
echo ${IDS[0]}## I put [0] it print whole array, I put [1] it prints nothing.

done

fi

> ./OLD_NEW.sh
==============================
Quote:
root
root userA userA root userA userA userA userA userA userA userA ##<--- this the whole array
userA
root userA userA root userA userA userA userA userA userA userA
userA
root userA userA root userA userA userA userA userA userA userA
root
root userA userA root userA userA userA userA userA userA userA
userA
root userA userA root userA userA userA userA userA userA userA
userA
root userA userA root userA userA userA userA userA userA userA
userA
root userA userA root userA userA userA userA userA userA userA
userA
root userA userA root userA userA userA userA userA userA userA
userA
root userA userA root userA userA userA userA userA userA userA
userA
root userA userA root userA userA userA userA userA userA userA
userA
root userA userA root userA userA userA userA userA userA userA
I need to be able to navigate through array "IDS"

Last edited by PoleStar; 01-27-2016 at 12:19 PM.
 
Old 01-27-2016, 12:31 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 170

Rep: Reputation: 56
I'm not entirely certain what result you're looking for, but enclosing the first part in another set of parentheses might help.
Code:
IDS=($(/bin/ps -ef | /bin/grep -v grep | /bin/grep -i userA | /usr/bin/awk '{print $1 }'))
You also have a "fi" at the end with no corresponding "if"
 
1 members found this post helpful.
Old 01-27-2016, 12:48 PM   #3
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Original Poster
Rep: Reputation: 2
yes it fixed it.
What was that a problem, I have to run this code on many machines, So I need to make sure it would work on all of them.

Reason I am saying that is, my original choice to make array was "IDS+=" (IDS is array name)
To my surprise it did not work on older systems. So I had to go back to simpler way.

BTW I took those line out of little longer script, hence fi.

Thanks for your help.

Last edited by PoleStar; 01-27-2016 at 12:49 PM.
 
Old 01-27-2016, 12:50 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
The brackets around the data makes the array, but $() processes the command and returns all the data, so you need the additional () around the outside to make it split the data into an array.
 
1 members found this post helpful.
Old 01-27-2016, 12:52 PM   #5
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Original Poster
Rep: Reputation: 2
I see.
 
Old 01-27-2016, 01:36 PM   #6
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Original Poster
Rep: Reputation: 2
Code:
#!/bin/bash

COUNT=0
CHECK_TOMCAT=`ps -ef | grep -v grep | grep -i userA`
#echo $CHECK_TOMCAT


if [[  $CHECK_TOMCAT != "" ]];
then
        echo tomcat found
        #echo $CHECK_TOMCAT

        IDS=($(/bin/ps -ef | /bin/grep -v grep | /bin/grep -i userA | /usr/bin/awk '{print $1 }')  )
        #echo $IDS
        echo ==============================
        for i in  ${IDS[@]}
                do
#                       echo $i
                        echo ${IDS[$COUNT]}
                        echo ==== $i === ${IDS[$COUNT +1]}  <----print current and the next one
                        if [ $i =  ${IDS[$COUNT] +1} ];then    <--------print only if they both match.
                                        echo problem $i === ${IDS[$COUNT +1]}
                        fi
                        COUNT=$(($COUNT+1))

                done

                echo $COUNT

fi


Quote:

tomcat found
==============================
root
==== root === userA
userA
==== userA === userA
userA
==== userA === root
root
==== root === userA
userA
==== userA === userA
userA
==== userA === userA
userA
==== userA === userA
userA
==== userA === userA
userA
==== userA === userA
userA
==== userA === userA
userA
==== userA ===
11
But it find not match.
I tried with "!=" then it do not print "problem" line.

Last edited by PoleStar; 01-27-2016 at 01:38 PM.
 
Old 01-27-2016, 02:14 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I am confused with what you are trying to do and with your explanation.

You say:
Quote:
I tried with "!=" then it do not print "problem" line.
But it did not print the problem line with '=' either according to your output ... it can't be both right and wrong??

Also, is there any reason why you mix up all the different ways to do things? You use both `` and $() and also [] and [[]] ... pick one and stick to it.

If needing to look at the current and next ID, why not create a for loop instead of a for/in loop:
Code:
for (( i = 0; i <= ${#ID[@]}; i++ ))
When performing tests, always use double quotes around variables unless you are 100 sure you do not need them.

Don;t combine a whole string of useless greps to remove stuff, just use awk:
Code:
ps -ef | awk '/userA/{print $1}'
See if any of that helps?

You can also try putting set -xv at the start of the script and check the output to see what the tests are seeing
 
1 members found this post helpful.
Old 01-27-2016, 03:49 PM   #8
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Original Poster
Rep: Reputation: 2
Code:
#!/bin/bash

COUNT=0
CHECK_TOMCAT=$(ps -ef | grep -v grep | grep -i userA)
#echo $CHECK_TOMCAT


if [[  $CHECK_TOMCAT != "" ]];then

        echo tomcat found
        #echo $CHECK_TOMCAT

        IDS=($(/bin/ps -ef |  /usr/bin/awk '/userA/{print $1 }')  )
        echo ==============================
        for ((i =0; i< ${#IDS[@]} ; i++))
                do
                echo ${IDS[$i]}
                        if [[ ${IDS[$i]} !=  ${IDS[$i +1]} ]];then
                                echo problem ${IDS[$i]}  === ${IDS[$i +1]}
                        fi

                done
fi

exit
Quote:
>OLD_NEW.sh

tomcat found
==============================
root
problem root === userA
userA
userA
problem userA === root
root
problem root === userA
userA
userA
userA
userA
userA
userA
userA
Awesome. Problem fixed.
Thank you!!!
 
Old 01-28-2016, 03:06 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
As a side note, you can also remove the $ from 'i' when using inside array subscript as an integer
Code:
${IDS[i]} or ${IDS[i+1]}
 
  


Reply


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 script array problem ajayyadavmca Programming 3 06-21-2012 10:35 AM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-26-2010 12:19 AM
invalid types ‘int[int]’ for array subscript medha Programming 16 08-25-2006 09:30 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 10:34 PM
shell script array problem rche3252 Programming 1 10-09-2003 12:43 AM


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