?
Solved

how to use shift for array within while loop

Posted on 2008-06-12
6
Medium Priority
?
2,496 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
Technology Partners: 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

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!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 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