Apache Nutch is an extensible and scalable web crawler
NUTCH-3199 Yetus: harden bin/crawl and bin/nutch for remaining shellcheck findings (#980)
Lewis John McGibbney495ff8070a84
diff --git a/src/bin/crawl b/src/bin/crawlindex 07ac7789d..7f60ab120 100755--- a/src/bin/crawl+++ b/src/bin/crawl@@ -54,25 +54,28 @@## @audience private## @stability stablefunction __to_seconds() {- NUMBER=$(echo $1 | tr -dc '0-9')- MODIFIER=$(echo $1 | tr -dc '[^s|h|m|d]]')+ NUMBER=$(printf '%s' "$1" | tr -dc '0-9')+ # Same characters the previous tr expression kept: lowercase suffixes plus+ # the literal brackets, caret, and pipe.+ MODIFIER=$(printf '%s' "$1" | tr -dc '[]^dhms|')case $MODIFIER inm|M)- SECONDS=`expr $NUMBER \* 60`+ SECONDS=$((10#$NUMBER * 60));;h|H)- SECONDS=`expr $NUMBER \* 120`+ # h means 120 seconds here, not 3600. A follow-up issue tracks the correction.+ SECONDS=$((10#$NUMBER * 120));;d|D)- SECONDS=`expr $NUMBER \* 86400`+ SECONDS=$((10#$NUMBER * 86400));;s|S|*)SECONDS=$NUMBER;;esac- echo $SECONDS+ echo "$SECONDS"}## @audience private@@ -228,8 +231,8 @@ if [ "$WAIT" != "-1" ]; thenecho "Time to wait (--wait) = $WAIT sec."fi-bin="`dirname "$0"`"-bin="`cd "$bin"; pwd`"+bin="$(dirname "$0")"+bin="$(cd "$bin" && pwd)" || exit# determines whether mode based on presence of job filemode=local@@ -248,11 +251,11 @@ fi# note that some of the options listed here could be set in the# corresponding hadoop site xml param file-commonOptions=("${HADOOP_PROPERTIES[@]}" -Dmapreduce.job.reduces=$NUM_TASKS -Dmapreduce.reduce.speculative=false -Dmapreduce.map.speculative=false -Dmapreduce.map.output.compress=true)+commonOptions=("${HADOOP_PROPERTIES[@]}" -Dmapreduce.job.reduces="$NUM_TASKS" -Dmapreduce.reduce.speculative=false -Dmapreduce.map.speculative=false -Dmapreduce.map.output.compress=true)# check that hadoop can be found on the path-if [ $mode = "distributed" ]; then- if [ $(which hadoop | wc -l ) -eq 0 ]; then+if [ "$mode" = "distributed" ]; then+ if [ "$(which hadoop | wc -l)" -eq 0 ]; thenecho "Can't find Hadoop executable. Add HADOOP_COMMON_HOME/bin to the path or run in local mode."# POSIX: 127 = utility not found (hadoop not on PATH)exit 127;@@ -301,15 +304,15 @@ function __update_hostdb {}# initial injection-if [[ ! -z $SEEDDIR ]]; then+if [[ -n "$SEEDDIR" ]]; thenecho "Injecting seed URLs"__bin_nutch inject "${commonOptions[@]}" "$CRAWL_PATH"/crawldb "$SEEDDIR"fi# sitemap processing based on sitemap definition file(s)-if [[ ! -z $SITEMAPDIR ]]; then+if [[ -n "$SITEMAPDIR" ]]; thenecho "Processing sitemaps defined in $SITEMAPDIR"- __bin_nutch sitemap "${commonOptions[@]}" "$CRAWL_PATH/crawldb" -sitemapUrls "$SITEMAPDIR" -threads $NUM_THREADS+ __bin_nutch sitemap "${commonOptions[@]}" "$CRAWL_PATH/crawldb" -sitemapUrls "$SITEMAPDIR" -threads "$NUM_THREADS"fi# main loop : rounds of generate - fetch - parse - update@@ -320,14 +323,14 @@ dobreakfi- if [ $LIMIT -ne -1 ]; then- if [ $a -gt $LIMIT ]; then- echo `date` ": Finished loop with $LIMIT iterations"+ if [ "$LIMIT" -ne -1 ]; then+ if [ "$a" -gt "$LIMIT" ]; then+ echo "$(date) : Finished loop with $LIMIT iterations"breakfi- echo `date` ": Iteration $a of $LIMIT"+ echo "$(date) : Iteration $a of $LIMIT"else- echo `date` ": Iteration $a"+ echo "$(date) : Iteration $a"fiif [[ "$SITEMAPS_FROM_HOSTDB_FREQUENCY" = "always" || ("$SITEMAPS_FROM_HOSTDB_FREQUENCY" = "once" && $a -eq 1) ]]; then@@ -337,19 +340,19 @@ do# sitemap processing based on HostDBif __directory_exists "$CRAWL_PATH"/hostdb; thenecho "Processing sitemaps based on hosts in HostDB"- __bin_nutch sitemap "${commonOptions[@]}" "$CRAWL_PATH"/crawldb -hostdb "$CRAWL_PATH"/hostdb -threads $NUM_THREADS+ __bin_nutch sitemap "${commonOptions[@]}" "$CRAWL_PATH"/crawldb -hostdb "$CRAWL_PATH"/hostdb -threads "$NUM_THREADS"fifiecho "Generating a new segment"if [[ "$HOSTDBGENERATE" == "true" ]] && __directory_exists "$CRAWL_PATH"/hostdb; then- generate_args=("${commonOptions[@]}" "$CRAWL_PATH"/crawldb "$CRAWL_PATH"/segments -topN $SIZE_FETCHLIST -numFetchers $NUM_FETCHERS -noFilter -hostdb "$CRAWL_PATH"/hostdb)+ generate_args=("${commonOptions[@]}" "$CRAWL_PATH"/crawldb "$CRAWL_PATH"/segments -topN "$SIZE_FETCHLIST" -numFetchers "$NUM_FETCHERS" -noFilter -hostdb "$CRAWL_PATH"/hostdb)else- generate_args=("${commonOptions[@]}" "$CRAWL_PATH"/crawldb "$CRAWL_PATH"/segments -topN $SIZE_FETCHLIST -numFetchers $NUM_FETCHERS -noFilter)+ generate_args=("${commonOptions[@]}" "$CRAWL_PATH"/crawldb "$CRAWL_PATH"/segments -topN "$SIZE_FETCHLIST" -numFetchers "$NUM_FETCHERS" -noFilter)fiecho "$bin/nutch generate" "${generate_args[@]}"- $bin/nutch generate "${generate_args[@]}"+ "$bin/nutch" generate "${generate_args[@]}"RETCODE=$?if [ $RETCODE -eq 0 ]; then: # ok: no error@@ -358,7 +361,7 @@ doif [ "$WAIT" -ne -1 ]; thenecho "Waiting for $WAIT sec. ..."- sleep $WAIT+ sleep "$WAIT"continueelseecho "Escaping loop: no more URLs to fetch now"@@ -375,28 +378,31 @@ do# call hadoop in distributed mode# or use ls- if [ $mode = "local" ]; then- SEGMENT=`ls "$CRAWL_PATH"/segments/ | sort -n | tail -n 1`+ if [ "$mode" = "local" ]; then+ # Numeric segment directory names. Keep ls|sort -n, which selects the same newest segment.+ # shellcheck disable=SC2012+ SEGMENT=$(ls "$CRAWL_PATH"/segments/ | sort -n | tail -n 1)else- SEGMENT=`hadoop fs -ls "$CRAWL_PATH"/segments/ | grep segments | sed -e "s/\//\\n/g" | egrep 20[0-9]+ | sort -n | tail -n 1`+ SEGMENT=$(hadoop fs -ls "$CRAWL_PATH"/segments/ | grep segments | sed -e "s/\//\\n/g" | grep -E '20[0-9]+' | sort -n | tail -n 1)fiecho "Operating on segment : $SEGMENT"# fetching the segmentecho "Fetching : $SEGMENT"- __bin_nutch fetch "${commonOptions[@]}" -D fetcher.timelimit.mins=$TIME_LIMIT_FETCH "$CRAWL_PATH"/segments/$SEGMENT -threads $NUM_THREADS+ __bin_nutch fetch "${commonOptions[@]}" -D "fetcher.timelimit.mins=$TIME_LIMIT_FETCH" "$CRAWL_PATH/segments/$SEGMENT" -threads "$NUM_THREADS"# parsing the segmentecho "Parsing : $SEGMENT"# enable the skipping of records for the parsing so that a dodgy document# so that it does not fail the full task- skipRecordsOptions="-D mapreduce.task.skip.start.attempts=2 -D mapreduce.map.skip.maxrecords=1"- __bin_nutch parse "${commonOptions[@]}" $skipRecordsOptions "$CRAWL_PATH"/segments/$SEGMENT+ # Four separate arguments, matching the historical unquoted expansion.+ skipRecordsOptions=(-D mapreduce.task.skip.start.attempts=2 -D mapreduce.map.skip.maxrecords=1)+ __bin_nutch parse "${commonOptions[@]}" "${skipRecordsOptions[@]}" "$CRAWL_PATH/segments/$SEGMENT"# updatedb with this segmentecho "CrawlDB update"- __bin_nutch updatedb "${commonOptions[@]}" "$CRAWL_PATH"/crawldb "$CRAWL_PATH"/segments/$SEGMENT+ __bin_nutch updatedb "${commonOptions[@]}" "$CRAWL_PATH"/crawldb "$CRAWL_PATH/segments/$SEGMENT"echo "HostDB update"if $HOSTDBUPDATE; then@@ -410,14 +416,14 @@ do# (both invertlinks and index accept multiple segments as input)# The latter is more efficient but the index is then updated later.echo "Link inversion"- __bin_nutch invertlinks "${commonOptions[@]}" "$CRAWL_PATH"/linkdb "$CRAWL_PATH"/segments/$SEGMENT -noNormalize -nofilter+ __bin_nutch invertlinks "${commonOptions[@]}" "$CRAWL_PATH"/linkdb "$CRAWL_PATH/segments/$SEGMENT" -noNormalize -nofilterecho "Dedup on crawldb"__bin_nutch dedup "${commonOptions[@]}" "$CRAWL_PATH"/crawldb -group "$DEDUP_GROUP"if $INDEXFLAG; thenecho "Indexing $SEGMENT to index"- __bin_nutch index "${commonOptions[@]}" "$CRAWL_PATH"/crawldb -linkdb "$CRAWL_PATH"/linkdb "$CRAWL_PATH"/segments/$SEGMENT -deleteGone+ __bin_nutch index "${commonOptions[@]}" "$CRAWL_PATH"/crawldb -linkdb "$CRAWL_PATH"/linkdb "$CRAWL_PATH/segments/$SEGMENT" -deleteGoneelseecho "Skipping indexing ..."fidiff --git a/src/bin/nutch b/src/bin/nutchindex 63dcc2cb5..77b14b362 100755--- a/src/bin/nutch+++ b/src/bin/nutch@@ -43,19 +43,19 @@# https://hadoop.apache.org/docs/stable3/hadoop-yarn/hadoop-yarn-common/yarn-default.xml#cygwin=false-case "`uname`" in+case "$(uname)" inCYGWIN*) cygwin=true;;esac# resolve links - $0 may be a softlinkTHIS="$0"while [ -h "$THIS" ]; do- ls=`ls -ld "$THIS"`- link=`expr "$ls" : '.*-> \(.*\)$'`+ ls=$(ls -ld "$THIS")+ link=$(expr "$ls" : '.*-> \(.*\)$')if expr "$link" : '.*/.*' > /dev/null; thenTHIS="$link"else- THIS=`dirname "$THIS"`/"$link"+ THIS=$(dirname "$THIS")/"$link"fidone@@ -131,8 +131,8 @@ COMMAND=$1shift# some directories-THIS_DIR="`dirname "$THIS"`"-NUTCH_HOME="`cd "$THIS_DIR/.." ; pwd`"+THIS_DIR="$(dirname "$THIS")"+NUTCH_HOME="$(cd "$THIS_DIR/.." && pwd)" || exit# some Java parametersif [ "$NUTCH_JAVA_HOME" != "" ]; then@@ -191,14 +191,14 @@ fi# cygwin path translationif $cygwin; then- CLASSPATH="`cygpath -p -w "$CLASSPATH"`"+ CLASSPATH="$(cygpath -p -w "$CLASSPATH")"fi# setup 'java.library.path' for native-hadoop code if necessary# used only in local modeJAVA_LIBRARY_PATH=''if [ -d "${NUTCH_HOME}/lib/native" ]; then- JAVA_PLATFORM=`"${JAVA}" -classpath "$CLASSPATH" org.apache.hadoop.util.PlatformName | sed -e 's/ /_/g' -e 's/\r//g'`+ JAVA_PLATFORM=$("${JAVA}" -classpath "$CLASSPATH" org.apache.hadoop.util.PlatformName | sed -e 's/ /_/g' -e 's/\r//g')if [ -d "${NUTCH_HOME}/lib/native/${JAVA_PLATFORM}" ]; thenif [ "x$JAVA_LIBRARY_PATH" != "x" ]; then@@ -209,8 +209,8 @@ if [ -d "${NUTCH_HOME}/lib/native" ]; thenfifi-if [ $cygwin = true -a "X${JAVA_LIBRARY_PATH}" != "X" ]; then- JAVA_LIBRARY_PATH="`cygpath -p -w "$JAVA_LIBRARY_PATH"`"+if [ "$cygwin" = true ] && [ "X${JAVA_LIBRARY_PATH}" != "X" ]; then+ JAVA_LIBRARY_PATH="$(cygpath -p -w "$JAVA_LIBRARY_PATH")"fi# restore ordinary behaviour@@ -226,10 +226,13 @@ fi#Fix log path under cygwinif $cygwin; then- NUTCH_LOG_DIR="`cygpath -p -w "$NUTCH_LOG_DIR"`"+ NUTCH_LOG_DIR="$(cygpath -p -w "$NUTCH_LOG_DIR")"fi-NUTCH_OPTS=($NUTCH_OPTS -Dhadoop.log.dir="$NUTCH_LOG_DIR")+# NUTCH_OPTS is documented as whitespace-separated Java options. Split on+# whitespace without pathname expansion so metacharacters stay literal.+read -r -a nutch_user_opts <<< "${NUTCH_OPTS-}"+NUTCH_OPTS=("${nutch_user_opts[@]}" -Dhadoop.log.dir="$NUTCH_LOG_DIR")NUTCH_OPTS=("${NUTCH_OPTS[@]}" -Dhadoop.log.file="$NUTCH_LOGFILE")if [ "x$JAVA_LIBRARY_PATH" != "x" ]; then@@ -332,10 +335,10 @@ fiEXEC_CALL=(hadoop jar "$NUTCH_JOB")if $local; then- EXEC_CALL=("$JAVA" $JAVA_HEAP_MAX "${NUTCH_OPTS[@]}" -classpath "$CLASSPATH")+ EXEC_CALL=("$JAVA" "$JAVA_HEAP_MAX" "${NUTCH_OPTS[@]}" -classpath "$CLASSPATH")else# check that hadoop can be found on the path- if [ $(which hadoop | wc -l ) -eq 0 ]; then+ if [ "$(which hadoop | wc -l)" -eq 0 ]; thenecho "Can't find Hadoop executable. Add HADOOP_COMMON_HOME/bin to the path or run in local mode."# POSIX: 127 = utility not found (hadoop not on PATH)exit 127;@@ -343,5 +346,5 @@ elsefi# run it-exec "${EXEC_CALL[@]}" $CLASS "$@"+exec "${EXEC_CALL[@]}" "$CLASS" "$@"