1

I've written a long bash script for configuring an SSH daemom, etc on android. Problem is, it has gotten too long & at times other users or I may only want to configure certain aspects of what the script has to offer. Instead of having to go through entire script each time it is ran I would like to add options that dictate what will be ran but still be able to run script as whole when nothing is specified.

Something like:

myscript -d -o -p

Or

myscript -dop    

The script can be found here. I think its too long to be posted here.

Basically it's broke up into 5 blocks like this:

#!/system/xbin/bash

echo "---SECTION-ONE---"

read
if test then  

while true
do

I looked up using getopts and can't quite wrap my head around it just yet without an example that matches with what I'm doing. Thats why I'm posting here ;) As always thanks for any inpout.

Geofferey
  • 2,862
  • 6
  • 21
  • 24
  • See a [getopts tutorial](http://wiki.bash-hackers.org/howto/getopts_tutorial) and an [example](http://stackoverflow.com/questions/16483119/example-of-how-to-use-getopts-in-bash). What should the `-d -o -p` options do? – zerodiff Aug 28 '14 at 21:28
  • If you edit the question to make it a little clearer what you want to do and what you've tried so far, we will be more able to help you. – Tom Fenech Aug 28 '14 at 21:45
  • Also you could make some symlinks to your script and do different things by detecting through which name it was called looking into `$0`. – user3132194 Aug 29 '14 at 04:24
  • If at all possible could you show me the symlink method, just so I could learn another way of doing things? Sorry it took me so long to reply, internet has become a scarce commodity for me. – Geofferey Aug 30 '14 at 20:07

2 Answers2

2

You can break down the different bodies of the script into functions that can be called when an option is present.

#!/bin/bash

printing_stuff (){
    echo "---SECTION-ONE---"
}

test_input (){
    read -p "Enter something: " input
    if [[ $input == something ]]; then
        echo "test successful"
        # do some stuff
    fi
}

body_loop (){
    while true; do
        echo "this is the loop"
        # some stuff
        break
    done
}

if [[ $@ ]]; then
    while getopts "dop" opt; do
        case $opt in
            d)
                printing_stuff
                ;;
            o)
                test_input
                ;;
            p)
                body_loop
                ;;
            \?)
                ;;
        esac
    done
else
    printing_stuff
    test_input
    body_loop
fi

The [[ $@ ]] test is done to only run the getopts loop if arguments to the script exist, otherwise run everything.

John B
  • 3,566
  • 1
  • 16
  • 20
  • 1
    Thanks for this answer John, it was exactly what I needed. Sorry it took so long to reply... Checkout my final product thanks to you. http://pastebin.com/V3E04CdJ – Geofferey Aug 30 '14 at 20:13
1

Here is an example with those options, but I'll edit if there's more info. This assumes the arguments don't take any variable text:

#!/bin/bash

usage() {
    echo "$0 [-d] [-o] [-p]"
    exit 1
}

d=false o=false p=false

while getopts "dop" option; do
    case "${option}" in
        d)
            d=true
            ;;
        o)
            o=true
            ;;
        p)
            p=true
            ;;
        *)
            usage
            ;;
    esac
done

echo "d = $d"
echo "o = $o"
echo "p = $p"

Example usage:

$ ./test.sh -dop
d = true
o = true
p = true
$ ./test.sh -d -o -p
d = true
o = true
p = true
$ ./test.sh -d
d = true
o = false
p = false

You can set variable values to anything you wan to test for, e.g.:

if [ "$d" == "true" ]; then
    # do something
else
    # do something else
fi
zerodiff
  • 1,690
  • 1
  • 18
  • 23