[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

how to use shift for array within while loop

Posted on 2008-06-12
6
Medium Priority
?
2,497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Accepted Solution

by:
ozo earned 200 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

649 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