Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use a variable in a bash script file path

Posted on 2009-07-13
4
Medium Priority
?
684 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:alexanderfoti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 400 total points
ID: 24838546

#!/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
 
LVL 5

Expert Comment

by:dcesari
ID: 24838553
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
 
LVL 1

Author Comment

by:alexanderfoti
ID: 24838816
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
 
LVL 5

Accepted Solution

by:
dcesari earned 600 total points
ID: 24839089
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question