Posts marked with "Posts" in posts

Append Contents to Files in Bash

In Bash, we can add contents to file easily using the > character. Like this :

echo "myContents" > ~/myfile.txt

What if you want to append contents to the file without overwriting ? The easiest way to do this is change the “>” to “»”. Just by making this small change, appending will be done :

echo "myContents" >> ~/myfile.txt

Even if the file doesn’t exist, Bash will create one with the contents given to append.

... [READ MORE]

if, else, elif Conditions in Bash

All programming languages have the ifelse conditions and they’re different in syntax according to each programming languages. In Bash, it’s really different and even a small space can cause syntax errors. So, you have to be careful while writing Bash code.

In this post, I’ll give you many examples on how to use if and else conditions in Bash.

if, else

a="AC"
if [ $a = "AC" ]; then
	echo "yes"
else
	echo "no"
fi

Notice the usage of then. It’s necessary to let Bash know that if the condition returns TRUE, do the code below it.

... [READ MORE]

Creating a File with Contents in Bash

It’s really easy to create a file in Bash with cat and > :

cat "/home/simsu/file.txt"
> "/home/simsu/file.txt"

But, what if you want to add contents to file too ? In this case, we can use cat and echo. Here’s how we do it in echo :

echo "My File, My Choice" > "/home/simsu/file.txt"

But, there’s a problem with doing like this. Since there is an option to limit the characters of a Terminal command, adding large contents is not possible. In this case, we need to use cat.

... [READ MORE]

Check if Bash Script is ran by Root User

Root users have the privilege to do administrative stuff in Linux and if you’re creating a Bash script to install something or change something in the system, you need root. So, checking whether the script was ran by root is very important.

if [ "$EUID" -ne 0 ]
   then echo "Please run as root"
   exit
fi

There is no else condition. You can add the code which will does stuff in root after the above. If root didn’t ran the script, then the script will terminate itself without continuing after “Please run as root” message is displayed.

... [READ MORE]

Newton Color Disc in jQuery

Newton Color Disc is used to find the color got by the combination of multiple colors. The color is not actually formed, but the eye identifies the combination as another color due to persistence of vision.

Normally, Newton’s Color disk is made by marking sectors filled with color on a circular shaped disc. When this disc is rotated at high speed, we see the circle as another color. This principle led to the method of creating different colors by the combination of some primary colors at different percentage compositions.

... [READ MORE]

Replace Strings in Files With PHP & Python

There are softwares already that helps to replace a string in a file. Suppose, if you’re making a program that requires saving some data in to a file. By “adding”, the program is actually replacing a word. Let’s see how it’s done in PHPPython.

This is the file where we are going to replace the word :

thisWord
I got thisWord for making it thatWord
replace thisWord already my friend.

We replace the word “thisWord” to “thatWord” containing in this file.

... [READ MORE]

Folder Recursion in PHP & Python

A folder has files and sometimes sub directories. If we use the normal function for obtaining contents of a folder, we won’t get the details of the files in the sub directories. In this case, we have to look over into the sub folders and into other sub folders within this sub folder. This looking up of files deep down is called Recursive Folder Searching.

By doing this, we can search for a file or do various actions with each files thus recursed. An example case of this is the replacing softwares like regexxer. It uses recursion to replace a string to another in multiple files in the same directory even under any sub folders.

... [READ MORE]

Word Palindrome Check in PHP & Python

Palindromes are unique words that are read the same forward and backward. We can identify them using out eyes and brain. How about we identify using a simple program in PHPPython ?

PHP

<?php
$word = strtolower("Malayalam");
$splitted = str_split($word);
$reversedWord = "";
$length = strlen($word);
for($i = 0; $i < $length; $i++)
	$reversedWord .= $splitted[$length - $i - 1];
echo $word == $reversedWord ? "It's a palindrome !n" : "It's not a palindromen";
?>

Instead of the loop to reverse the word, you can also use :

... [READ MORE]

Executing Terminal Commands in Python & PHP

Python & PHP are great languages and there are many similarities between them. In this short post, you will learn how to execute terminal commands in PHP & Python.

PHP

As I said before many times in other posts, there areexec andsystem command to execute commands :

exec("firefox ‘http://subinsb.com’");

Or in the other way :

system("firefox ‘http://subinsb.com’");

Python

In Python, there is no exec or system function. But there is a system function in the os nodule. So, you have to include the os module first and then execute the command :

... [READ MORE]

Infinite loop in PHP & Python

Infinite loops are loops that doesn’t have a defined ending. It will continue to run until the program it’s running in is terminated. Normally an infinite loop is created using while loop and is used for continuity of the same program.

In this short tutorial, you will learn how to make an infinite loop in both PHPPython.

PHP

There is no need of defining any variables.

while(1){
  print "Hello"
}

 

... [READ MORE]

Follow/Subscribe

Telegram 

Mastodon  Twitter

GitHub GitLab

Subdomains

Demos  Lab

Past

This blog was once on WordPress. Now a static site. See source code on

GitLab