简体   繁体   中英

Bash Scripting in Linux Variable/Array Assignment

I'm trying to make my shell script similar to ordering at a fast food restaurant. However, I'm confused as to how to set arrays to determine the size of the drink the customer wishes to order. My sizes are as follows:

small=12oz
medium=16oz
large=20oz
xlarge =24oz

I have my shell script programmed to read the customers input for $size but I don't know how to translate the word "small" into an oz amount when customer declares their size. I as going to use the eval “$size="` option but what do I put so that it can vary? Will x be sufficient?

Thanks!

Try an associative lookup.

$: declare -A drink=(
    [small]=12oz
    [medium]=16oz
    [large]=20oz
   )
$: choice=medium
$: echo "${drink[$choice]}"
16oz
$: drink[xlg]=32oz
$: choice=xlg
$: echo "${drink[$choice]}"
32oz

Watch the syntax, though, it's kind of a pain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM