Skip to main content

Posts

Showing posts with the label Script

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"}

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:             print "Item is found and is present on the list."          #do something more if required        else:           print(" Item 'yitem&…

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 there, gdm.service is runn…

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: slicethestringTo get the sub-string “Algorithm World”.Code will be:echo ${slicethestring:6:23} #start the slice at position 6 till position 23Another way to slice the string from a specified start position till the end; is to tell Bash the start position and just omit the end position.O…

Read or get the list of contents on S3 bucket

S3 or Simple Storage Service bucket is quite handy to store files or any data on AWS cloud.As with any storage, online or offline organizing the data is quite an issue.When there are a lot of files of data on the storage; finding the data that you will need will be difficult. Especially, if the data or bucket is not organized properly. And even if the data has been organized by folders with proper time stamp but no specific data catalog to classify which one is which one it is quite a challenge to find or get the data on time.S3 is ideal for backup, since backup is often accessed only when it is needed. So, familiarization of the backup folder structure is necessary. In order to find or get easily the data, listing the contents of the S3 bucket is a good option when unable to find the files or data needed.Once the list is ready, opening the list via editor such as notepad and searching or finding thru the editor will provide an option to find things quickly and easily.
How to list the …

PowerShell switch case call function

Code below collect user input and uses switch case statement to check if the input matches, and if it matches a function is called that will execute commands.Here’s the code:#get or read from user input$computer_name=read-host ("Enter Computer Name:")switch ($computer_name ){#if the input is computer_1 then function func_comuputer1 is called computer_1{func_computer1}computer_2{func_computer2}}#function called if computer_1 is the inputfunctionfunc_computer1 {write-host"You entered Computer_1"#or replaced with other function like reboot / shutdown /or other commands#Restart-Computer -ComputerName computer_1}
#function called if computer_2 is the inputfunctionfunc_computer2 {write-host"You entered Computer_2"#or replaced with other function like reboot / shutdown /or other commands#Ex: Enter-Pssession computer_2}

Code below will keep asking for input and will only exit if a specific string matches to exit the script. Here’s the code:do {$computer_name