1

I am trying to take a string of options fed to a bash script and assign them to variables with case. I believe case is conditional. Is there a way to have case loop through the options and assign variables? I am using some once and looping through others. The print loop doesn't work after the case statement.

example:

run script: ./ping_ds_replication_setup --bind_dn nate --bind_password password --slave_ds_servers server1 server2 server-name.troweprice.io --master_ds_server server3 --admin_uid nate --admin_password password

output currently: ping_bind_dn: nate bind password: password secondary servers: server1 secondary servers: server2 secondary servers: server-name.troweprice.io master ds server: server3 admin_uid: nate admin_password: password

script:

while [ -n 1 ]; do
    case "$1" in
  --bind_dn)
    ping_bind_dn=$2
    printf "ping_bind_dn: $ping_bind_dn\n"
    shift
    ;;
  --bind_password)
    bind_password=$2
    printf "bind password: $bind_password\n"
    shift
  ;;
  --admin_uid)
    admin_uid=$2
    printf "admin_uid: $admin_uid\n"
    shift
  ;;
--admin_password)
    admin_password=$2
    printf "admin_password: $admin_password\n"
    shift
  ;;
  --master_ds_server)
  master_ds_server=$2
  printf "master ds server: $master_ds_server\n"
  shift
  ;;
  --slave_ds_servers)
    slave_raw_input=( $@ )
    slave_switch_removed=${slave_raw_input[@]:1}
    regex2=$(grep -Po '.*?(?=--)'  <<<  ${slave_switch_removed[@]})
    secondary_ds_server=($(echo "${regex2}"|head -n 1))
     for secondary_server in "${secondary_ds_server[@]}"; do
       printf "secondary servers: $secondary_server \n"
    done
    shift
   esac
   shift
done

for secondary_server in "${secondary_ds_server[@]}"; do
   printf "secondary servers outside case: $secondary_server \n"
done
nlew82
  • 11
  • 4
  • See here for a loop solution: http://stackoverflow.com/a/7948533/402322 – ceving Apr 11 '17 at 14:06
  • I was able to break out of the loop by testing for a space since all of the options have arguments. Using while [[ $1 != "" ]]; do – nlew82 Apr 11 '17 at 15:12

2 Answers2

1

for handling long options getopts won't work, as a workaround you can:

#!/bin/bash

for arg in "$@"; do
    shift
    case "$arg" in
        --hello) echo "$1" ;; 
        --bye) echo "$1"  ;;
        --help) echo "Help" ;;
        *) echo "Option doesn't exist"; exit 1 ;;
    esac
done

output would be:

$ ./test_args.sh --hello "Hi, what's up?" --bye Goodbye! --help --hello Hi!
Hi, what's up?
Goodbye!
Help
Hi!

the only caveat is that you have to quote arguments that have spaces between them.

odradek
  • 993
  • 7
  • 14
0

I think this example can be helpful:

$ cat example.sh
#! /bin/sh
while getopts "h?o:" opt; do                                                 
    case "$opt" in                                                               
    h|\?)                                                                        
        echo help actions                                                           
        ;;                                                                       
    o)                                                                           
        echo $OPTARG                                                         
        ;;                                                                       
    esac                                                                         
done

You can call your script with:

$ ./example.sh -h

to execute h) case, or you can do

$ ./example.sh -o argument

to execute o) case, receiving "argument" in variable OPTARG.

criptobadia
  • 1,984
  • 1
  • 17
  • 30
  • what about when I want to do ./example.sh -o argument -h argument – nlew82 Apr 11 '17 at 14:09
  • this won't work with the example posted in the question that uses GNU-style long options like `--bind_dn`, it only works with short options, like `-h` (but not `--help`) – odradek Apr 11 '17 at 14:28
  • You can use -o argument because of the ":" in the string pattern following 'o' character. You can use ":" in all parameters you want. – criptobadia Apr 12 '17 at 06:23
  • @odradek, you are right, but the original question was edited after sending my answer, in the original question there wasn't any example code. – criptobadia Apr 12 '17 at 06:24