0

is ist Possible, that i check the names in a until queue ?

b=1      
 read -p "Enter how much databases you want to install: " COUNTER
    until [ ${a} -eq ${COUNTER} ]; do

    a=$((a+1))


    echo "What is the name for the $((b++)) database ?"
        read name

    if [ $name == "already there" ]; then
    echo " Please, don't use the same name for a database!"
    exit 1
    else
    :
    fi

I want, that the script exit when the name is already there...

any ideas ?

user247702
  • 21,902
  • 13
  • 103
  • 146
Atlantikdiver
  • 132
  • 1
  • 14

2 Answers2

1

Use double brackets to enclose your condition or double quote your variable :

if [[ $name == "already there" ]]; then
    echo " Please, don't use the same name for a database!"
    exit 1
else
    #...
fi

or

if [ "$name" == "already there" ]; then
    echo " Please, don't use the same name for a database!"
    exit 1
else
    #...
fi
epsilon
  • 2,604
  • 12
  • 21
  • I will use `if [ "x$name" == "xalready there" ]; then` in case user just presses Enter without giving any value to $name – ray Dec 04 '13 at 09:22
1

UPDATED

you need to save entered name into array, and check whether newly inserted name is in there or not . array array checking is came from here. Check if an array contains a value

#!/bin/bash

has_element ()
{
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}

b=1
read -p "Enter how much databases you want to install: " COUNTER

let -a CONTAINER

a=0
until [ ${a} == ${COUNTER} ]
do
    a=$((a+1))

    echo "What is the name for the $((b++)) database ?"
    read name


    if has_element "$name" "${CONTAINER[@]}"
    then
        echo "already has"
        exit
    fi

    CONTAINER+=($name)

done

Orig. Answer

I think you're almost there. modified some error.

b=1
read -p "Enter how much databases you want to install: " COUNTER

a=0
until [ ${a} == ${COUNTER} ]
do
    a=$((a+1))

    echo "What is the name for the $((b++)) database ?"
    read name

    if [ "$name" == "already there" ]
    then
        echo " Please, don't use the same name for a database!"
        exit 1
    else
        echo "stay..."
    fi

done
Community
  • 1
  • 1
Jason Heo
  • 9,036
  • 2
  • 30
  • 50