如何将环境变量放在数组中? [英] How to put environment variable in Array?
本文介绍了如何将环境变量放在数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DIR1,DIR2,....DIRN来自包含某些目录路径的环境变量. 例子
DIR1, DIR2, .... DIRN are coming from environment variables that contains some directory path. example
export DIR1="/users/abcd"
export DIR2="/users/abcd/xyz"
.
.
.
我怎么知道环境变量中有多少DIR_并将所有这些放在下面的数组中
How do i know that how many DIR_ is in environment variable and put all these in the following array
arr=(DIR1 DIR2 ... . . DIRN)
i=0
while [ $i -lt ${#arr[@]} ]
do
cd ${arr[$i]}
i=`expr $i + 1`
done
推荐答案
对于环境变量的值中可能包含换行符的情况,您可以使用此脚本,该脚本使用printenv
和awk
的gnu版本.
For the cases when environment variables may contain newlines in their values you can use this script that uses gnu versions of printenv
and awk
.
mapfile -t arr < <(printenv -0 | awk -v RS='\0' -F= '/^DIR/{print $1}')
然后将您的数组内容检查为:
Then check your array content as:
declare -p arr
这篇关于如何将环境变量放在数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文