M
M
Michael2016-01-13 15:28:55
linux
Michael, 2016-01-13 15:28:55

Linux bash virtualhost apache. How to pass parameters?

I don't know bush well. I want to write a script that would create virtual hosts for Apache. I wrote the script and it works. But then the idea came up to add the ability to specify aliases to the virtual host. I planned to implement all this according to the appropriate flags.
For example:

addvhost -host example.dev -dir /home/domains/ -alias adm.example.loc

This idea came up when I decided to try Yii2 advanced.
Now there is this script:
#!/bin/bash 

set -e

ME=`basename $0`

function printHelp() {
    echo "Скрипт добавления виртуальных хостов"
    echo
    echo "Используйте: "$ME" с опциями:"
    echo "  -h     имя виртуального хоста"
    echo "  -a     алиас"
    echo "  -d     путь до директории"
    echo
    exit 0; 
}

function writeHost() {
    echo $host
echo $alias
echo $path

тут само добавление и тд..
}

# Если скрипт запущен без аргументов, открываем справку.
if [ $# = 0 ]; then
    printHelp
fi


while getopts "h:a:d:" opt;
do
    case $opt in
 	h) host=$OPTARG;
    writeHost
    ;;
  a) alias=$OPTARG;
    writeHost
    ;;
  d) path=$OPTARG;
    writeHost
    ;;
  *) echo "Неправильный параметр";
     echo "Для вызова справки запустите $ME -help";
     exit 1
     ;;
  esac
done

Actually what is the problem. if you call the script, then the following will appear in the console
[email protected] ~ $ sudo addvhost1 -h text -d dddd -a alias
text
text
dddd
text
alias
dddd

those. getopts cycles through and all options are visible only when the queue reaches aliasa. How to get around this, I don’t understand .. Maybe someone knows? Or at least tell me where to go? maybe instead of getopts there is something more suitable?
I found the script itself on Habré in some article, I'm just trying to adapt a little to my tasks ..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Kryvel, 2016-01-13
@mscrack

So it's all good, isn't it?
getopts handles all the options, and you, after it does everything, run writeHost

while getopts "h:a:d:" opt;
do
    case $opt in
 	h) host=$OPTARG;
    ;;
  a) alias=$OPTARG;
    ;;
  d) path=$OPTARG;
    ;;
  *) echo "Неправильный параметр";
     echo "Для вызова справки запустите $ME -help";
     exit 1
     ;;
  esac
done

writeHost

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question