NoPaste

fm_slmenu.sh

von RobertDebiannutzer

Dein Code:
  1. #!/bin/bash
  2.  
  3. # License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  4. # This is free software; you are free to change and redistribute it.
  5. # There is NO WARRANTY, to the extent permitted by law.
  6.  
  7. args=($@)
  8.  
  9. if [[ -n "${args[@]}" ]]; then
  10.   argsl=$(printf '%s\n' "${args[@]}")
  11.   while read line; do
  12.     case "$line" in
  13.       -*)
  14.         plugin_path="${line#-}"
  15.         if [[ -f "$plugin_path" ]] && [[ -x "$plugin_path" ]]; then
  16.           insert_tmp+=("$(sed -n "3 {s/# //p}" "$plugin_path")")
  17.         else
  18.           exit 1
  19.         fi
  20.         ;;
  21.       *)
  22.         if [[ -d "$line" ]] && [[ -x "$line" ]]; then
  23.           workdir="$line"
  24.           export workdir
  25.         else
  26.           workdir="$HOME"
  27.         fi
  28.         ;;
  29.     esac
  30.   done <<< "$argsl"
  31. fi
  32.  
  33. insert_cmd=("$(printf '%s\n' "${insert_tmp[@]}" | sort -rg -ti -k1)")
  34.  
  35. if [[ -n "${insert_cmd[@]}" ]]; then
  36.   fm_file=$(< /opt/scripts/fm_slmenu.sh)
  37.   for x in "${!insert_cmd[@]}"; do
  38.     fm_file="$(sed -e "${insert_cmd[x]}" <<< "$fm_file")"
  39.   done
  40.   bash <(echo "$fm_file")
  41.   exit 0
  42. fi
  43.  
  44. apps_path="$HOME/.local/share/applications/"
  45.  
  46. [[ -z "$workdir" ]] && workdir="$HOME"
  47. cd "$workdir" || exit
  48.  
  49. slmenu_cmd () {
  50.   slmenu "$1" -t -i -l 61
  51. }
  52.  
  53. check_root () {
  54.   case "$(pwd)" in
  55.     '/')
  56.       pardir=""
  57.       ;;
  58.     *)
  59.       pardir="../\n"
  60.       ;;
  61.   esac
  62. }
  63.  
  64. input_short () {
  65.   check_root
  66.   short_ls () { ls -1 --group-directories-first -F; }
  67.   printf "%s\n$pardir%s\n" "$(pwd)" "$(short_ls)" | slmenu_cmd
  68. }
  69.  
  70. input_verbose () {
  71.   check_root
  72.   long_ls () { LC_ALL="C" ls -lAh --group-directories-first -F; }
  73.   printf "%s\n$pardir%s\n" "$(pwd)" "$(long_ls)" | slmenu_cmd -d
  74. }
  75.  
  76. type_cmd () {
  77.   file -b --mime-type -- "$list"
  78. }
  79.  
  80. tool_cmd () {
  81.   sed -n "/^${type//\//\\/}/ {s@^.*=@$apps_path@;s/\;.*//p;q}" -- \
  82.     "$HOME/.config/mimeapps.list"
  83. }
  84.  
  85. comppre_cmd () {
  86.   sed -n '/\[Desktop Entry\]/,/^Exec=/!d;s/^Exec=//p' -- "$tool"
  87. }
  88.  
  89. testterm_cmd () {
  90.   grep -q '^Terminal=true' -- "$tool"
  91. }
  92.  
  93. error_cmd () {
  94.   echo "$list" >> tmp
  95.   printf '%s' 'An error occurred. Please press Enter.' |
  96.   slmenu_cmd > /dev/null
  97. }
  98.  
  99. perm_cmd () {
  100.   printf '%s' "$list: Permission denied" | slmenu_cmd > /dev/null
  101. }
  102.  
  103. noapp_cmd () {
  104.   printf '%s' "There's no app specified for $type in mimeapps.list" |
  105.   slmenu_cmd > /dev/null
  106. }
  107.  
  108. exec_terminal () {
  109.   urxvt -e bash -c -- "$(comppre_cmd) -- $list" > /dev/null 2>&1
  110. }
  111.  
  112. exec_gui () {
  113.   exec $(comppre_cmd) "$list" > /dev/null 2>&1
  114. }
  115.  
  116. sed_cmd () {
  117.   sed -E 's/([,.:a-zA-Z0-9-]*[[:space:]]*){8}//'
  118. }
  119.  
  120. while true; do
  121.   list="$(input_short)"; [[ -z $list ]] && { clear && exit; }
  122.   case "$list" in *"*") list="${list%?}";; esac; [[ -z $list ]] && exit
  123.   if [[ -d "$list" ]]; then
  124.     if [[ -x "$list" ]]; then
  125.       cd -- "$list" || { error_cmd && continue; }
  126.     else
  127.       perm_cmd && continue
  128.     fi
  129.   elif [[ -f "$list" ]]; then
  130.     [[ ! -r "$list" ]] && { perm_cmd && continue; }
  131.     type="$(type_cmd)"; [[ -z $type ]] && { error_cmd && continue; }
  132.     tool="$(tool_cmd)"; [[ ! -r $tool ]] && { noapp_cmd; continue; }
  133.     if testterm_cmd; then
  134.       exec_terminal &
  135.     else
  136.       exec_gui &
  137.     fi
  138.     clear && exit
  139.   elif [[ $list == '//+' ]]; then
  140.     while true; do
  141.       input="$(input_verbose)"; [[ -z $input ]] && { clear && exit; }
  142.       case "$input" in
  143.         "../")
  144.           cd ../ || { error_cmd && continue; }
  145.           ;;
  146.         "//q")
  147.           continue 2
  148.           ;;
  149.         "//"*)
  150.           eval "${input#\/\/}" || { error_cmd && continue; }
  151.           continue
  152.           ;;
  153.         *)
  154.           list="$(sed_cmd <<< "$input")"; [[ -z $list ]] && { error_cmd && continue; }
  155.           case "$input" in
  156.             *"*")
  157.               list="${list%?}"; [[ -z $list ]] && { error_cmd && continue; }
  158.               ;;
  159.           esac
  160.           if [[ -d "$list" ]]; then
  161.             if [[ -x "$list" ]]; then
  162.               cd -- "$list" || { error_cmd && continue; }
  163.             else
  164.               perm_cmd && continue
  165.             fi
  166.           elif [[ -f "$list" ]]; then
  167.             if [[ -r "$list" ]]; then
  168.               type="$(type_cmd)"; [[ -z $type ]] && { error_cmd && continue; }
  169.               tool="$(tool_cmd)"; [[ ! -r $tool ]] && { noapp_cmd; continue; }
  170.               if testterm_cmd; then
  171.                 exec_terminal &
  172.               else
  173.                 exec_gui &
  174.               fi
  175.             else
  176.               perm_cmd && continue
  177.             fi
  178.           else
  179.             error_cmd && continue
  180.           fi
  181.           ;;
  182.       esac
  183.     done
  184.   else
  185.     case "$list" in
  186.       "//"*)
  187.         eval "${list#\/\/}" || { error_cmd && continue; }
  188.         continue
  189.         ;;
  190.       *)
  191.         error_cmd && continue
  192.         ;;
  193.     esac
  194.   fi
  195. done

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. Alternativ kannst du den gesamten Eintrag auch als Datei herunterladen.