I have a free form string which I need to sanitize in bash in order to produce safe-and-nice filenames.
Example:
STAGE_NAME="Some usafe name 2/2#"
Expected sanitized result"
"some-unsafe-name-2-2"
Logic:
dash
(including spaces)Use of external tools like sed is allowed as long they are portable (not using options that are no available on BSD/OSX/...).
In bash command -d is to check if the directory exists or not. For example, I having a directory called. /home/sureshkumar/test/. The directory variable contains: "/home/sureshkumar/test/"
Show activity on this post. $@ is basically use for refers all the command-line arguments of shell-script. $1 , $2 , $3 refer to the first command-line argument, the second command-line argument, third argument. Follow this answer to receive notifications.
To sanitize a string input which you want to store to the database (for example a customer name) you need either to escape it or plainly remove any quotes (', ") from it. This effectively prevents classical SQL injection which can happen if you are assembling an SQL query from strings passed by the user.
You can use this pure bash function for this sanitization:
sanitize() {
local s="${1?need a string}" # receive input in first argument
s="${s//[^[:alnum:]]/-}" # replace all non-alnum characters to -
s="${s//+(-)/-}" # convert multiple - to single -
s="${s/#-}" # remove - from start
s="${s/%-}" # remove - from end
echo "${s,,}" # convert to lowercase
}
Then call it as:
sanitize "///Some usafe name 2/2##"
some-usafe-name-2-2
sanitize "Some usafe name 2/2#"
some-usafe-name-2-2
Just for an academic exercise here is an awk
one-liner doing the same:
awk -F '[^[:alnum:]]+' -v OFS=- '{$0=tolower($0); $1=$1; gsub(/^-|-$/, "")} 1'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With