Last active
January 15, 2018 22:06
-
-
Save GarreauArthur/caf12287a4316c0d3526c855681ac6a9 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
if [ -z "$1" ] || [ -z "$2" ] | |
then | |
echo "Parameter 1 or 2 missing" | |
kill -INT $$ | |
fi | |
i=0 | |
array=() | |
for file in .* *; do # proper way to do it : https://unix.stackexchange.com/a/162589 | |
filename=${file##*/} | |
base=${filename%.*} | |
ext=".${filename##*.}" | |
l_ext=${#ext} | |
l=${#2} | |
search="" | |
if [[ ${2:-l_ext} == $ext ]]; then # if user type the extension | |
search=${filename: -l} | |
else | |
search=${base: -l} | |
fi | |
if [[ $search == $2 ]]; then | |
array[$i]=$filename; | |
i=$((i+1)) | |
fi | |
done | |
found=${#array[@]} | |
if [[ $found == "0" ]]; then | |
echo "No corresponding file found" | |
elif [[ $found == "1" ]]; then | |
$1 ${array[0]} | |
else | |
echo "too many files found :" | |
for a in ${array[*]}; do | |
echo "* ${a}"; | |
done | |
fi | |
# ${variable#pattern} Trim the shortest match from the beginning | |
# ${variable##pattern} Trim the longest match from the beginning | |
# ${variable%pattern} Trim the shortest match from the end | |
# ${variable%%pattern} Trim the shortest match from the end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let's try to build a script that allow autocomplete from the end of the word. For example, if you have a couple of files with the same beginning :
You can just type
./end-autocomplete.sh vim 1
, and the script will open2018-1
in vim.