Bash arrays index und item

kosovafan

Erfahrenes Mitglied
Hallo,

ich bin mir nicht sicher ob ich hier richtig bin, aber ich probiere es einfach mal. Ich habe
mir folgendes Script gebastelt das nichts anderes macht als für Hugo ein MD File im
entsprechenden Content / Language Ordner anzulegen.

Bash:
#!/usr/bin/env bash

# variables through user input
lang=$1
tags=$2
tmp_title=$3
cover=$4

# variables which we use in script
# create a title with small letters and remove whitespace
title=$(echo ${tmp_title,,} | sed -e 's/\s/-/g')

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

# language tag arrays
de=(computer medien staat gesellschaft)
en=(computer media state society)

# funtion
function create_file()
{
  {
    echo "---"
    echo "title: \"$tmp_title\""
    echo "date: $date"
    echo "draft: false"
    echo "tags: \"$tags\""
    echo "shorttext:"
    echo "cover: \"$cover\""
    echo "lang: $lang"
    echo "---"
  } >> "$file"
}

case $lang in
  de)
    if test -n "$lang"; then
      # check tag is in array
      if [[ ${de[*]} =~ $tags ]]; then

        # 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 [[ ${en[*]} =~ $tags ]]; then

        # 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
  ;;
esac

Das Script wird aufgerufen ./bin/new.sh de gesellschaft "Das ist der Title" cover.

Es geht mir um das Cover, was eigentlich nur aus den Array en=() kommen braucht. Es wird geprüft ob der tag ($2) im entsprechenden Array lang=() ist, mit einer Array Abfrage gibt es ein item + index. Wie kann man es schaffen den index des jeweiligen tag in cover einzutragen (create_file)? So müsste man Cover nicht eingeben! Ich habe im Internet gesucht, tausende von Bash Tutorials gelesen, aber ich werde irgendwie nicht fündig. Vielleicht hat hier jemand eine Idee?


Mfg & Danke
Silvio
 
Zuletzt bearbeitet:
Ich weiß nicht, ob ich die Frage richtig verstehe.
Aber vermutlich würde ich einen for loop über das Array laufen lassen und Vergleichen ob Tag und Wert im Array gleich sind. Dazu einen counter für den Index (Ich würde sowas ja mit python lösen, denn da gibt es enumerate und Template strings) und du bekommst den Index und das Item steht ja in $2 schon drin.
How to use arrays in bash script - LinuxConfig.org
 
Ich bin mir nicht ganz sicher, ob ich das richtig verstanden habe. Aber im Grunde suchst du nur nach dem Index eines Array-Elements?

Habe mit Dr. Google das hier gefunden:

https://stackoverflow.com/questions/15028567/get-the-index-of-a-value-in-a-bash-array
Die Top-Antwort schlägt das hier vor:
Bash:
#!/bin/bash

my_array=(red orange green)
value='green'

for i in "${!my_array[@]}"; do
   if [[ "${my_array[$i]}" = "${value}" ]]; then
       echo "${i}";
   fi
done

Also im Grunde genommen iterierst du einfach über alle Indizes und wartest den Treffer ab. Ist nicht schön und nicht effizient, sollte aber bei 5-10 Elementen gehen.

Gruß Technipion

EDIT: Da war @ikosaeder wohl 20s schneller als ich o_O
 
Hallo,

die Seiten kenne ich brachte aber nicht den Erfolg. Zumindest nicht das Ziel. Ich habe das jetzt anders gemacht. Ich frage nach den cover und von denen aus werden die Tags in der jeweiligen Sprache erstellt.

Bash:
#!/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

Mfg & Danke
Silvio
 

Neue Beiträge

Zurück