Solved

how to use shift for array within while loop

Posted on 2008-06-12
6
2,485 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

21 Experts available now in Live!

Get 1:1 Help Now