Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting specific values from an array in ksh
    text
    copied!<p>I have a customized .profile that I use in ksh and below is a function that I created to skip back and forth from directories with overly complicated or long names.</p> <p>As you can see, the pathnames are stored in an array (<code>BOOKMARKS[]</code>) to keep track of them and reference them at a later time. I want to be able to delete certain values from the array, using a case statement (or OPTARG if necessary) so that I can just type <code>bmk -d #</code> to remove the path at the associated index.</p> <p>I have fiddled around with <code>array +A and -A</code>, but it just wound up screwing up my array (what is left in the commented out code may not be pretty...I didn't proofread it). </p> <p>Any suggestions/tips on how to create that functionality? Thanks!</p> <pre><code># To bookmark the current directory you are in for easy navigation back and forth from multiple non-aliased directories # Use like 'bmk' (sets the current directory to a bookmark number) to go back to this directory, i.e. type 'bmk 3' (for the 3rd) # To find out what directories are linked to which numbers, type 'bmk -l' (lowercase L) # For every new directory bookmarked, the number will increase so the first time you run 'bmk' it will be 1 then 2,3,4...etc. for every consecutive run therea fter # TODO: finish -d (delete bookmark entry) function make_bookmark() { if [[ $# -eq 0 ]]; then BOOKMARKS[${COUNTER}]=${PWD} (( COUNTER=COUNTER+1 )) else case $1 in -l) NUM_OF_ELEMENTS=${#BOOKMARKS[*]} while [[ ${COUNTER} -lt ${NUM_OF_ELEMENTS} ]] do (( ACTUAL_NUM=i+1 )) echo ${ACTUAL_NUM}":"${BOOKMARKS[${i}]} (( COUNTER=COUNTER+1 )) done break ;; #-d) ACTUAL_NUM=$2 #(( REMOVE=${ACTUAL_NUM}-1 )) #echo "Removing path ${BOOKMARKS[${REMOVE}]} from 'bmk'..." #NUM_OF_ELEMENTS=${#BOOKMARKS[*]} #while [[ ${NUM_OF_ELEMENTS} -gt 0 ]] #do #if [[ ${NUM_OF_ELEMENTS} -ne ${ACTUAL_NUM} ]]; then # TEMP_ARR=$(echo "${BOOKMARKS[*]}") # (( NUM_OF_ELEMENTS=${NUM_OF_ELEMENTS}-1 )) #fi #echo $TEMP_ARR #done #break #for VALUE in ${TEMP_ARR} #do # set +A BOOKMARK ${TEMP_ARR} #done #echo ${BOOKMARK[*]} #break ;; *) (( INDEX=$1-1 )) cd ${BOOKMARKS[${INDEX}]} break ;; esac fi } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload