Skip to main content

Posts

Showing posts with the label Linux for noob

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…

Linux find accessed and modified files

Finding accessed and modified files might be necessary at times to check or for audit purposes.
If files kept in a folder or directory has been accessed or modified but should not be the case then something dubious is going on. 
In Linux finding accessed and modified files can be done in a one liner command.
find /home -type f -amin -60 || -mmin -60 -print
Above command will find or show any files accessed within the last 60 minutes with the option "-amin" and it will show also the files modified within the last 60 minutes with the option "-mmin".
A shell script can be created and further processing can be done when files are detected.
The time can be adjusted if there's a need, but a more robust solution to check any accessed or modified files should be a file system watcher, but above command is quite helpful to check any activity that should not be occurring.

Cheers..till next time!================================Free Android Apps:
Click  links below to find out mor…

Linux sed remove duplicates and get unique values

Removing duplicates and getting unique values is quite easy provided that the input data follows a specific format, for example the string or raw data has spaces in between.
But a dilemma can occur if the data has no spaces in between the characters of the string, instead of spaces it is separated by dashes.
So, how to remove duplicates, get the unique values and still retain the format of the raw data?
Like this raw data: (just a sample string) the-quick-brown-fox-jumps-over-the-lazy-dog-jumps-over-the-lazy-cat-jumps-over-the-rabbit
When removing duplicates and getting unique values via this command:
sort duplicates.txt | uniq (this will work if the data is separated by spaces)
duplicates.txt assumes that it has the string as illustrated above.
Sample output:

The output will be the exactly be the same with the input. Why? It is because the whole string is treated as literal one string, because the dashes connect between the character eliminating the space delimiter.
Example, if the requiremen…