从sh文件运行时,Shell脚本会引发未找到的错误。但是,如果手动输入,则命令有效


19

我正在尝试使用以下脚本为我的网站生成一个站点地图。当我运行它时,sh thsitemap.sh出现这样的错误并创建一个空的sitemap.xml文件:

thsitemap.sh: 22: thsitemap.sh: [[: not found
thsitemap.sh: 42: thsitemap.sh: [[: not found
thsitemap.sh: 50: thsitemap.sh: Syntax error: "(" unexpected

但是作为同一用户,root当我在终端上手动复制并粘贴这些行时,它可以正常工作而没有任何错误,并且sitemap.xml文件具有所有url。有什么问题?我怎样才能解决这个问题?

#!/bin/bash
##############################################
# modified version of original http://media-glass.es/ghost-sitemaps/
# for ghost.centminmod.com
# http://ghost.centminmod.com/ghost-sitemap-generator/
##############################################
url="techhamlet.com"
webroot='/home/leafh8kfns/techhamlet.com'
path="${webroot}/sitemap.xml"
user='leafh8kfns'       # web server user
group='leafh8kfns'      # web server group

debug='n' # disable debug mode with debug='n'
##############################################
date=`date +'%FT%k:%M:%S+00:00'`
freq="daily"
prio="0.5"
reject='.rss, .gif, .png, .jpg, .css, .js, .txt, .ico, .eot, .woff, .ttf, .svg, .txt'
##############################################
# create sitemap.xml file if it doesn't exist and give it same permissions
# as nginx server user/group
if [[ ! -f "$path" ]]; then
    touch $path
    chown ${user}:${group} $path
fi

# check for robots.txt defined Sitemap directive
# if doesn't exist add one
# https://support.google.com/webmasters/answer/183669
if [ -f "${webroot}/robots.txt" ]; then
SITEMAPCHECK=$(grep 'Sitemap:' ${webroot}/robots.txt)
    if [ -z "$SITEMAPCHECK" ]; then
    echo "Sitemap: http://${url}/sitemap.xml" >> ${webroot}/robots.txt
    fi
fi
##############################################
echo "" > $path

# grab list of site urls
list=`wget -r --delete-after $url --reject=${reject} 2>&1 |grep "\-\-"  |grep http | grep -v 'normalize\.css' | awk '{ print $3 }'`

if [[ "$debug" = [yY] ]]; then
    echo "------------------------------------------------------"
    echo "Following list of urls will be submitted to Google"
    echo $list
    echo "------------------------------------------------------"
fi

# put list into an array
array=($list)

echo "------------------------------------------------------"
echo ${#array[@]} "pages detected for $url" 
echo "------------------------------------------------------"

# formatted properly according to
# https://support.google.com/webmasters/answer/35738
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<urlset xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">" > $path

echo ' 
   ' >> $path;
   for ((i=0;i<${#array[*]};i++)); do
echo "<url>
    <loc>${array[$i]:0}</loc>
    <lastmod>$date</lastmod>
    <changefreq>$freq</changefreq>
    <priority>$prio</priority>
</url>" >> $path
   done
echo "" >> $path
echo "</urlset>" >> $path

# notify Google
# URL encode urls as per https://support.google.com/webmasters/answer/183669
if [[ "$debug" = [nN] ]]; then
    wget  -q --delete-after http://www.google.com/webmasters/tools/ping?sitemap=http%3A%2F%2F${url}%2Fsitemap.xml

    rm -rf ${url}
else
    echo "wget  -q --delete-after http://www.google.com/webmasters/tools/ping?sitemap=http%3A%2F%2F${url}%2Fsitemap.xml"

    echo "rm -rf ${url}"
fi
echo "------------------------------------------------------"

exit 0

您如何运行脚本?
cuonglm

@Gnouc sh script.sh
THpubs

8
尝试:bash script.sh,将shmake bash切换到posix模式。我记得这类问题已经得到回答。您可以参考这个:unix.stackexchange.com/questions/44836/...
cuonglm

Answers:


35

以下列方式运行脚本:

bash script.sh

要不就:

./script.sh

bash使用name运行时sh,它将禁用其大多数扩展,例如[[测试运算符。

由于具有#!/bin/bashshebang行,因此无需在命令行上显式指定shell解释器。作为命令运行脚本将使用该行查找外壳。


-1

您的两个 if [[ debug = "...“ ]] ; then...命令都不正确,应读取...

if [ "debug" = "Y" -o "debug" = "y" ]

6
不,代码是有效的bash,只是无效的sh。
glenn jackman 2014年

错误1,第22行[[。
黄狗

@rhubarbdog如果具有#!/ bin / sh,那么您的答案可能会起作用,如果文件具有#!/ bin / bash,则if [[debug =“ ...”]]应该没有问题
Rajesh Hatwar
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.