arrays - Translate Array with Array
问题描述
I search a way to translate a key from one array with other array.
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
I search a way to translate "tags: \"$2\"" >> "$file" in the language array value. When I append society to the script then should be tags: "gesellschaft".
Thank you for help.
Silvio
解决方案
我有它意识到其他。我询问类别,然后转换为 $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
西尔维奥
推荐阅读
- r - 检查数据框是否在 R 的循环中具有超过 2 个唯一值
- php - 旧内容的超链接缓存
- linux - psql 以前创建的函数在退出后消失了
- docker - 配置文件的变量如何被 docker-compose ENV 替换?
- reactjs - 在 POST 或 DELETE 到数据库之后,如何让 React 从 API 重新获取我的数据?
- linux - ldd 命令如何知道加载了哪个虚拟地址共享库?
- css - CSS 将 url() 用于在其他地方导入文件的资产
- linux - 从动态子目录获取 SOURCES
- python - Jupyter notebook 内核错误:PyInit_win32api
- angular - Angular RxJs 如何等待最终确定 observable?