Wednesday, 27 April 2016

Difference bewteen pipe, xargs, and exec

 xargs, pipes and exec:

First off, exec is nothing like the first two. exec does a variety of things from executing a command without returning(the shell is replaced by the new command), as well as opening/closing files.

pipes transfer the output of one program into the input of another. Without the pipe or redirection, they'd be trying to read from your keyboard and trying to print to your screen, but you can feed them data from any source and send their output anywhere you like. Shell utilities are intended to be flexible that way.

# useless example
echo asdf | cat

'echo' prints to standard output, 'cat' reads from standard input, the pipe joins them together so 'asdf' is read by cat and printed.

But what about this:

# This won't print anything
echo something | echo

echo does not read from standard input, it takes commandline parameters and nothing but parameters. So how do we translate from pipe into that?

This is what xargs is for.
echo a b c d | xargs command

is equivalent to command a b c d. Whenever you need to translate from a pipe or file into commandline arguments, xargs serves.

# This prints 'something' into xargs' input, causing xargs to run 'echo something'.
echo something | xargs echo 

Let's consider an example of exec:

$ find . -name "*.txt" -exec ls -l {} \;

The above command will create multiple process and generate the result. Suppose there are 3 text files named file1.txt, file2.txt, & file3.txt. The above command will start below 3 commands:

ls -l file1.txt
ls -l file2.txt
ls -l file3.txt

To restrict to 1 thread or 1 process, you can use xargs or +symbol. For example:

$ find . -name "*.txt" | xargs ls -l
$ find . -name "*.txt" -exec ls -l {} \+

However, you can mention the parallelism in xargs with -P option. The above command will result in:

ls -l file1.txt file2.txt file3.txt

Only 1 thread.


Saturday, 9 April 2016

Composer | Windows | PHP Error : "You must enable the openssl extension to download files via https"

PHP - Composer - Windows

Okay, you are trying to install composer, and encountered the above error. This error occurred, if your php_openssl.dll is missing or commented in your php.ini.

Steps to resolve:

1. Download from here (64 bit version). (Other version can be found here: PHP for Windows)

2. Unzip it and copy all the contents inside of uncompressed folder in to the C:\Program Files\php directory. (Create php directory)

3. Add C:\Program Files\php in your environment PATH variable.

4. Download composer and try to install it.

5. If you get the above error, copy the php.ini-development file present in C:\Program Files\php, and rename it to php.ini file.

6. Open the php.ini file.

7.  Find the ";extension=php_openssl.dll " then change it to "ext/php_openssl.dll" (removed semi-colon and added ext)

and continue with the installation of composer. It will install successfully.