0

I need to export the values from input. However it's failing in bash.

Here's the code -

echo "Please enter HOST :"
read RDSHOST && export RDSHOST

And my value is something like - myserver.abc-zone5.com

I am getting this error when i run the script - unexpected EOF while looking for matching `"' syntax error: unexpected end of file

Need to understand how can i export my variable in double quotes to avoid this error

5
  • 2
    Check that your quotes are the normal ASCII quotes, i.e. ", and not some other fancy or "smart" quote. Check the whole script, not just that part. Syntax highlighting often helps, as a quoted string where the other part is the ASCII quote and the other something else often leaves everything after it colored as a quoted string. There's nothing wrong with that snippet, as far as I can see.
    – ilkkachu
    Jul 21, 2022 at 12:36
  • Yeah seems so, i just tested it. What i am trying to do is get multiple values and then add those values to a command to run However after two input i am getting the error. Here's the script - echo "Please enter REGION to login :" read region && export region echo "Please enter HOST to login :" read HOST && export HOST echo Please enter Port to login :" read localport && export localport #echo "Please enter username to login : " #read username && export username sql "host=$HOST port=$localport user=$username --region=$region
    – Devendra
    Jul 21, 2022 at 13:21
  • can you show your code, what exactly you doing, what error you getting
    – shas
    Jul 21, 2022 at 13:23
  • echo "Please enter REGION to login :" read region && export region echo "Please enter HOST to login :" read HOST && export HOST echo Please enter Port to login :" read localport && export localport echo "Please enter username to login : " read username && export username sql "host=$HOST port=$localport user=$username --region=$region
    – Devendra
    Jul 21, 2022 at 13:26
  • are you pasted correct code?, as i can see one DOUBLE QUOTE missing at Please enter Port to login :".. check
    – shas
    Jul 21, 2022 at 13:33

2 Answers 2

1
echo "Please enter REGION to login :" 
read region && export region 
echo "Please enter HOST to login :" 
read HOST && export HOST
echo "Please enter Port to login :" 
read localport && export localport
echo "Please enter username to login : " 
read username && export username sql "host=$HOST port=$localport user=$username --region=$region"

echo "username sql \"host=$HOST port=$localport user=$username --region=$region\""

OUTPUT:

Please enter REGION to login :
ASIA
Please enter HOST to login :
localhost
Please enter Port to login :
6000
Please enter username to login : 
foo
username sql "host=localhost port=6000 user=foo --region=ASIA"
1
  • Thank you. I realised that after pasting the code here
    – Devendra
    Jul 21, 2022 at 14:20
0

shas's answer is good, and I'd like to add that the export attribute of a variable doesn't have to be set after a value is assigned into the variable. This means you can have the exports where they don't distract from the main flow of the script:

# prep for user input
export region
export HOST
export localport
export username

# prompt user for input
echo 'Please enter REGION to login :'
read region
echo 'Please enter HOST to login :'
read HOST
echo 'Please enter Port to login :'
read localport
echo 'Please enter username to login :'
read username

# show and invoke the sql command
echo "sql \"host=$HOST port=$localport user=$username --region=$region\""
sql "host=$HOST port=$localport user=$username --region=$region"

Further, export can take the names of multiple variables to set the attribute, making that part of the code shorter:

# prep for user input
export region HOST localport username

# prompt user for input
echo 'Please enter REGION to login :'
read region
echo 'Please enter HOST to login :'
read HOST
echo 'Please enter Port to login :'
read localport
echo 'Please enter username to login :'
read username

# show and invoke the sql command
echo "sql \"host=$HOST port=$localport user=$username --region=$region\""
sql "host=$HOST port=$localport user=$username --region=$region"

(I also wrapped the prompt strings in single quote marks, since they don't expand any variables, and echoed the sql command before invoking it, so the user knows what the script is doing if the sql command hangs)

You must log in to answer this question.

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