1

This is my code

#!/bin/bash
function boo {

    function goo {
        echo "Please enter name"
            read name;
        echo "Hello $name"

    }

    function xoo {

        echo "Please enter number"
        read numl;
        echo "$numl"
    }

    read -p "`echo $'\n> \n>'`  Name N or Number S`echo $'\n> \n>'`" var
        if [[ $var =~ [nN](es)* ]]
            then    
                goo   
                elif [[ $var =~ [sS](es)* ]]
                then
                xoo
                fi

}
boo
read -p "`echo $'\n> \n>'`  To Repeat press Y`echo $'\n> \n>'`" prompt
if [[ $prompt =~ [yY](es)* ]]
then
boo    
else
echo""
fi

Initially I am able to successfully run this script by double clicking but when I tried to repeat it its exiting

> 
>  Name N or Number S
> 
>n
Please enter name
john
Hello john

> 
>  To Repeat press Y
> 
>y

> 
>  Name N or Number S
> 
>n
Please enter name
ajay

I am able to print hello john but when i enter ajay its exiting. Any idea whats happening

1 Answer 1

1

Your problem is the if…then. After the first call off boo you check the value of prompt and boo will be started, if prompt is y. After that, there is no more code to execute and the script exits.

Using this script

#!/bin/bash
function boo {
    function goo {
        echo "Please enter name"
        read -r name;
        echo "Hello $name"
    }

    function xoo {
        echo "Please enter number"
        read -r numl;
        echo "$numl"
    }

    read -rep $'\n> \n> Name N or Number S \n> \n> ' var
    if [[ $var =~ [nN](es)* ]]; then    
      goo   
    elif [[ $var =~ [sS](es)* ]]; then
      xoo
    fi
}

while true; do
  boo
  read -rep $'\n> \n> To Repeat press Y \n> \n> ' prompt
  if [[ ! $prompt =~ [yY](es)* ]]; then
    break
  fi
done

exit 0

Example

% ./foo

> 
> Name N or Number S 
> 
> N
Please enter name
abc
Hello abc

> 
> To Repeat press Y 
> 
> y

> 
> Name N or Number S 
> 
> N
Please enter name
def
Hello def

> 
> To Repeat press Y 
> 
> y

> 
> Name N or Number S 
> 
> N
Please enter name
jjj
Hello jjj

> 
> To Repeat press Y 
> 
> n
%
1
  • Thanks it worked but can you tell me where i made the mistake. why read -r , read -rep and while loop
    – potholiday
    Jul 29, 2015 at 7:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .