当前位置:网站首页>Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 2 Validate input: letters and numbers only

Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 2 Validate input: letters and numbers only

2022-08-09 10:29:00 Mary Soda Meatloaf

validalphanum(){validchars="$(echo $1 | sed -e 's/[^[:alnum:]]//g')" #1if [ "$validchars" = "$1" ];thenreturn 0elsereturn 1fi}read -p "Enter input: " input #2if ! validalphanum "$input" ; thenecho "your input must consist only letters and numbers."exit 1elseecho "input is valid"fiexit 0

1.shell script sed tool

sed itself is also a pipe command and can parse standard input.And sed can also replace, delete, add, select specific rows and other functions of data.

 [[email protected] ~] sed [-nefr] [action] filename

Options and parameters:

  • -n: The general sed command will output all data to the screen.If this option is added, only the lines processed by the sed command will be output to the screen;
  • -e: Allows multiple sed command edits to be applied to the input data;
  • -f script filename: Read in sed operations from a sed script.Very similar to the -f option of the awk command;
  • -r: support extended regular expressions in sed;
  • -i: directly modify the file that reads the data with the modification result of sed, instead of outputting the action on the screen

Operation:n1,n2 function

  • a : Append, add one or more lines after the current line.
  • c : Line replacement, replace the original data line with the string following c.
  • i : Insert, insert one or more lines before the current line.
  • d: delete, delete the specified line;
  • P: print, output the specified line;
  • s: String substitution, replace one string with another.The format is "line range s/oldstring/newstring/g" (similar to the replacement format in Vim);

2. Variable keyboard read

$read [-pt] variable

Options and parameters:

-p: can be followed by prompt characters

-t: The number of seconds that can be followed by waiting.

Run result

原网站

版权声明
本文为[Mary Soda Meatloaf]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091026200275.html