![Bash 案例菜单不会调用函数:未找到命令](https://linux22.com/image/945679/Bash%20%E6%A1%88%E4%BE%8B%E8%8F%9C%E5%8D%95%E4%B8%8D%E4%BC%9A%E8%B0%83%E7%94%A8%E5%87%BD%E6%95%B0%EF%BC%9A%E6%9C%AA%E6%89%BE%E5%88%B0%E5%91%BD%E4%BB%A4.png)
我试图创建一个具有案例菜单的 bash 脚本,该脚本在选择时会调用一个函数:
#!/bin/bash
# Menu
PS3='Vælg en funktion: '
options=("1: fileSplitter" "2: rowGrepper" "3: grepCounter?" "4: exit")
select opt in "${options[@]}"
do
case $opt in
"1: fileSplitter") fileSplitter ;;
"2: rowGrepper") rowGrepper ;;
"3: grepCounter?") grepCounter ;;
"4: exit") break ;;
*) echo "$REPLY er ikke tilgængeligt.." ;;
esac
done
# Functions
function fileSplitter() {
echo "fileSplitter function"
}
function rowGrepper() {
echo "rowGrepper function"
}
function grepCounter() {
echo "grepCounter"
}
但是当选择调用指定函数的选项一、二或三时,我收到错误:fileSplitter:未找到命令
答案1
谢谢,问题是由菜单和功能的顺序引起的。在菜单之前说明功能时,它工作正常:
#!/bin/bash
#Functions:
function fileSplitter() {
echo "fileSplitter function"
}
function rowGrepper() {
echo "rowGrepper function"
}
function grepCounter() {
echo "grepCounter"
}
# Menu:
PS3='Vælg en funktion: '
options=("1: fileSplitter" "2: rowGrepper" "3: grepCounter?" "4: exit")
select opt in "${options[@]}"
do
case $opt in
"1: fileSplitter") fileSplitter ;;
"2: rowGrepper") rowGrepper ;;
"3: grepCounter?") grepCounter ;;
"4: exit") break ;;
*) echo "$REPLY er ikke tilgængeligt.." ;;
esac
done
结果:
./csv_helper.sh
1) 1: fileSplitter
2) 2: rowGrepper
3) 3: grepCounter?
4) 4: exit
Vælg en funktion: 1
fileSplitter function