Перевести массив с массивом - PullRequest
0 голосов
/ 14 февраля 2020

Я ищу способ перевести ключ из одного массива в другой массив.

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".

Спасибо за помощь.

Сильвио

Ответы [ 2 ]

0 голосов
/ 17 февраля 2020

Я это понимаю, другие. Я прошу категории и затем перевожу на значение $ 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

Сильвио

0 голосов
/ 14 февраля 2020

Рассмотрим этот подход

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
...