gpt4 book ai didi

arrays - 从 ksh 数组中删除特定值

转载 作者:行者123 更新时间:2023-12-02 22:13:16 26 4
gpt4 key购买 nike

我有一个在 ksh 中使用的自定义 .profile,下面是我创建的一个函数,用于从名称过于复杂或过长的目录中来回跳转。

如您所见,路径名存储在数组 (BOOKMARKS[]) 中,以跟踪它们并在以后引用它们。我希望能够使用 case 语句(或必要时使用 OPTARG)从数组中删除某些值,以便我只需键入 bmk -d # 即可删除关联索引处的路径。

我摆弄了array +A和-A,但它最终搞砸了我的数组(注释掉的代码中剩下的内容可能不太漂亮......我没有'不校对它)。

关于如何创建该功能有什么建议/提示吗?谢谢!

# 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
}

最佳答案

Korn shell(以及 Bash 和其他)中的数组是稀疏的,因此如果您使用 unset 删除数组的成员,您将无法将数组的大小用作最后一个成员的索引和其他限制。

这里有一些有用的片段(第二个 for 循环是您可以立即使用的):

array=(1 2 3)
unset array[2]
echo ${array[2]} # null
indices=(${!array[@]}) # create an array of the indices of "array"
size=${#indices[@]} # the size of "array" is the number of indices into it
size=${#array[@]} # same
echo ${array[@]: -1} # you can use slices to get array elements, -1 is the last one, etc.
for element in ${array[@]}; do # iterate over the array without an index

for index in ${indices[@]} # iterate over the array WITH an index
do
echo "Index: ${index}, Element: ${array[index]}"
done

for index in ${!array[@]} # iterate over the array WITH an index, directly

最后一个可以消除对计数器的需要。

这里有一些更方便的技巧:

array+=("new element")    # append a new element without referring to an index
((counter++)) # shorter than ((counter=counter+1)) or ((counter+=1))
if [[ $var == 3 ]] # you can use the more "natural" comparison operators inside double square brackets
while [[ $var < 11 ]] # another example
echo ${array[${index}-1] # math inside an array subscript

这一切都假设 ksh93,有些功能在早期版本中可能无法工作。

关于arrays - 从 ksh 数组中删除特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2106011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com