LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-01-2012, 05:17 AM   #1
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Bash: loop through parameters with shift


Hi there,

I have the following scriptlet:

Code:
while [[ "${1:0:1}" == "-" ]]
do
        case ${1} in
                "-c")
                        shift
                        echo "Matched -c"
                        dbname=${1}
                        ;;
                "-u")
                        shift
                        echo "Matched -u"
                        dbuser=${1}
                        ;;
                "-p")
                        shift
                        echo "Matched -p"
                        dbpass=${1}
                        ;;
                *)
                        break
                        ;;
        esac
done

if [[ ! -z "${dbname}" ]]
then
   echo "Connecting to db ${dbname} with user ${dbuser} and password ${dbpass}"
fi

echo ${*}
This is some work in progress; hence the simple echo instead of really connecting. The idea is quite simple: if this script is called with the parameters -c, -p and -u these values must NOT be echoed by "echo ${*} and the variables have to be set according to the parameters given.

so I need to be able to run my script like this:
Code:
script.sh -c db1 -u dbuser -p dbpass run this.sql
and
Code:
script.sh echo oh my goodness
Somehow, my thinking went wrong and the while loop only takes the first parameter, the shifting done within the case statement (if applicable) does not seem to affect the while loop.

Unfortunately I have bash version 3.2.51, and no means to upgrade it to bash 4.

Getopts would not help me alot here I reckon, because I need to strip stuff from my parameter list; I had hoped shift would let me.
 
Old 03-01-2012, 05:44 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
You never shift after you get the OPT's OPTARG. This should fix that problem:

Code:
...
                        shift
                        echo "Matched -c"
                        dbname=${1}
                        shift
                        ;;
                "-u")
                        shift
                        echo "Matched -u"
                        dbuser=${1}
                        shift
                        ;;
                "-p")
                        shift
                        echo "Matched -p"
                        dbpass=${1}
                        shift
                        ;;
                *)
                  ...
 
1 members found this post helpful.
Old 03-01-2012, 05:48 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The script shits the option out of the way but not the option argument. Fix by adding a shift statement after the esac.

EDIT: beaten to it by jhwilliams

EDIT2: nothing in what has been shown so far precludes using getopts

Last edited by catkin; 03-01-2012 at 05:50 AM.
 
Old 03-01-2012, 05:49 AM   #4
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Original Poster
Rep: Reputation: 111Reputation: 111
how simple things can be... thanks; that was it.
 
  


Reply



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] A question about for loop (increasing two parameters) yaron.kadem Linux - Server 5 12-28-2011 02:25 AM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
[SOLVED] Loop through list of URLs in txt file, parse out parameters, pass to wget in bash. dchol Linux - Newbie 16 07-27-2011 02:19 PM
Sabayon LiveDVD Loop from USB - Grub kernel parameters manwithaplan Linux - General 0 01-14-2009 01:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06: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
Open Source Consulting | Domain Registration