Issue
How would I implement something similar to functional programming within bash?
I have a function:
requestPassword(){
passwordValidated=false
while [ "$passwordValidated" = "false" ];do
tput clear
[ -n "$infomsg" ] && echo -e "$infomsg" >&2
[ -n "$errmsg" ] && echo -e "$errmsg" >&2
read -s -p "some prompt:" pass >&2
errmsg=$()#do some validation
[ -n "$errmsg" ] && continue
passwordValidated=true
done
echo "$pass"
}
But I want to make it as generic as possible to work with different systems
Maybe I want to ask the password for an encrypted file Maybe I want to ask the password for a system user Maybe I want to use this function to request a new password from a user
The idea would be that I could pass this function a function that takes in a pass parameter and only outputs something if validation failed
Solution
You can pass a function name as an argument
requestPassword(){
local infomsg="$1"
local validator=${2:-the_default_validator_function}
# ...
local errmsg
errmsg=$( "$validator" "$pass" )
# ...
}
then
# a valid "foobar" password contains the substring "foo"
foobarPasswordValidator() { [[ $1 == *foo* ]]; }
pass=$(requestPassword "Enter your FooBar password" foobarPasswordValidator)
If you don't have a default validator, use :
or true
-- they accept arguments, ignore them(1) and return a "success" exit status.
(1) ignored after any parameter expansions have occurred
Answered By - glenn jackman Answer Checked By - Gilberto Lyons (WPSolving Admin)