#!/bin/sh # A dmenu-based file picker (prints selected file to stdout) # Requires: dmenu (or similar) # Usage: dmenu-filepicker [-m menu] [path] menu="dmenu -i -l 15" while getopts "m:" opt; do case "$opt" in m) menu="$OPTARG" ;; esac done shift `expr $OPTIND - 1` fullpath=$(realpath ${@:-"$(pwd)"}) while true; do if [ "$sel" = "." ]; then echo "$fullpath" | sed 's|/\.||' break elif [ -d "$fullpath" ] || [ -z "$fullpath" ]; then if [ -z "$fullpath" ]; then fp="/"; else fp="$fullpath"; fi sel=$(ls -a "$fp" | sed 's|.*/||' | $menu) if [ "$sel" = ".." ]; then fullpath=$(echo "$fullpath" | sed "s|/[^/]*$||") else fullpath=$(echo "$sel" | sed "s|^|$fullpath/|") fi else echo "$fullpath" break fi [ -z "$sel" ] && break done