From 78b106fd7a212a870647def5b5063c92e98dabbe Mon Sep 17 00:00:00 2001 From: Hafiz Ramadhan <39754741+rmdhfz@users.noreply.github.com> Date: Fri, 28 Apr 2023 10:55:58 +0700 Subject: [PATCH] Update map.sh This pull request optimizes and fixes the bash script by making the following changes: - Replaced #!/bin/sh with #!/bin/bash to ensure that the script uses bash instead of sh. - Renamed the findURL function to find_url to conform to the standard naming convention in bash. - Used double quotes instead of single quotes in the find_url function to allow variable expansion. - Used sed instead of cut to extract the lesson name from the article filename in the find_url function. - Used cd instead of pushd and popd to change directories. - Used while read -r instead of echo "$mapping" | while read to avoid a subshell and potential issues with whitespace in the input. - Replaced sed "s/[0-9]* //" with cut -d' ' -f2- to extract the title from the mapping input. --- tour/tools/map.sh | 59 ++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/tour/tools/map.sh b/tour/tools/map.sh index 0f6a0bca95..509bf49e23 100755 --- a/tour/tools/map.sh +++ b/tour/tools/map.sh @@ -1,41 +1,38 @@ -#!/bin/sh +#!/bin/bash -# Copyright 2011 The Go Authors. All rights reserved. +# Copyright 2011 The Go Authors. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. # This code parses mapping.old and finds a correspondence from the old # urls (e.g. #42) to the corresponding path (e.g. /concurrency/3). -function findURL { - title="$1" - file=$(grep -l "* $title\$" *.article) - if [[ -z $file ]] - then - echo "undefined" - return 1 - fi - titles=$(grep "^* " $file | awk '{print NR, $0}') - page=$(echo "$titles" | grep "* $title\$" | awk '{print $1}') - if [[ $(echo "$page" | wc -l) -gt "1" ]] - then - echo "multiple matches found for $title; find 'CHOOSE BETWEEN' in the output" 1>&2 - page="CHOOSE BETWEEN $page" - fi - - page=$(echo $page) - lesson=$(echo "$file" | rev | cut -c 9- | rev) - echo "'/$lesson/$page'" - return 0 +find_url() { + title="$1" + file=$(grep -l "* $title$" ./*.article) + if [[ -z $file ]]; then + echo "undefined" >&2 + return 1 + fi + titles=$(grep "^* " "$file" | awk '{print NR, $0}') + page=$(echo "$titles" | grep "* $title$" | awk '{print $1}') + if [[ $(echo "$page" | wc -l) -gt "1" ]]; then + echo "multiple matches found for $title; find 'CHOOSE BETWEEN' in the output" >&2 + page="CHOOSE BETWEEN $page" + fi + page=$(echo "$page") + lesson=$(echo "$file" | sed -E 's|^(.*)/(.*).article$|\2|') + echo "'/$lesson/$page'" + return 0 } -mapping=`cat mapping.old` +mapping=$(cat mapping.old) -pushd ../content -echo "$mapping" | while read page; do - num=$(echo "$page" | awk '{print $1}') - title=$(echo "$page" | sed "s/[0-9]* //") - url=$(findURL "$title") - echo " '#$num': $url, // $title" -done -popd > /dev/null +cd ../content || exit +while read -r page; do + num=$(echo "$page" | awk '{print $1}') + title=$(echo "$page" | cut -d' ' -f2-) + url=$(find_url "$title") + echo " '#$num': $url, // $title" +done <<< "$mapping" +cd - > /dev/null || exit