V
V
Vitaly2015-10-28 12:00:48
linux
Vitaly, 2015-10-28 12:00:48

How to deal with user input and if else in bash?

I want to do something like this:

#!/bin/bash

read -p "сменить файл конфигурации (y/n)? " answer
case ${answer:0:1} in
    y|Y )
        read -p "сменить хост? :"  dohost
        echo $dohost
        if [$dohost  = y|Y ]; then
                read -p "укажите новый хост : " host
                echo $host
        else
                host = "champ"
                echo $host
        fi 
       read -p "сменить порт? :"  doport
      if [$doport  = y|Y ]; then
                read -p "укажите новый порт : "  port
                echo $port
        else
                port = "8001"
                echo $port
        fi 
        touch myjson.json
        cat <<-EOF >> myjson.json
        {
        "host" : "$host",
        "port" : "$port"
        }
        EOF
         ;;
    * )
        echo No
    ;;
esac

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Y
younghacker, 2015-10-28
@younghacker

Oleg, this is a question from the category of correcting errors in my script. :)
Vitaliy,
To begin with, insert a space after the opening bracket
. Before the closing brackets, you put it for some reason. Where is the beauty of symmetry? :)
Then check the documentation for the difference between single brackets [ ] and double brackets
You need double brackets. Since inside is a regular expression. And also put a tilde after = and deal with regular expressions.
This code will work on any "y" in any line. for example answer nnnooooyes. :)
I think it would be better:
you can add the -n 1 switch to the read statement,
handle the situation with a negative or incorrect answer (for example, Cyrillic).
and then deal with the cut command
why is touch there? why >> ? do you need to complete the file? What happens after two script runs? I think it might look like this:

`cat > myjson.json <<EOF
        {
        "host" : "$host",
        "port" : "$port"
        }
        EOF`

To edit a configuration file from a script, learn sed.
And finally, remake the algorithm :)
Along the way, figure out the design and indentation by reading other people's good scripts.

O
Oleg Tsilyurik, 2015-10-28
@Olej

I want to do something like this:

Well, what is the problem? if you want to do it, do it...
Author: Mendel Cooper
Translation: Andrey Kiselev

3
3vi1_0n3, 2015-10-28
@3vi1_0n3

It's even easier there:

#!/bin/bash

# Дефолтные значения
HOST="champ"
PORT="8001"

read -p "сменить файл конфигурации? (y/n): " -n 1
if [ "$REPLY" == "y" ];
then
    read -p "сменить хост? (y/n): " -n 1
    if [ "$REPLY"  == "y" ];
    then
        read -p "укажите новый хост : " HOST
        echo $HOST
    fi 
    read -p "сменить порт? (y/n): " -n 1
    if [ "$REPLY"  == "y" ]
    then
        read -p "укажите новый порт : " PORT
        echo $PORT
    fi 

    cat <<EOF > myjson.json
{
"host" : "$HOST",
"port" : "$PORT"
}
EOF
else
    echo "Exiting without changes"
fi

A
Azazel PW, 2015-10-28
@azazelpw

If it’s simple,
I have no idea what the json module does for you, so it’s probably crooked there.

#!/bin/bash

read -p "сменить файл конфигурации? (y/n)" answer
if [ $answer = "y" ];
then
        read -p "сменить хост? (y/n):"  dohost
        echo $dohost
        if [ "$dohost"  = "y" ];
        then
                read -p "укажите новый хост : " host
                echo $host
        else
                host = "champ"
                echo $host
        fi 
       read -p "сменить порт? (y/n):"  doport
      if [ "$doport"  = "y" ]; then
                read -p "укажите новый порт : "  port
                echo $port
        else
                port = "8001"
                echo $port
        fi 

   touch myjson.json
        cat <<-EOF >> myjson.json
        {
        "host" : "$host",
        "port" : "$port"
        }
        EOF

    else
        echo "No"

fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question