Created
April 22, 2021 12:06
-
-
Save Hayao0819/707be0f5ec468fa6205bf1bf5371eb56 to your computer and use it in GitHub Desktop.
Linuxにインストールされてるアプリの一覧をJsonで出力するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu | |
AppDir="/usr/share/applications" | |
DesktopFileExt="desktop" | |
function getDesktopFile(){ | |
#grep -E "^${2}" "${1}" | cut -d "=" -f 2 | tr -d "\n" | |
_Result="$(crudini --get "${1}" "Desktop Entry" "${2}")" | |
_Result="$(echo ${_Result} | tr -d "\"")" | |
if echo "${_Result}" | grep -q "^[0-9]\+$" || [[ "${_Result}" = true ]] || [[ "${_Result}" = false ]]; then | |
echo -n "${_Result}" | |
else | |
echo -n "\"${_Result}\"" | |
fi | |
} | |
# Load AppList | |
while read -r app; do | |
AppList+=("${app}") | |
done < <(find "${AppDir}" -maxdepth 1 -mindepth 1 -name "*.${DesktopFileExt}" -printf "%f\n" 2> /dev/null | sed "s|.${DesktopFileExt}$||g" | sort) | |
JSON="{}" | |
Count=0 | |
for _App in "${AppList[@]}"; do | |
Count=$(( Count + 1 )) | |
echo "Loading ${_App} ... ${Count}/${#AppList[@]} $(awk "BEGIN { print ${Count} * 100 /${#AppList[@]}}")%" >&2 | |
_JsonName="$(echo -n "${_App}" | tr "." "_" | tr "-" "_")" | |
_DesktopFilePath="${AppDir}/${_App}.${DesktopFileExt}" | |
_setValueToJson(){ | |
JSON="$(echo "${JSON}" | jq -c ".${_JsonName}.${1} = $(getDesktopFile "${_DesktopFilePath}" "${1}")")" | |
} | |
JSON="$(echo "${JSON}" | jq -c ".${_JsonName} = {}")" | |
_setValueToJson "Name" | |
_setValueToJson "Exec" | |
_setValueToJson "iCON" | |
_setValueToJson "Type" | |
_setValueToJson "Comment" | |
done | |
echo "${JSON}" | jq |
colrichie
commented
May 17, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment