Linux

Parse query string in shell script

Sometimes we have to integrate multiple languages like node.js/php/shell script into bash shell script and parse query string to array for supplying query string data for manipulation.

Here is the simple script which will help you to achieve that goal:

#!/usr/bin/env bash
declare -A querydict
populate_querystring_array () {
query=”$1″
while IFS== read arg value
do
querydict[“$arg”]=”$value”
done < <(echo “$query” | sed ‘s/&/\n/g’ )
}

q=’user=mindgeek&password=1234f’
populate_querystring_array “$q”
printf “${querydict[user]}\n”

Read More...

Linux

No package ‘opencv’ found

If you are getting this error while compiling certain modules which requires opencv library which is causing the execution to be aborted.

For fixing it, you need to install lib-opencv using following command:

Ubunntu/Debian – sudo apt-get install libopencv-dev

Centos/Red Hat – sudo yum install opencv-devel

After you do so, pkg-config –cflags opencv and pkg-config –libs opencv will work as expected.

Read More...

Linux

How to configure libstdc++ with GCC 4.8?

You need to tell your dynamic linker (it’s executed when you run your program) where to find the library. Set LD_LIBRARY_PATH to the path of the library (probably somewhere under /app/gcc/4.8.0/lib or something).

Use find /app/gcc/4.8.0 -name “libstdc++.so.6”. Add the directory to your LD_LIBRARY_PATH. e.g with the path I mentioned:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/app/gcc/4.8.0/lib (if you’re using a bourne-like shell which the default on Linux).

Then try to run your program.

Voila!!

Read More...

Linux

What is configure, make, make install ?

There are lot of confusions for usual configure, make, make install sequence to get a program running in Linux. Unfortunately, most developers today have never used a compiler to install an application and used the GUI application builders.

The make utility is designed to decrease a programmer’s need to remember line of commands. I guess that is actually the good way of saying, it decreases a programmer’s need to document. In any case, the idea is that if you establish a set of rules to create a program in a format make understands, you don’t have to remember them again. The make utility has a set of built-in rules so you only need to tell it what new things it needs to know to build your particular utility. For example, if you typed in make program, make would first look for some new rules from you. If you didn’t supply it any then it would look at its built-in rules. One of those built-in rules tells make that it can run the linker (ld) on a program name ending in .o to produce the executable program.

So, make would look for a file named program.o. But, it wouldn’t stop there. Even if it found the .o file, it has some other rules that tell it to make sure the .o file is up to date. In other words, newer than the source program. The most common source program on Linux systems is written in C and its file name ends in .c.

If make finds the .c file (program.c in our example) as well as the .o file, it would check their timestamps to make sure the .o was newer. If it was not newer or did not exist, it would use another built-in rule to build a new .o from the .c (using the C compiler). This same type of situation exists for other programming languages. The end result, in any case, is that when make is done, assuming it can find the right pieces, the executable program will be built and up to date.

The old UNIX joke, by the way, is what early versions of make said when it could not find the necessary files. In the example above, if there was no program.o, program.c or any other source format, the program would have said:
make: don’t know how to make program. Stop.

Getting back to the task at hand, the default file for additional rules in Makefile in the current directory. If you have some source files for a program and there is a Makefile file there, take a look. It is just text. The lines that have a word followed by a colon are targets. That is, these are words you can type following the make command name to do various things. If you just type make with no target, the first target will be executed.

What you will likely see at the beginning of most Makefile files are what look like some assignment statements. That is, lines with a couple of fields with an equal sign between them. Surprise, that is what they are. They set internal variables in make. Common things to set are the location of the C compiler (yes, there is a default), version numbers of the program and such.

This now beings up back to configure. On different systems, the C compiler might be in a different place, you might be using ZSH instead of BASH as your shell, the program might need to know your host name, it might use a dbm library and need to know if the system had gdbm or ndbm and a whole bunch of other things. You used to do this configuring by editing Makefile. Another pain for the programmer and it also meant that any time you wanted to install software on a new system you needed to do a complete inventory of what was where.

As more and more software became available and more and more POSIX-compliant platforms appeared, this got harder and harder. This is where configure comes in. It is a shell script (generally written by GNU Autoconf) that goes up and looks for software and even tries various things to see what works. It then takes its instructions from Makefile.in and builds Makefile (and possibly some other files) that work on the current system.

Background work done, let me put the pieces together.

You run configure (you usually have to type ./configure as most people don’t have the current directory in their search path). This builds a new Makefile.
Type make This builds the program. That is, make would be executed, it would look for the first target in Makefile and do what the instructions said. The expected end result would be to build an executable program.
Now, as root, type make install. This again invokes make, make finds the target install in Makefile and files the directions to install the program.

References:
http://tldp.org/LDP/LG/current/smith.html

Read More...

Linux

Remove Double Extensions in Linux

 

Following script will help you to remove double extensions which can be added by some programs or by user mistakes and need to be corrected in bulk.

Here is the solution, You have to perform this in the current working directory (non-recursively).

vim fix_extension.sh

Content of fix_extension.sh


#!/bin/bash
echo "Existing Extension" $1
echo "Converted Extension" $2
for file in *$1
do
mv "${file}" "${file%$2}"
echo " -- Fixing ${file%.$2}"
done

chmod +x  fix_extension.sh

Now run this script as:

sh fix_extension.sh <double_wrong_extension> <correct_extension>

for example,

For double extension .jpg.jpg do this:

sh fix_extension.sh .jpg.jpg .jpg

Voila!!

Download this script : fix_extension.sh

Read More...

Linux

Check Application Memory usage using simple command

To check the total amount of memory used by any program’s child processes:

Create a shell script
# vim checkmem.sh

Contents:

echo “————————————-Memory Checkup————————————“;

ps -ylC $1 –sort:rss | awk ‘!/RSS/ { s+=$8 } END { printf “%s\n”, “Total memory used by child processes: “; printf “%dM\n”, s/1024 }’

Run Script with a argument whose child processed needs to be calculated:

sh checkmem.sh nginx

————————————-Memory Checkup————————————
Total memory used by child processes:
5679M

Download checkmem.sh

Read More...

Linux

Setup Pure-FTPd Daemon in Linux

Install Pure-FTPd
In a Linux shell run the following:
Ubuntu –
apt-get install pure-ftpd-common pure-ftpd

Red Hat, Centos-
yum install pure-ftpd-common pure-ftpd

Now we need to create a new system group for pureftpd:

groupadd ftpgroup

Now we add a user for the group and give that user no permission to a home directory or a shell:

useradd -g ftpgroup -d /dev/null -s /etc ftpuser

Create a new user
Lets create our first FTP user. In this example our user will be “justin”:

pure-pw useradd justin -u ftpuser -g ftpgroup -d /home/pubftp/justin -N 10

In the above command we gave him a limit of 10 MB disk space with the option “-N 10”. Now you have to enter justin’s new password twice.
By default your users will be saved in /etc/pure-ftpd/pureftpd.passwd, but first we have to update the pureftpd Database:

pure-pw mkdb

The “Database” here is simply a binary file but it is ordered and has an index for quick access.
User Information
To get some user details enter the following to get a complete list of all pureftpd users:

pure-pw list

If you want to show information about a specific user:

pure-pw show justin

This will show you detailed information about the user “justin”.
You will notice that the line “Directory: /home/pubftp/justin/./” has a trailing ./ but you shouldn’t worry as this is simply the chroot for the user, which means he can’t go “above” his directory.
Resetting a password
If you forget the password for a user, you can reset it as follows:

pure-pw passwd justin

After a password reset update your database:

pure-pw mkdb

Starting the FTP Server
To test the server let’s start it:

/usr/sbin/pure-ftpd -S 127.0.0.1,21 -c 30 -C 1 -l puredb:/etc/pureftpd.pdb -x -E -j -R

The shell will open up a new pure-ftpd session and you should be able to connect to your FTP server. Use an FTP client to test whether or not you are able to login with your user details you created.
Once you are happy close the session off:

ctrl z

Configuring Pure-FTPd
Right so by now you have created a user and been able to connect to your FTP server. We now want to setup a few things so that we can run Pure-FTPd as a daemon.
First you need to set Pure-FTPd as a standalone server:

vim /etc/default/pure-ftpd-common

Replace this:

STANDALONE_OR_INETD=inetd

With this:

STANDALONE_OR_INETD=standalone

Now we want to ensure that the standalone server checks our usernames and passwords against the pureftpd database file:

cd /etc/pure-ftpd/conf
vim PureDB

Add the following to that file (if it doesn’t exist):

/etc/pure-ftpd/pureftpd.pdb

Now we need to create a symbolic link to the PureDB file:

cd /etc/pure-ftpd/auth
ln -s /etc/pure-ftpd/conf/PureDB 50pure
ls -ls

You should now see a new file “50pure” linking to ../conf/PureDB.
Restart Pure-FTPd:

/etc/init.d/pure-ftpd restart

Read More...

Linux, MySql, PHP

Php Mysql installation in a linux server

Use this command on a Red Hat based linux installation to install Php Mysql completely without any issues:

yum install php php-devel php-gd php-imap php-ldap php-mysql php-odbc php-pear php-xml php-xmlrpc curl curl-devel perl-libwww-perl ImageMagick libxml2 libxml2-devel

Read More...

Linux, MySql

Mysql Socket issue

Sometimes mysql gives an error while starting/opening mysql service for the socket, It happens when your mysql socket get corrupted.
To create a new socket you just need to:
Comment the socket settings in /etc/my.cf
Restart mysqld and httpd 🙂

Read More...

Linux

Linux Open Port 80 (HTTP Web Server Port)

Login as root user and type:

# vi /etc/sysconfig/iptables
Append rule as follows:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
Save and close the file. Restart iptables:
# /etc/init.d/iptables restart

Read More...

Linux, Magento

Adding a Cron Job in Magento

Add the following lines to your module’s config.xml file under etc folder.

<crontab>

<jobs>

            <namespace_yourmodulename>

                <schedule><cron_expr>*/1 * * * *</cron_expr></schedule>

<run><model>navision/observer::methodToCall</model></run>

</namespace_yourmodulename>

</jobs>

</crontab>

 

Observer:

<?php

class Namespace_Yourmodulename_Model_Observer {

public static function methodToCall(){

//Code to Execute

Mage::log(“Cron Executed”);

}

}

?>

Note: Magento root file cron.php needs to be added to crontab on web server.

Read More...