【问题标题】:Translate Array with Array用数组转换数组
【发布时间】:2020-05-30 09:05:59
【问题描述】:

我寻找一种方法来将一个数组中的键与另一个数组进行转换。

tmp_title=$3
title=$(echo ${tmp_title,,} | sed -e 's/\s/-/g')
tags=(computer media state society)
de=(computer medien staat gesellschaft)
fr=(ordinateur journalisme politique société)
ru=(Компьютер СМИ штат общество)

      file="./content/de/blog/$(date +"%Y")/$(date +"%m")/$title.md"
      if test -n "$2"; then

        # check tag is in tags array
        if [[ ${tags[*]} =~ $2 ]]; then

          # check the folder structure is right
          if [[ -d ./content/de/blog/$(date +"%Y")/$(date +"%m") ]]; then

            # create the content and fill up the file
            echo "---" >> "$file"
            echo "title: \"$3\"" >> "$file"
            echo "date: $(date +\"%Y-%m-%d\")" >> "$file"
            echo "draft: false" >> "$file"
            echo "tags: \"$2\"" >> "$file"
            echo "shorttext:" >> "$file"
            echo "cover: \"$2\"" >> "$file"
            echo "lang: $1" >> "$file"
            echo "---" >> "$file"
          fi
        else
          echo "Enter a valid tag name ..."
        fi
      fi

我寻找一种方法来翻译语言数组值中的 "tags: \"$2\"" >> "$file"。当我将社会附加到脚本时,应该是标签:“gesellschaft”。

感谢您的帮助。

西尔维奥

【问题讨论】:

    标签: arrays bash shell arraylist indexof


    【解决方案1】:

    考虑这种方法

    case $LANG in
        *en*) tags=(computer   media       state     society     );;
        *de*) tags=(computer   medien      staat     gesellschaft);;
        *fr*) tags=(ordinateur journalisme politique société     );;
        *ru*) tags=(Компьютер  СМИ         штат      общество    );;
    esac
    

    【讨论】:

      【解决方案2】:

      我有它实现其他。我询问类别,然后转换为 $lang 值。

      #!/usr/bin/env bash
      
      # variables through user input
      lang=$1
      cover=$2
      tmp_title=$3
      
      # variables which we use in script
      # create a title with small letters and remove whitespace
      title=$(echo ${tmp_title,,} | sed -e 's/\s/-/g')
      
      # categories
      categories=(computer media repression society)
      
      # date variables
      date=$(date +"%Y-%m-%d")
      year=$(date +"%Y")
      month=$(date +"%m")
      
      # content variables
      content_dir="./content/$lang/blog/$year/$month"
      file="$content_dir/$title.md"
      
      # function
      function create_file()
      {
        {
          echo "---"
          echo "title: \"$tmp_title\""
          echo "date: $date"
          echo "draft: false"
          echo "tags: \"$tag\""
          echo "shorttext:"
          echo "cover: \"$cover\""
          echo "lang: $lang"
          echo "---"
        } >> "$file"
      }
      
      case $1 in
        de)
          if test -n "$lang"; then
            # check tag is in array
            if [[ ${categories[*]} =~ $cover ]]; then
      
              # translation tag > categories
              if [[ $cover =~ "computer" ]]; then
                tag="Computer"
      
              elif [[ $cover =~ "media" ]]; then
                tag="Medien"
      
              elif [[ $cover =~ "repression" ]]; then
                tag="Staat"
      
      
              elif [[ $cover =~ "society" ]]; then
                tag="Gesellschaft"
              fi
      
              # check the folder structure is right
              if [[ -d "$content_dir" ]]; then
      
                # create the content and fill up the file
                create_file
      
                if [[ -f "$file" ]]; then
                  subl "$file"
                fi
      
              else
      
                # create the folder of content
                mkdir -p "$content_dir"
      
                # create the content and fill up the file
                create_file
      
                if [[ -f "$file" ]]; then
                  subl "$file"
                fi
      
              fi
            else
              echo "Enter a valid tag name ..."
            fi
          fi
        ;;
      
        en)
          if test -n "$lang"; then
            # check tag is in array
            if [[ ${categories[*]} =~ $cover ]]; then
      
              # translation tag > categories
              if [[ $cover =~ "computer" ]]; then
                tag="Computer"
      
              elif [[ $cover =~ "media" ]]; then
                tag="Media"
      
              elif [[ $cover =~ "repression" ]]; then
                tag="State"
      
              elif [[ $cover =~ "society" ]]; then
                tag="Society"
              fi
      
              # check the folder structure is right
              if [[ -d "$content_dir" ]]; then
      
                # create the content and fill up the file
                create_file
      
                if [[ -f "$file" ]]; then
                  subl "$file"
                fi
      
              else
      
                # create the folder of content
                mkdir -p "$content_dir"
      
                # create the content and fill up the file
                create_file
      
                if [[ -f "$file" ]]; then
                  subl "$file"
                fi
      
              fi
            else
              echo "Enter a valid tag name ..."
            fi
          fi
        ;;
      
        info)
          echo "To work with this script you need append the follow stuff"
          echo "./bin/new.sh lang cover title"
          echo "./bin/news.sh en society 'This is a title'"
          echo "cover: computer, media, repression, society"
        ;;
      esac
      

      西尔维奥

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-30
        • 1970-01-01
        • 2015-05-23
        • 1970-01-01
        • 2023-03-09
        • 2018-10-13
        • 2011-08-13
        相关资源
        最近更新 更多