LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-04-2012, 04:20 PM   #1
KG425
LQ Newbie
 
Registered: Jun 2012
Posts: 4

Rep: Reputation: Disabled
Using shift in bash


Hello,

I have a quick question. Suppose I do the following:
Code:
set 1 2 3 4 5
a=''
for i in "$@"; do
    if [ $i = '2' ]; then
        shift
        a=$i
    fi
done

After doing it, I want a to have value of 3 but instead I get 2 which is not what I want. Please help me.

Last edited by KG425; 06-04-2012 at 04:30 PM.
 
Old 06-04-2012, 07:30 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,896

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
$i = '2' indicates value assignment.

$i == '2' indicates equality.
 
Old 06-05-2012, 03:19 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
@bigrigdriver - actually, either is applicable in bash. Although I do personally prefer == from a visual perspective.

You appear to be confusing parameters with the value stored in your variable. Shift has no affect on "i" but rather on the parameters.
So when $i is equal to 2, it never stops being equal to 2. Also, as you are comparing numbers you should either use -eq for the test or the more
preferred (()) when testing numbers.

So I would suggest something like:
Code:
#!/bin/bash

while (( $# ))
do
    if (( $1 == 2 ))
    then
	shift
	a=$1
    fi
    shift
done

echo $a
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash: loop through parameters with shift Ramurd Programming 3 03-01-2012 06:49 AM
Capturing ctrl-shift-t signal in bash (linux) anoosh.michael Linux - General 4 10-18-2011 04:08 AM
BASH SHELL “Shift + Page Up” NOT WORKING r00ster Linux - Newbie 4 09-07-2008 02:33 PM
Setting Bash Screen Scrollback Length? (Shift + Page Up) juanbobo Linux - Software 1 06-14-2006 11:01 AM
Scrolling with Shift + Page Up in Bash Ragsdale45 Linux - Newbie 7 07-14-2005 04:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration