Skip to main content

Posts

Showing posts from July, 2020

Compare multiple variables Conditions with if else in PowerShell

Multiple conditions to evaluate is sometimes necessary in order to decide whether the script should continue to execute a specific command or do another process. PowerShell is able to do this kind of task using if else statement. Example script below just shows 3 variables that should match and it will output "OK", the output is just a write-host command but it could be replaced with other codes. And if one of the statements does not match the "else" statement is executed. Example code: All statements or condition matches $var1 = "Online" $var2 = "Running" $var3 = "Proceed"   if (( $var1 -eq "Online" ) -and ( $var2 -eq "Running" )   -and ( $var3 -eq "Proceed" )) {   write-host "OK"   } else {   write-host "Not OK unable to Proceed"   } Output: OK One of the variable does not match: $var1 = "Online" $var2 = "Running" $var...

Python check if an item is on the list

How to check whether an item exist on the list? If then else and using the "in" operator in Python is able to check whether an item is on the list or not. Here's a code example, it's not a complete program but a demonstration how the snippet can be used, the code can be integrated into an existing complete code. lst = list() #list declaration in Python         #if item is already on the list it will just continue and will not execute the else command            if yitem in lst: continue          else:           print(" Item 'yitem' is not present in the list, appending item to the list.")           lst.append(yitem)  #action that will be done if item was not found on the list For completeness sake if something needs to be done if the item is found, then the code will be like this:        if yitem in lst:      ...

Bash save Linux command output to a variable

How to save the output of a command to a variable in Bash script? In Bash getting the output of a command line in terminal is sometimes necessary when doing Bash/Shell script. For example, if need to monitor a specific service whether it is installed, running or disabled. Getting the output of the command that checks the status of specific service is quite important, so the script will know on what to execute. If the specific output shows that the service has stopped then the script can decide to start the service, or if the output shows that a specific service or software is not installed then an option to install the software can be done. Here’s an example on how to save the status of a specific output to a variable using Bash. #!/bin/bash dcommand=$(systemctl status gdm.service) command_output=$(echo "$dcommand") str_running='Active: active (running)' if [[ "$command_output" == *"$str_running"* ]]; then   echo "It's the...

Bash slice string in Linux Shell

Strings are quite basic in any scripting or programming languages. If a journey to a thousand miles starts with a single step, in the programming world the journey starts with a string called “Hello World” and beyond “Hello World” pseudocode and algorithm will keep the journey going.   Strings in scripting or programming are just group of characters. So, if string is a group of characters then it can be slice by character or a sub-string can be taken from the string. To slice a string in Bash, a syntax which is part of the Bash library can be used. Syntax is: ${string_to_be_sliced:slice_start_position:slice_end_position} Example string: slicethestring=“Hello Algorithm World” The string is stored in a variable: slicethestring To get the sub-string “Algorithm World”. Code will be:   echo ${slicethestring:6:23} #start the slice at position 6 till position 23 Another way to slice the string from a specified start position till the end; is to tell Bash the star...