0

我是 bash 脚本的新手,无法弄清楚为什么这段代码不起作用(是的,我已经用谷歌搜索了)。

这是我的代码:

if [ $usertype = normal ]
then
    commands[0]="hi" ; descriptions[0]="get greeted"
    commands[1]="test" ; descriptions[1] = "test"
elif [ $usertype = hacker ]
    commands[0]="hi" ; descriptions[0]="get greeted"
    commands[1]="test" ; descriptions[1] = "test"
fi

alias fhelp='
for ((i=0; i<=${commands[@]}; i++))
do
    printf '%s %s\n' "${commands[i]}" "${descriptions[i]}"
done'

有任何想法吗?

提前致谢。

4

2 回答 2

1

You can't use single quotes inside single quotes. Do this, it treats "'" as a string of a single quote and concatenate them.

alias fhelp='
for ((i=0; i<=${commands[@]}; i++))
do
  printf '"'"'%s %s\n'"'"' "${commands[i]}" "${descriptions[i]}"
done'

And use ${#commands[@]} to get the array length.

于 2013-05-13T01:56:06.047 回答
1
elif [ $usertype = hacker ]
# missing then!
commands[0]="hi" ; descriptions[0]="get greeted"
于 2013-05-13T01:57:38.443 回答