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]}

我在正确的轨道上吗?我有点卡住了。有人可以帮忙吗?

如果用户输入 1、2,我希望脚本在下面执行此操作 -

sudo launchctl load -w org.disk.plist
sudo launchctl load -w org.memory.plist
4

3 回答 3

1

试试这个,

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

您还需要检查输入是否超出数组范围以及抛出和错误。

于 2012-05-02T00:00:58.487 回答
1

令人惊讶的是,bash 中有一个用于此类选择的内置函数,称为“选择”:

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

试试help select

于 2012-05-02T00:04:54.360 回答
0

这个更好:

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
于 2012-05-02T11:28:21.650 回答