Glam Prestige Journal

Bright entertainment trends with youth appeal.

I have tried to create a bash script which has a case menu, that calls a function when chosen:

#!/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"
}

But when choosing option one, two or three that calls the specified functions, i get the error: fileSplitter: command not found

2

1 Answer

Thank you, the problem was caused by the order of menu and functions. It works perfectly when stating the functons before the menu:

#!/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

Result:

./csv_helper.sh
1) 1: fileSplitter
2) 2: rowGrepper
3) 3: grepCounter?
4) 4: exit
Vælg en funktion: 1
fileSplitter function
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy