In the above code, we look for all possible files and print their names and then in the next step we look some a specific file and a directory in the path location and print out if any of the file matches the ones we are looking for. In case of the remainder being 0, post-operation of modulo operator, the output is printed that number is even, else in other cases it is printed that number is odd.Įcho "Learn If condition with multiple branching!"Įcho 'find learnIf.sh and Directory LearnIFDir'Įcho " $each_file is neither a learIf.sh nor LearnIFDir” In the above code, the number is entered through the command line, and then the remainder is checked using a modulo operator and then equated with 0 to see if the remainder is 0 or not. Here, we would first look at the code understand that, and then look at the output of the code. In this section, we have sure to include examples which resemble real-time situation and also at the same time keep things simple to understand. Now using the above technical knowledge let us look at some examples in the next section. If the exists and has a file size greater than 0Ĭhecks if the exist and has permission granted for writeĬhecks if the exist and has permission granted for executeĪlong with the above operation, if condition also supports usage of AND (&) and OR (||) operators as well. To check if exists and has the permission granted for reading To check if the mentioned is present and if it is a directory Integer 1 is less than Integer 2 Numerically Integer 1 is equal to Integer 2 Numerically The string length is 0 or in other words, it is an empty string The string length should be greater than 0 Below listed are a few common operations which are widely used and hence become imperative to understand them as they constitute an important part. Now we are in a position to understand different operators supported by if conditions. This is to make sure that another person can easily understand the code and in cases of maintenance of the code, it is super easy when indented. Indentation is not essential for shell script but when it comes to best coding practice, it is always suggested to follow proper indentation. One thing we must be wary about is the indentation. This type of statement is for adding another if condition within the else block. This is a nested else if statement and acts more like the if else statement. If the condition is not satisfied one would feel like start climbing down the ladder and only stops if one condition is satisfied. This type of statement is more like a ladder. Starting from here, and the next type of if condition statement, the condition starts becoming branched. This type of statement is used when the program needs to check one condition and perform a task if the condition is satisfied or perform the other set of tasks if the condition is not.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |