How to use a variable in a bash script file path

Hi all. I am trying to code a bash script that will generate a redirect page based on a couple of questions but im having trouble. I have included the code below. When run all it ouputs is this:

Enter the domain name you would like to redirect from, eg owa.alexfoti.com
testdomain1234.com
Enter the URL that you want to redirect to, eg http://71.2.3.46/exchange
http



<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN>
<html>
<head>
<title>Redirecting.....</title>
<meta http-equiv=REFRESH content=0
 done :)

It does not write to the file....

Thanks in advance for all the help.
#!/bin/bash
 
echo "Enter the domain name you would like to redirect from, eg owa.alexfoti.com"
read domainname
 
echo "Enter the URL that you want to redirect to, eg http://78.105.8.96/exchange"
 
read redirecturl
 
echo  "
 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Redirecting.....</title>
<meta http-equiv="REFRESH" content="0;url=$redirecturl"></HEAD>
<BODY>
Redirecting.......
</BODY>
</HTML>
 
" > "/opt/lampp/htdocs/$domaininame/index.html"
 
echo " done :)"

Open in new window

LVL 1
alexanderfotiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:

#!/bin/bash
 
echo "Enter the domain name you would like to redirect from, eg owa.alexfoti.com"
read domainname
 
echo "Enter the URL that you want to redirect to, eg http://78.105.8.96/exchange"
 
read redirecturl
 
 
cat >/opt/lampp/htdocs/$domaininame/index.html <<EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Redirecting.....</title>
<meta http-equiv="REFRESH" content="0;url=$redirecturl"></HEAD>
<BODY>
Redirecting.......
</BODY>
</HTML>
EOF
 
echo "done :)"

Open in new window

0
dcesariCommented:
You surely have to quote all the double quote characters " within shell double quotes, as

!DOCTYPE HTML PUBLIC \"

etc., otherwise you will not get double quotes in the output, and this incidentally solves also your file-redirection problem, although at once I cannot explain why.
0
alexanderfotiAuthor Commented:
Right I have made some changes to the script: it now looks like this

But it now fails on the line:

echo '<meta http-equiv="REFRESH" content="0;url="$redirecturl"></HEAD>' >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"


Stating:

./redirect.sh: line 14: syntax error near unexpected token `<'
./redirect.sh: line 14: `echo "<meta http-equiv="REFRESH" content="0;url="$redirecturl"></HEAD>" >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"'


Any ideas?
read redirecturl
 
echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">" > "/opt/lampp/htdocs/$DOMAINNAME/index.html"
echo "<html>" >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"
echo "<head>" >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"
echo "<title>Redirecting.....</title>" >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"
echo '<meta http-equiv="REFRESH" content="0;url="$redirecturl"></HEAD>' >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"
echo "<BODY>" >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"
echo "Redirecting......." >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"
echo "</BODY>" >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"
echo "</HTML>" >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"
 
echo " done :)"

Open in new window

0
dcesariCommented:
well you still have unquoted double quotes and, anyway, if you use single quotes to avoid this problem, like in the line

echo '<meta http-equiv="REFRESH" content="0;url="$redirecturl"></HEAD>' >> "/opt/lampp/htdocs/$DOMAINNAME/index.html"

then environment variables like $redirecturl are not expanded, which is actually required here, so my advice is: either to use the approach suggested by Tintin, or to revert to the initial version with a single echo statement but with quoted special characters, e.g.:
#!/bin/bash
 
echo "Enter the domain name you would like to redirect from, eg owa.alexfoti.com"
read domainname
 
echo "Enter the URL that you want to redirect to, eg http://78.105.8.96/exchange"
 
read redirecturl
 
echo  "
 
 
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
<html>
<head>
<title>Redirecting.....</title>
<meta http-equiv=\"REFRESH\" content=\"0;url=$redirecturl\"></HEAD>
<BODY>
Redirecting.......
</BODY>
</HTML>
 
" > /opt/lampp/htdocs/$domaininame/index.html
 
echo " done :)"
~

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.