In a bash script, how do I sanitize user input?

前端 未结 6 1089
抹茶落季
抹茶落季 2020-12-24 05:01

I\'m looking for the best way to take a simple input:

echo -n \"Enter a string here: \"
read -e STRING

and clean it up by removing non-alph

6条回答
  •  青春惊慌失措
    2020-12-24 05:43

    You could run it through perl.

    export CLEANSTRING=$(perl -e 'print join( q//, map { s/\\s+/_/g; lc } split /[^\\s\\w]+/, \$ENV{STRING} )')
    

    I'm using ksh-style subshell here, I'm not totally sure that it works in bash.

    That's the nice thing about shell, is that you can use perl, awk, sed, grep....

提交回复
热议问题