From the course: Learning Linux Shell Scripting

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

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…

Contents