课程: Learning Linux Shell Scripting

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Handling bad data

Handling bad data

- [Instructor] Your program should be prepared to handle bad data. Let's see a few ways to validate user input. Let's assume that we need two parameters from the user, their name, a string, and their age, an integer. So, we'll start off by saying touch user.sh, chmod 755 user.sh, and atom user.sh. Put the shebang in, usr/bin/env bash. And we'll begin with by having a global variable that says whether or not we consider the input valid, so we're gonna say VALID=0, meaning that it is not validated yet. Then we have a while, square brackets, space, $VALID, - eq, for equals, 0. So, while the VALID equals 0, we're gonna continue in this loop. We're gonna do a do, and a done, and then in the middle, we'll say read -p, and then we'll give the user a prompt. Remember, the -p says, put the prompt on the same line as the user input, put them both on the same line. So, we'll say, please enter your name and age, colon, space. Then we're gonna get our two input variables, the first one is NAME…

内容