I'm trying to use the following script to generate a sitemap for my website. When I run it as sh thsitemap.sh I get an error like this and creates an empty sitemap.xml file:thsitemap.sh: 22: thsitemap.sh: [[: not foundthsitemap.sh: 42: thsitemap.sh: [[: not foundthsitemap.sh: 50: thsitemap.sh: Syntax error: ( unexpectedBut as the same user root when I manually copy and paste these lines on the terminal, it works without any error and the sitemap.xml file have all the urls. What's the problem? How can I fix this?#!/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.comwebroot='/home/leafh8kfns/techhamlet.com'path=${webroot}/sitemap.xmluser='leafh8kfns'       # web server usergroup='leafh8kfns'      # web server groupdebug='n' # disable debug mode with debug='n'##############################################date=`date +'%FT%k:%M:%S+00:00'`freq=dailyprio=0.5reject='.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/groupif [[ ! -f $path ]]; then    touch $path    chown ${user}:${group} $pathfi# check for robots.txt defined Sitemap directive# if doesn't exist add one# https://support.google.com/webmasters/answer/183669if [ -f ${webroot}/robots.txt ]; thenSITEMAPCHECK=$(grep 'Sitemap:' ${webroot}/robots.txt)    if [ -z $SITEMAPCHECK ]; then    echo Sitemap: http://${url}/sitemap.xml >> ${webroot}/robots.txt    fifi##############################################echo  > $path# grab list of site urlslist=`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 arrayarray=($list)echo ------------------------------------------------------echo ${#array[@]} pages detected for $url echo ------------------------------------------------------# formatted properly according to# https://support.google.com/webmasters/answer/35738echo <?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\> > $pathecho '    ' >> $path;   for ((i=0;i<${#array[*]};i++)); doecho <url>    <loc>${array[$i]:0}</loc>    <lastmod>$date</lastmod>    <changefreq>$freq</changefreq>    <priority>$prio</priority></url> >> $path   doneecho  >> $pathecho </urlset> >> $path# notify Google# URL encode urls as per https://support.google.com/webmasters/answer/183669if [[ $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}fiecho ------------------------------------------------------exit 0
Shell script throws a not found error when run from a sh file. But if entered manually the commands work
bash;shell;shell script
Run the script either as:bash script.shor just:./script.shWhen bash is run using the name sh, it disables most of its extensions, such as the [[ testing operator.Since you have the #!/bin/bash shebang line, you don't need to specify the shell interpreter explicitly on the command line. Running the script as a command will use that line to find the shell.