shell脚本中的HTML到文本转换

时间:2009-05-23 15:11:36

标签: html shell lynx

我编写了一个shell脚本,使用lynx将HTML源代码转换为纯文本。

这是:

#!/bin/sh

if [ -f = "/usr/bin/lynx" ]
  then
    if [ -f = "$1" ]
      then
        lynx -dump $1 > $2
      else
        echo "File $1 does not exist!"
    fi
  else
    echo "Lynx is not installed!"
fi

现在,尽管lynx存在于正确的目录中,并且我传递了正确的参数,但我得到“Lyns没有安装!”消息或(如果我评论第一个测试)“文件$ 1不存在!”。我不擅长sh,所以有人能告诉我这个剧本有什么问题吗?

4 个答案:

答案 0 :(得分:4)

我认为第一个是错误的,应该用

替换
if [ -f /usr/bin/lynx ]

答案 1 :(得分:2)

尝试删除“-f =”并将其保持为“-f”

答案 2 :(得分:0)

现在就是这样:

#!/bin/sh

if [ -f /usr/bin/lynx ]
  then
    if [ -f $1 ]
      then
        lynx -dump $1 > $2
      else
        echo "File $1 does not exist!"
    fi
  else
    echo "Lynx is not installed!"
fi

测试问题已经消失,但现在我收到了这个错误:

第7行:$ 2:模棱两可的重定向

虽然

lynx -dump site.html>如果从控制台

运行,site.txt工作正常

答案 3 :(得分:0)

LINKS=`which links`
if [ -x $LINKS ]; then
   ...
else
   ...
endif

如果没有安装在/ usr / bin /中怎么办?如果由于某种原因它不可执行怎么办?