LinuxQuestions.org
Help answer threads with 0 replies.
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 09-02-2011, 04:38 PM   #1
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
shell script arguments?


here's a little script i wrote for importing batches of mysql databases and creating users and privileges for them

the question is simple, how do i take the parts in brackets and accept them as arguments
like
Code:
./script.sh -rp {mysql_root_password} -nup {password_for_new_users}
and generate an error message if either of those arguments is omitted

Code:
for file in `ls *.sql`
do
part1=`echo $file | cut -d . -f 1`
part2=`echo $file | cut -d . -f 2`
mysql -hlocalhost -uroot -p{mysql_root_password} << eof
create database `echo $part1`;
create user '`echo $part1`'@localhost;
GRANT SELECT ,INSERT ,UPDATE ,DELETE ,CREATE ,DROP ,INDEX ,ALTER ,CREATE TEMPORARY TABLES,LOCK TABLES ON `echo $part1`.* TO '`echo $part1`'@'localhost';
set password for '`echo $part1`'@localhost = password('{password_for_new_users}');
flush privileges;
eof
mysql -hlocalhost -u`echo $part1` -p{password_for_new_users} `echo $part1` < $file
done
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-02-2011, 04:47 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If you're using bash look at help getopts. I don't use getopts, so I don't have an example for you.
Kevin Barry
 
Old 09-02-2011, 08:26 PM   #3
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Yeah, you could use getopts.
But here's my favorite way to handle this:
Code:
#!/bin/sh
function error_exit() { echo "ERROR: $*" 1>&2; exit 1; }

ROOTPASS= USERPASS=
while test $# -gt 0; do
    case "$1" in
        -rp)   shift; ROOTPASS="$1"        ;;
        -rp*)         ROOTPASS="${1#-rp}"  ;;
        -nup)  shift; USERPASS="$1"        ;;
        -nup*)        USERPASS="${1#-nup}" ;;
        *)   error_exit "Unknown switch or argument \"$1\"" ;;
    esac
    shift
done
test -z "$ROOTPASS"  &&  error_exit "No root password was given"
test -z "$USERPASS"  &&  error_exit "No user password was given"

# Etcetera
 
2 members found this post helpful.
Old 09-02-2011, 08:57 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by KenJackson View Post
Yeah, you could use getopts.
But here's my favorite way to handle this:
That's an interesting solution. You might make it a little more flexible, though:
Code:
#!/bin/bash

error_exit() { echo "ERROR: $*" 1>&2; exit 1; }

unset ROOTPASS USERPASS others

while test $# -gt 0; do
    case "$1" in
        -rp)   shift; ROOTPASS="$1"        ;;
        -rp*)         ROOTPASS="${1#-rp}"  ;;
        -nup)  shift; USERPASS="$1"        ;;
        -nup*)        USERPASS="${1#-nup}" ;;
        --)    shift; break ;;
        -*)    error_exit "Unknown switch or argument \"$1\"" ;;
        *)     others=( ${others[@]-} "$1" ) ;;
    esac
    shift
done

others=( ${others[@]-} "$@" )

test -z "$ROOTPASS"  &&  error_exit "No root password was given"
test -z "$USERPASS"  &&  error_exit "No user password was given"

for other in "${others[@]}"; do
  echo "OTHER: $other"
done

# Etcetera
This allows for non-option arguments and the "--" end-of-options separator.
Kevin Barry
 
1 members found this post helpful.
Old 09-03-2011, 02:20 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
If the OP wants to use the real getopts, this page details its use rather well:

http://wiki.bash-hackers.org/howto/getopts_tutorial

As for the above function, it's not a bad solution for a simple script, although in the end it's really just a home-made, simplified getopts-style processor. Unlike getopts, you can personalize your option formats, but on the other hand it won't easily handle option combos (at least not without defining all the possible combinations), or catch missing argument errors without doing further tests.

I'd also like to add my two cents to the above:
Code:
while test $# -gt 0; do

# can be changed to...

while (( $# > 0 )); do

# or even just...

while (( $# )); do
Code:
others=( ${others[@]-} "$1" ) 
others=( ${others[@]-} "$@" )

# can be changed to...

others+=( "$1" )
others+=( "$@" )
Or is there any reason to use ${others[@]-} instead of ${others[@]}? All the first one does is set an empty element if the array was previously empty.

.........

PS to frieza: I posted a comment earlier to your previous thread with some suggestions for improving your script. Check it out.
 
2 members found this post helpful.
Old 09-04-2011, 07:39 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
The best solution is probably to use getopt (not getopts, but getopt).

Code:
opts="$(getopt --alternative --quiet --shell=bash  -l 'rp:,nup:' -- "$@")"
if [[ $? != 0 ]]
then
    echo "Invalid option syntax" 1>&2
    print_usage 1>&2
    exit 1
fi
eval set -- "$opts"

options_finished="no"
while (( $# > 0 ))
do
    if [[ $options_finished = no ]]
    then
        case "$1" in
            -rp)
                shift
                rootpass="$1"
                ;;
            -nup)
                shift
                userpass="$1"
                ;;
            --)
                options_finished=yes
                ;;
            *)
                echo "Invalid option \"${1}\"" 1>&2
                print_usage 1>&2
                exit 1
                ;;
        esac
    else
        # do somethong with non-option argument in $1
    fi
    shift
done
 
Old 09-04-2011, 08:20 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
The best solution is probably to use getopt (not getopts, but getopt).
This doesn't seem much more convenient than the other solutions given, and it depends on an external program.
Kevin Barry
 
Old 09-06-2011, 08:37 PM   #8
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by ta0kira View Post
This doesn't seem much more convenient than the other solutions given, and it depends on an external program.
I find getopt convenient when I want a script to handle short and long options (-o, --option), since getopts supports short options only.
 
  


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
Passing arguments to shell script like perl script get-opts somupl86 Linux - Newbie 2 12-02-2010 11:14 PM
How to pass arguments in a shell script? Drigo Linux - Newbie 1 11-14-2009 11:55 AM
C program to execute Shell script with arguments baigmd Programming 4 10-27-2009 09:51 PM
LXer: Parsing arguments for your shell script LXer Syndicated Linux News 0 07-18-2007 03:17 AM
Passing arguments to a shell script subu_s Programming 3 09-02-2005 05:13 AM

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

All times are GMT -5. The time now is 05:34 AM.

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