Solved

how to use shift for array within while loop

Posted on 2008-06-12
6
2,494 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

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!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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