1
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
1
  • 2
    try select ... in ... statement in bash. type help select in the terminal for help. Commented May 1, 2012 at 23:51

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

help select should work, as it's a shell built-in. man select will probably give you the manpage for the (unrelated) syscall.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.