Last active
June 8, 2017 06:47
-
-
Save peshoicov/b64ed4f0ee6f9274af2b3ef295aa85f8 to your computer and use it in GitHub Desktop.
Shell script for quick navigation upwards in directories :
This file contains hidden or 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 | |
# .. | |
# Installation guide: | |
# 1. Paste this code in a file. Let say - in "/home/{user}/bin" and name it - for example - "goto" | |
# 2. In .bash_alias add an alias like so: "alias gt=". /home/{user}/bin/goto". NOTE the dot and the interval after it! | |
# 3. Make sure you've "sourced" the .bash_alias file. (cd ~; source .bash_alias) | |
# 4. Try "gt" anywhere, with or without parameters | |
# .. | |
# Or - to be closer with Shell's naming convention - name the file "goupto" and the alias "alias gut=". /home/{user}/bin/goupto". | |
# First - check if user specified one parameter .. | |
if (( $# != 1)); then | |
desiredDir="" | |
else | |
desiredDir=$1 | |
fi | |
# echo "Desired directory is: $desiredDir" | |
# Split the full directory into array .. | |
IFS='/' read -r -a foldersArray <<< "$PWD" | |
# Define the variable for all directories .. | |
allDirectories=() | |
# All real directories - in array .. | |
index=0 | |
oneFolder="" | |
for folder in "${foldersArray[@]}" | |
do | |
if [ "$folder" != "" ]; then | |
oneFolder="$oneFolder/$folder" | |
# Check if supplied parameter is in folder .. | |
if [[ $oneFolder == *$desiredDir* ]]; then | |
allDirectories[$index]=$oneFolder | |
# Increment the index .. | |
index=$((index+1)) | |
fi | |
fi | |
done | |
# Check if we have found any folder at all .. | |
if [ "$index" -gt 0 ]; then | |
# Define some colors we'll use .. | |
RED='\033[0;31m' | |
GREEN='\033[1;32m' | |
YELLOW='\033[1;33m' | |
DARKGREEN='\033[1;32m' | |
CIAN='\033[1;36m' | |
NOCOLOR='\033[0m' | |
if [ "$index" -eq 1 ]; then | |
# if only option is PWD - don't navigate .. | |
if [ "$PWD" == "${allDirectories[0]}" ]; then | |
printf "${GREEN}Staying${NOCOLOR} in $PWD\n" | |
echo "Bye for now" | |
else | |
printf "${CIAN}Navigating${NOCOLOR} to ${allDirectories[0]}\n" | |
cd ${allDirectories[0]} | |
fi | |
else | |
# Output full directories .. | |
echo "Choose:" | |
ii=0 | |
numberOfChoice="" | |
for (( jj=$index-1; jj >= 0; jj-- )) | |
do | |
ii=$((ii+1)) | |
# In case we have 10+ results - show leading empty space .. | |
if [ "$index" -gt 9 ] && [ $ii -lt 10 ]; then | |
numberOfChoice=" $ii" | |
else | |
numberOfChoice="$ii" | |
fi | |
# Show the choice .. | |
if [ "$desiredDir" == "" ]; then | |
echo "$numberOfChoice: ${allDirectories[$jj]}" | |
else | |
printf "$numberOfChoice: ${allDirectories[$jj]//$desiredDir/${YELLOW}${desiredDir}${NOCOLOR}}\n" | |
fi | |
done | |
# Wait for user to choose .. | |
printf ">" | |
while : ; do | |
read choice | |
# check if user has entered an integer .. | |
if [ $choice -eq $choice 2>/dev/null ]; then | |
break | |
else | |
printf "You must type a ${YELLOW}number${NOCOLOR}!\n" | |
printf ">" | |
fi | |
done | |
# If user entered valid choice - navigate to it .. | |
if [ "$choice" -gt 0 ] && [ "$choice" -le "$index" ]; then | |
indexOfChosen=$((index-choice)) | |
folderToNavigate=${allDirectories[$indexOfChosen]} | |
# if chosen option is PWD - don't navigate .. | |
if [ "$PWD" == "$folderToNavigate" ]; then | |
printf "${GREEN}Staying${NOCOLOR} in $PWD\n" | |
echo "Bye for now" | |
else | |
printf "${CIAN}Navigating${NOCOLOR} to ${folderToNavigate}\n" | |
cd ${folderToNavigate} | |
fi | |
else | |
printf "${GREEN}Staying${NOCOLOR} in $PWD\n" | |
echo "Bye for now" | |
fi | |
fi | |
else | |
printf "${CIAN}No such folder${NOCOLOR}\n" | |
# exit 1 | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment