How to use javascript code in unix shell?

Hi

Need help...I have wrritten one code for html through shell scripting in that i am using java scripts to validate some condition and open the html page without clicking the button....

Code Details

echo "<script type="text/javascript">"
echo "function exec_refresh()"
echo "{"
       echo " window.status = "Redirecting..." + myvar;"
        echo "myvar = myvar + " .";"
        echo "var timerID = setTimeout("exec_refresh();", 100);"
        echo "if (timeout > 0)"
        echo "{"
               echo" timeout -= 1;"
        echo "}"
        echo "else"
        echo "{"
               echo "clearTimeout(timerID);"
               echo " window.status = "";"
                echo "window.location = "Test.com Web Based Testing and Certification Software v2.0";"
        echo "}"
echo "}"
echo "var myvar = "";"
echo "var timeout = 20;"
echo "exec_refresh();"
echo "</script>"

Not positive if you are showing part of your code, or the whole thing? If this is a separate file that is called from another web page you will still need the opening #!/bin/bash. If this is a stand-alone web page you will also need opening and closing html in there. Not sure what your intended conditions are supposed to be either. Try what I've suggested if you haven't already. If you already have, reply back more details on what your conditions are and more code if you have it.

Good luck!

The " inside the javascript will be eaten by the shell because they're not escaped. It doesn't know the quotes are supposed to be for javascript, and handles them itself.

$ echo " window.status = "Redirecting..." + myvar;"
 window.status = Redirecting... + myvar;

$ echo " window.status = \"Redirecting...\" + myvar;"
 window.status = "Redirecting..." + myvar;

$

...but it'll be more efficient and far less typo-prone to do this in a here-document, that will let you plunk it down nearly verbatim:

cat <<EOF
<script type="text/javascript">"
function exec_refresh()
{
window.status = "Redirecting..." + myvar;
myvar = myvar + " .";"
var timerID = setTimeout("exec_refresh();", 100);
if (timeout > 0)
{
timeout -= 1;
}
else
{
clearTimeout(timerID);
window.status = "";
window.location = "Test.com Web Based Testing and Certification Software v2.0";
}
}
var myvar = "";
var timeout = 20;
exec_refresh();
</script>
EOF

${VARIABLES} and `backticks` will still be substituted inside a here document. Escape ` and $ with \ to avoid it.

Note the ending EOF must be at the beginning of the line, not indented!

1 Like

Thanks and i am able to see the page now.. Thanks everyone

If you just want to print verbatim theft, it may be better to use

cat << "EOF"

[/COLOR] to avoid possible parameter expansion, command substitution, and arithmetic expansion by the shell, unless this is what you want...