mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Use getopts for string's helpers
This commit is contained in:
parent
f24b7a6fda
commit
c057d38fe1
1 changed files with 37 additions and 22 deletions
|
@ -1,59 +1,74 @@
|
||||||
# Generate a random string
|
# Generate a random string
|
||||||
#
|
#
|
||||||
# example: pwd=$(ynh_string_random 8)
|
# example: pwd=$(ynh_string_random --length=8)
|
||||||
#
|
#
|
||||||
# usage: ynh_string_random [length]
|
# usage: ynh_string_random [--length=string_length]
|
||||||
# | arg: length - the string length to generate (default: 24)
|
# | arg: -l, --length - the string length to generate (default: 24)
|
||||||
ynh_string_random() {
|
ynh_string_random() {
|
||||||
|
# Declare an array to define the options of this helper.
|
||||||
|
declare -Ar args_array=( [l]=length= )
|
||||||
|
local length
|
||||||
|
# Manage arguments with getopts
|
||||||
|
ynh_handle_getopts_args "$@"
|
||||||
|
length=${length:-24}
|
||||||
|
|
||||||
dd if=/dev/urandom bs=1 count=200 2> /dev/null \
|
dd if=/dev/urandom bs=1 count=200 2> /dev/null \
|
||||||
| tr -c -d 'A-Za-z0-9' \
|
| tr -c -d 'A-Za-z0-9' \
|
||||||
| sed -n 's/\(.\{'"${1:-24}"'\}\).*/\1/p'
|
| sed -n 's/\(.\{'"$length"'\}\).*/\1/p'
|
||||||
}
|
}
|
||||||
|
|
||||||
# Substitute/replace a string (or expression) by another in a file
|
# Substitute/replace a string (or expression) by another in a file
|
||||||
#
|
#
|
||||||
# usage: ynh_replace_string match_string replace_string target_file
|
# usage: ynh_replace_string match_string replace_string target_file
|
||||||
# | arg: match_string - String to be searched and replaced in the file
|
# | arg: -m, --match_string - String to be searched and replaced in the file
|
||||||
# | arg: replace_string - String that will replace matches
|
# | arg: -r, --replace_string - String that will replace matches
|
||||||
# | arg: target_file - File in which the string will be replaced.
|
# | arg: -f, --target_file - File in which the string will be replaced.
|
||||||
#
|
#
|
||||||
# As this helper is based on sed command, regular expressions and
|
# As this helper is based on sed command, regular expressions and
|
||||||
# references to sub-expressions can be used
|
# references to sub-expressions can be used
|
||||||
# (see sed manual page for more information)
|
# (see sed manual page for more information)
|
||||||
ynh_replace_string () {
|
ynh_replace_string () {
|
||||||
local delimit=@
|
# Declare an array to define the options of this helper.
|
||||||
local match_string=$1
|
declare -Ar args_array=( [m]=match_string= [r]=replace_string= [f]=target_file= )
|
||||||
local replace_string=$2
|
local match_string
|
||||||
local workfile=$3
|
local replace_string
|
||||||
|
local target_file
|
||||||
|
# Manage arguments with getopts
|
||||||
|
ynh_handle_getopts_args "$@"
|
||||||
|
|
||||||
|
local delimit=@
|
||||||
# Escape the delimiter if it's in the string.
|
# Escape the delimiter if it's in the string.
|
||||||
match_string=${match_string//${delimit}/"\\${delimit}"}
|
match_string=${match_string//${delimit}/"\\${delimit}"}
|
||||||
replace_string=${replace_string//${delimit}/"\\${delimit}"}
|
replace_string=${replace_string//${delimit}/"\\${delimit}"}
|
||||||
|
|
||||||
sudo sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$workfile"
|
sudo sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$target_file"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Substitute/replace a special string by another in a file
|
# Substitute/replace a special string by another in a file
|
||||||
#
|
#
|
||||||
# usage: ynh_replace_special_string match_string replace_string target_file
|
# usage: ynh_replace_special_string match_string replace_string target_file
|
||||||
# | arg: match_string - String to be searched and replaced in the file
|
# | arg: -m, --match_string - String to be searched and replaced in the file
|
||||||
# | arg: replace_string - String that will replace matches
|
# | arg: -r, --replace_string - String that will replace matches
|
||||||
# | arg: target_file - File in which the string will be replaced.
|
# | arg: -t, --target_file - File in which the string will be replaced.
|
||||||
#
|
#
|
||||||
# This helper will use ynh_replace_string, but as you can use special
|
# This helper will use ynh_replace_string, but as you can use special
|
||||||
# characters, you can't use some regular expressions and sub-expressions.
|
# characters, you can't use some regular expressions and sub-expressions.
|
||||||
ynh_replace_special_string () {
|
ynh_replace_special_string () {
|
||||||
local match_string=$1
|
# Declare an array to define the options of this helper.
|
||||||
local replace_string=$2
|
declare -Ar args_array=( [m]=match_string= [r]=replace_string= [f]=target_file= )
|
||||||
local workfile=$3
|
local match_string
|
||||||
|
local replace_string
|
||||||
|
local target_file
|
||||||
|
# Manage arguments with getopts
|
||||||
|
ynh_handle_getopts_args "$@"
|
||||||
|
|
||||||
# Escape any backslash to preserve them as simple backslash.
|
# Escape any backslash to preserve them as simple backslash.
|
||||||
match_string=${match_string//\\/"\\\\"}
|
match_string=${match_string//\\/"\\\\"}
|
||||||
replace_string=${replace_string//\\/"\\\\"}
|
replace_string=${replace_string//\\/"\\\\"}
|
||||||
|
|
||||||
# Escape the & character, who has a special function in sed.
|
# Escape the & character, who has a special function in sed.
|
||||||
match_string=${match_string//&/"\&"}
|
match_string=${match_string//&/"\&"}
|
||||||
replace_string=${replace_string//&/"\&"}
|
replace_string=${replace_string//&/"\&"}
|
||||||
|
|
||||||
ynh_replace_string "$match_string" "$replace_string" "$workfile"
|
ynh_replace_string "$match_string" "$replace_string" "$target_file"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue