-1

I have a question about a bash script:

My Script:

while (( $# )) ; do
    case "$1" in
        "-ip")
            ip="$2"
            shift;;
        "-port")
            port="$2"
            shift;;
        "-portstep")
            portstep="$2"
            shift;;
        "-maxplayers")
            maxplayers="$2"
            shift;;
        "-password")
            password="$2"
            shift;;
        "-othercommand")
            othercommand="$2"
            shift;;
    esac
    shift
done

I run it with:

./script -ip myip -port myport -portstep myportstep -maxplayers mymaxplayers -password mypassword

This is for an gameserver webinterface. I can modify the command like I want.

But now I can set different variables which can be run, and all this commands will set after -password

now I won't set an password I run for example

./script -ip myip -port myport -portstep myportstep -maxplayers mymaxplayers -password -othercomamnd myothercomand

now it will set the password to -othercomand,

how i can modify this script so the password variable will set to "nothing" if nothing is given as password, so my script edit the file correctly with no password and add the othercommand correctly.

David W.
  • 105,218
  • 39
  • 216
  • 337
GenXRoad
  • 1
  • 4
  • 2
    Why are you using the `-password` flag at all if you don't want to set a password? Just remove `-password` from the call entirely. – Etan Reisner Feb 25 '15 at 16:20
  • because other users should only run there gameserver with password – GenXRoad Feb 25 '15 at 16:24
  • 1
    That doesn't answer the question. Why are you, when you don't want to set a password, using the flag to set a password? Why not just leave `-password` off that running of `script` entirely? So instead of going from `./script ... -password pAsSwOrD ...` to `./script ... -password ...` go to `./script ... ...`. – Etan Reisner Feb 25 '15 at 16:53
  • i can only set 1 startscript command in the webinterface, my friends should only run there gameserver with Password, and i won't run it with Password, so i can only run all Servers with Password or all without Password. bit this is not the thig i will do, so i Need an solution to skipp the Password if not set or even set it to nothing, but not to -othercommand – GenXRoad Feb 25 '15 at 17:03
  • 1
    Can legal passwords start with `-`? Can you use `-password ""` to set an empty string password? (I'm assuming your "startscript" is that `./script ....` command?) – Etan Reisner Feb 25 '15 at 17:04
  • in my webinterface i Need to set: ./script -ip gsip -port gsport -Password gspasswd <- this will be read out from mysql table i think i can run with -Password "gspasswd" and then without an Password it will send -Password "" so i Need to remove the "" this doublequotes should given as $2 in Password command, i will try it – GenXRoad Feb 25 '15 at 17:05
  • I'm not sure what that information was supposed to tell me. – Etan Reisner Feb 25 '15 at 17:07
  • sorry for my bad english, i'm from Germany... i think i can remove the doublequotes before writing the Password in the configuration file, so that it will be write as password = and not as password = "" or password = -othercommands – GenXRoad Feb 25 '15 at 17:12
  • Your script will not see the quotes in the argument at all. The shell will remove them. You will just get an empty string. – Etan Reisner Feb 25 '15 at 17:13

2 Answers2

2

in a hurry, but I will drop this here, maybe it helps you

while getopts 'M:U:Q:d:h' OPT; do
  case $OPT in
    M)  server=$OPTARG;;
    U)  user=$OPTARG;;
    Q)  query=$OPTARG;;
    h)  hlp="yes";;
    *)  unknown="yes";;
  esac
done

Later edit: got home, changed as per OP request

cat test3.sh
#!/bin/bash

while getopts 'M:p:s:m:Y:o' OPT; do
  case $OPT in
    M)  server_ip=$OPTARG;;
    p)  server_port=$OPTARG;;
    s)  server_port_step=$OPTARG;;
    m)  server_max_players=$OPTARG;;
    Y)  server_password=$OPTARG;;
    o)  server_other_command=$OPTARG;;
    *)  unknown="yes";;
  esac
done

case $server_password in
        "-o")

                server_other_command="${@: -1}"
                server_password=""
        ;;
        *)
                server_other_command="${@: -1}"
                server_password="${@: -3:1}"
        ;;
esac

echo "ip -> $server_ip"
echo "port -> $server_port"
echo "portSteps -> $server_port_step"
echo "max players -> $server_max_players"
echo "password -> $server_password"
echo "other command -> $server_other_command"

result1 with password

./test3.sh -M myip -p myport -s myportstep -m mymaxplayers -Y 123 -o asd
ip -> myip
port -> myport
portSteps -> myportstep
max players -> mymaxplayers
password -> 123
other command -> asd

result2 without password

./test3.sh -M myip -p myport -s myportstep -m mymaxplayers -Y -o asd
ip -> myip
port -> myport
portSteps -> myportstep
max players -> mymaxplayers
password ->
other command -> asd

Later edit2: If the above does not help, please have a look here:

Bash getopts: reading $OPTARG for optional flags?

Community
  • 1
  • 1
candymanuu
  • 110
  • 7
  • as i said, i was (still am) in a hurry. now i'm logging from my phone, did not have time to explain, or to modify to help. also said it may help, not that it solves his issue. it's not hard to make modification to handle his parameters – candymanuu Feb 25 '15 at 17:36
  • Much better but still quite different than the OPs code (long vs. short arguments for starters) and only handles the single case of `-p -o` and so isn't a general solution. It also disallows any password from starting with `-o` as a result. – Etan Reisner Feb 25 '15 at 18:42
  • well...long vs short arguments ... should not be a issue...only if some other scripts/application rely on this to use long args. As for the pass starting with "-o" ... there is no issue there `./test3.sh -M myip -p myport -s myportstep -m mymaxplayers -Y -o123 -o asd ip -> myip port -> myport portSteps -> myportstep max players -> mymaxplayers password -> -o123 other command -> asd ` – candymanuu Feb 25 '15 at 18:46
  • True, not starting with `-o` just a literal password of `-o`. That's much less of an issue. My mistake. It is still *incredibly* sensitive to argument order though. So while it might work for this it isn't a general solution in any way. – Etan Reisner Feb 25 '15 at 18:48
  • maybe the OP can use it like this :D – candymanuu Feb 25 '15 at 19:02
0

Test $2 to see if it is either empty or starts with a dash. If $2 starts with a dash, you know you've picked up the next parameter, and the there's no parameter for -password. If there's no $2, you've come to the end of the parameter list.

    while (( $# )) ; do
        case "$1" in
            "-ip")
                ip="$2"
                shift;;
            "-port")
                port="$2"
                shift;;
            "-portstep")
                portstep="$2"
                shift;;
            "-maxplayers")
                maxplayers="$2"
                shift;;
            "-password")
                password="$2"
                if [[ $password = -* || $2 = "" ]]
                then
                    password=""
                else
                    shift
                fi;;
            "-othercommand")
                othercommand="$2"
                shift;;
        esac
        shift
    done
David W.
  • 105,218
  • 39
  • 216
  • 337
  • This disallows passwords that start with `-` for no real reason since you've handled (badly but we'll get to that) the right solution here. Passing an empty argument already Just Works (and works here, coincidentally, despite the lack of shift because the blank argument falls through the case statement on the next loop and gets shifted away then). – Etan Reisner Feb 25 '15 at 20:47
  • I'm just answering the OP's question. As you commented, if you don't need a password, don't provide `-password`, but this wasn't acceptable to the OP. The requirements that the OP had makes it impossible for the password to start with a dash because I have to assume that if the next argument starts with a dash, it's another parameter and not the password. Maybe the solution is to have a `-nopassword` parameter to handle what the OP wants. – David W. Feb 26 '15 at 13:12
  • The solution is either to not use `-password` at all (which was rejected) or to always pass an empty argument for the password, as I said in my comment on the OP and here. But yes, both this and the other answer "solve" the problem. I was just pointing out the limitations of those solutions. – Etan Reisner Feb 26 '15 at 13:40