Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sanitize a string in bash?

Tags:

bash

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:

  • lowercase chars
  • replace all unsupported or unsafe chars with dash (including spaces)
  • remove duplicated dashes
  • remove any dashes from prefix or suffix

Use of external tools like sed is allowed as long they are portable (not using options that are no available on BSD/OSX/...).

like image 798
sorin Avatar asked Jun 28 '17 19:06

sorin


People also ask

What is %d in bash?

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/"

What is #@ in bash?

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.

How do you disinfect input in python?

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.


1 Answers

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'
like image 194
anubhava Avatar answered Oct 11 '22 19:10

anubhava