質問

I am trying to optimize my shell script a bit and want to be able to display the contents of an array based on user input that is stored in a variable:

Here is an excerpt from my script

logArr( firstLogs  secondLogs  thirdLogs  fourthLogs)
firstLogs=( a  b  c  d  e )
secondLogs=( f  g  h  i  j )
thirdLogs=( k  l  m  n  o )
fourthLogs=( p  q  r  s  t )


echo "Please enter a top-level directory for the logger..."
count=1
for i in "${logArr[@]}"
do
    echo "$count: $i"
    count=`expr $count + 1`
done
read logDir
logDir=`expr $logDir - 1`
logDir=${logArr[$logDir]}

So at this point I have the user input set in the variable $logDir. Is there a way to use that to display the values of the respective array, i.e. if they chose secondLogs I want to then display the contents of the secondLogs array without having to use and if elif elif elif scenario if possible. I have tried some eval stuff but I cant seem to make it work.

Any help is appreciated.

EDIT: If my request is too vague let me know and I am happy to clarify anything. Thanks!

役に立ちましたか?

解決

logArr=(firstLogs secondLogs thirdLogs fourthLogs)
firstLogs=(a b c d e)
secondLogs=(f g h i j)
thirdLogs=(k l m n o)
fourthLogs=(p q r s t)
echo 'Please enter a top-level directory for the logger...'

select logDir in ${logArr[*]}
do
  break
done

set $logDir[*]
echo ${!1}

Output

Please enter a top-level directory for the logger...
1) firstLogs
2) secondLogs
3) thirdLogs
4) fourthLogs
#? 2
f g h i j
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top