Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

What am I doing wrong in this script?

#!/bin/bash

cat > /tmp/flash-viewer.htm <<- _EOF_
<HTML><HEAD><TITLE>Flash Viewer</TITLE></HEAD><BODY>
<object width=\"1280\" height=\"720\" data=\"$1\"></object>
</BODY></HTML>
_EOF_

if [ -n $BROWSER ]; then
  $BROWSER '/tmp/flash-viewer.htm'
elif which xdg-open > /dev/null; then
  xdg-open '/tmp/flash-viewer.htm'
elif which gnome-open > /dev/null; then
  gnome-open '/tmp/flash-viewer.htm'
else
  echo "Could not detect the web browser to use."
fi

The script gives these errors:

joe@U13:~/Script$ ./html-flash-viewer.sh "/home/joe/video.swf"
/tmp/flash-viewer.htm: line 1: <HTML><HEAD><TITLE>Flash: command not found
/tmp/flash-viewer.htm: line 2: <object: command not found
/tmp/flash-viewer.htm: line 3: </BODY></HTML>: No such file or directory

Is there some kind of escape sequence I'm missing? I've already tried using \ to escape the LT/GT brackets and enclosing the html in single and double quotes. Each produces similar errors.

share|improve this question
    
What does $BROWSER actually evaluate to? it looks like your script is trying to execute the contents of the HTML file, rather than pass it as an argument to the command. –  steeldriver May 23 at 13:05
    
How are you trying to execute the script? You have not set the BROWSER variable anywhere, when is it set? –  i08in May 23 at 13:11
2  
if works for me if I launch it with BROWSER=firefox ./your_script.sh –  Sylvain Pineau May 23 at 13:12

1 Answer 1

up vote 5 down vote accepted

If $BROWSER is empty [ -n $BROWSER ] becomes just [ -n ]. That tests if -n has non-zero length, which is always true.

Then $BROWSER '/tmp/flash-viewer.htm' is executed which actually is just '/tmp/flash-viewer.htm' if $BROWSER is empty.

Use

if [ -n "$BROWSER" ]; then

so that there's always a second argument.

To find out the exact path of your browser, use which <browser>, replacing <browser> with the browser you are trying to open the file with.

For example,

which firefox

would return /usr/bin/firefox, so you could do:

BROWSER=/usr/bin/firefox
share|improve this answer
1  
Or you can use Bash's double square brackets. Also, type -p is better than which (for one thing, it's a built-in). –  Dennis Williamson May 23 at 16:08
    
While this answer is insightful, it does not help me. The browser selection code was taken from another question on this site and it works for me. The problem lies with the "cat" statement (aka "Here document") not writing the html tags. –  sienile May 23 at 21:57
    
According to the error messages there are errors when running (not writing) /tmp/flash-viewer.htm. –  Florian Diesch May 24 at 1:04
    
Right you are. I removed the $BROWSER section of the if statement and xdg-open launched the file. Thanks. -- I had thought $BROWSER was a global system environment variable. ...Wonder why it worked in that other guy's script. –  sienile May 24 at 10:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.