Solved

how to use shift for array within while loop

Posted on 2008-06-12
6
2,486 Views
Last Modified: 2013-12-26
Experts,

I have a while loop that does some work on a number of cmd line parameters in sucession.

creates an array within the while loop.
based on the array created I want to exclude the first element within that array but only if it matches.
problem is when I use shift it effects the cmd line parameters instead and not the array.

EG
script.bash one two three four
while [ "$#" -gt 0 ]
do
array='sql code'
test=$(echo ${array} | awk '{print $1}')
if [ ${test} = badElement ];then
shift
fi
done

so when running this code and executing my test
the shift changes the command line argument from one to two
what I want is the shift to remove the first element inside the array instead

suggestions?
0
Comment
Question by:sugarfreeless
  • 3
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21773017
script.bash `sql code`
while [ $1 = badElement  ]
do
shift
fi
done
0
 
LVL 84

Expert Comment

by:ozo
ID: 21773035
or do you mean something like
array='badElement sql code'
echo ${array#badElement}
0
 
LVL 3

Author Comment

by:sugarfreeless
ID: 21773105
more similar to your second post.

Using my example I'll give you the output I'm experiencing...

script.bash one two three four
while [ "$#" -gt 0 ]
do
array='sql code'
echo cmdline:$1
echo array:$array
test=$(echo ${array} | awk '{print $1}')
if [ ${test} = badElement ];then
shift
echo cmdline:$1
echo array:$array
fi
done

output:

above array test
cmdline:one
array:connect. column -------------- result

below array test
cmdline:                # command line argument was shifted not array element
array:connect. colum ------------- result

My goal:
shift the 1st element out of the array if it equalls 'connect.'
and not to shift the 1st element from the command line arguments.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 21773175
array=${array#connect. }
0
 
LVL 3

Author Closing Comment

by:sugarfreeless
ID: 31466661
ozo,

AWESOME!
Thank you very much.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 21773243
You are confusing terminologies.  You are not using an array, you are using a simple shell variable, hence there are no elements to shift.

What's wrong with ozo's second suggestion, ie:


#!/bin/bash

while [ $# -gt 0 ]

do

  result='sql code'

  echo cmdline:$1

  echo "result:$result"

  echo "{$result#connect.}

  shift

done

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
fizzArray  challenge 1 63
Execute multiple curl cmds with sleep and send output to file 10 89
wordcount challenge 11 108
firstChar challenge 13 110
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now