From d96c5061d61f004680efcb56f418967457d228eb Mon Sep 17 00:00:00 2001 From: nemsia Date: Tue, 9 May 2017 22:44:48 +0200 Subject: [PATCH] [enh] add ynh_find_port helper --- scripts/_common.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index a7f8ad0..6a452df 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -102,4 +102,20 @@ EOF ynh_remove_app_dependencies () { dep_app=${app//_/-} # Replace all '_' by '-' ynh_package_autoremove ${dep_app}-ynh-deps # Remove the fake package and its dependencies if they not still used. -} \ No newline at end of file +} + +# Find a free port and return it +# +# example: port=$(ynh_find_port 8080) +# +# usage: ynh_find_port begin_port +# | arg: begin_port - port to start to search +ynh_find_port () { + port=$1 + test -n "$port" || ynh_die "The argument of ynh_find_port must be a valid port." + while netcat -z 127.0.0.1 $port # Check if the port is free + do + port=$((port+1)) # Else, pass to next port + done + echo $port +}