array=('org.battery.plist' 'org.disk.plist' 'org.memory.plist');

echo "1) org.battery.plist"
echo "2) org.disk.plist"
echo "3) org.memory.plist"

echo "Enter selection(s) to load, separated by commas: "
read var

sudo launchctl load -w ${array[$var]}

Am I on the right track? I'm a little stuck. Can someone help?

If user inputs 1, 2, I expect the script to perform this below -

sudo launchctl load -w org.disk.plist
sudo launchctl load -w org.memory.plist
share|improve this question

2  
try select ... in ... statement in bash. type help select in the terminal for help. – kev May 1 at 23:51
feedback

3 Answers

up vote 1 down vote accepted

Try this,

IFS=","
for i in $var
do
    sudo launchctl load -w ${array[$i - 1]}
done

You will also need to check whether the input is out of array bounds and throw and error.

share|improve this answer
thanks dpp ...appreciate the complete code. – Jim May 2 at 0:11
feedback

There is a buildin in bash for such selects, surprisingly, called 'select':

select entry in ${array[@]}; 
do  
    sudo launchctl load -w $entry
done 

Try help select.

share|improve this answer
help select should work, as it's a shell built-in. man select will probably give you the manpage for the (unrelated) syscall. – ephemient May 2 at 0:31
@ephemient: Yes, thanks, concentration mistake – user unknown May 2 at 0:33
feedback

This is better:

array=('org.battery.plist' 'org.disk.plist' 'org.memory.plist');

for (( i=0;i<"${#array[@]}";i++ )) ; do
    let n=i+1
    printf '%d) %s\n' $n "${array[$i]}"
done

IFS=, read -r -p 'Enter selection(s) to load, separated by commas: ' -a selections

for selection in "${selections[@]}" ; do
    let selection=selection-1
    sudo launchctl load -w "${array[$selection]}"
done
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.