Reputation: 161
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
Upvotes: 1
Views: 499
Reputation: 26086
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
Upvotes: 0
Reputation: 36229
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
.
Upvotes: 1
Reputation: 1758
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.
Upvotes: 1