Validating user input in shell script
When coding in a script, or even in a fully-featured software application, it's important to account for as many scenarios as possible.To limit the number of possible scenarios, it's a best practice to incorporate some kind of input validation into your code.
The language goes to all ends in its pursuit of this principle.In the Get-Event Date example, where I planned to write an error anyway, Validate Script is probably a cleaner alternative.But in the Archival Atrocity script, it would have been a much better user experience to create a directory if it didnâ€™t already exist.In particular, Validate Script lets you write a script block to test the conditions that the values must satisfy. The syntax of this thing looks like a sampler of Windows Power Shell enclosures.Windows Power Shell runs the validation script on the parameter values and, if the script returns $False, it throws a terminating error. Thereâ€™s a square bracket â€œ[â€œ or two â€œ]â€, a pair of parentheses â€œ( )â€ and even some curly braces â€œâ€. But once you get over the syntax, you realize that putting the parameter value validation into the parameter definition means that you donâ€™t need to test the parameter value in your script.