![]() The above examples show some good examples of using integer based operators. ![]() If it is not 1 or 2 it must be much larger so at this point it will give up checking and say “There are too many Benjamins”. If it is than we will execute the second if statement and see if it is either equal to 1, and if not equal to exactly 1 if it is equal to exactly 2. Our if statement will check if the value of the value variable is -ge greater than or equal to 1. First we execute the grep and send its value to the value variable. Let us break down how the above statements work together. ![]() value=$( grep -ic "benjamin" /etc/passwd ) Nested if statementsĪ nested if statement is where you have an if statement inside of an existing if statement. This is using the elif or else if statement. If the value is not specifically 1 I then check if the value is greater than 1, if it isn't 1 or greater then 1 I simply tell you that I didn't find any Benjamins. The order of this if statement is extremely important, you will notice that I first check if the value is specifically 1. I can also use the elif statement to perform an additional if statement if the first one wasn't found to be true. While it would be easy enough to simply add an else to either the less than or greater than examples to handle conditions where I found more or less “Benjamins” than the if statement is looking for. ![]() Second we will use the less than operator. In the previous two examples you can see the use of the -eq equals operator, in this example I am going to show the -gt greater than and -lt less than operators.įirst let us start with the greater than operator. Checking if value is greater or less than This is great for performing a check and executing a specific command if the test is true, and a different command if the test is false. The else statement is part of an if statement, its actions are only performed when the if statements comparison operators are not true. The above if statement works great for checking if the user exists but what happens when the user doesn't exist? Right now, nothing but we can fix that. This is a simple and fast way of checking whether a string exists within a file and if it does perform some action. The grep -ic command tells grep to look for the string and be case insensitive, and to count the results. Now this alone doesn't seem all that amazing but when you combine it with other commands, like for example checking to see if a username exists in the passwd file. The first example is one of the most basic examples, if true. In this article I am going to show several examples of using if statements and explain how they work. These scripts can be used for anything from installing software, configuring software or quickly resolving a known issue.Ī fundamental core of any programming language is the if statement. The ability to script mundane & repeatable tasks allows a sysadmin to perform these tasks quickly. Shell scripting is a fundamental skill that every systems administrator should know.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |