我试图创建一个具有案例菜单的 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