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.


Code:
# 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:


Code:
# 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.
Code:
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.


Code:
# 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
or
$ 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.


Credit: unix.com

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 php.zip 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.

Monday, 14 December 2015

Creating a Restaurant Menu using Shell script

I love to visit restaurant and on their counter you will see a machine, having list of all the items with their price tags. With the help of that machine, they generate your bills. You can create a same using the shell script.

Here is the code:

#!/bin/bash
out=$(dialog --stdout --menu 'CodingTheRightWay Menu' 999 999 999 Chocolate \$5 IceCream \$10)
echo "You ordered chocolate $out"

Save the code, and make it executable.

Once you run the code, then it will look like this:



You can use your keyboard arrows to choose the item, and for placing order. Cool!

Now, it's your time to create something interesting out of it.

Cheers,

Wednesday, 18 November 2015

One Liner to delete all the files with names present in the txt file using xargs bash




There is simple one liner, that will delete all the files, you listed in a text file containing their names.

For Ex:

Create a file with name "filenames" contains the list of all the files  that you want to delete, then execute below command on the terminal.

xargs -a filenames -d'\n' rm

xargs -  to build and execute command lines from
-a - to use for providing input from text file instead of stdin
filenames - the file you just created
-d - delimiter
rm - command to pass the list

You can also use "-i" with rm for interactive mode.

Cheers.




Wednesday, 7 October 2015

How to change the default display output limit of terminal ubuntu linux

By default, in ubuntu, when you type your command, or try to open your log/messages file, the output goes beyond the display, and it trims out. Below are the methods, that can help you to see more output on the terminal:
1. Go to Edit->Profiles->Edit->Scrolling, and select the option unlimited.

2. $ cat /home/abcd/Downloads/fileName.txt | more , with the usage of more command.
3. You can save your complete session, by using script command.
script — make typescript of terminal session
Ex:
$ script /tmp/command.out
Script started, file is /tmp/command.out
$ the_command
...
$ exit
Script done, file is /tmp/command.out
$ less /tmp/command.out

Saturday, 12 September 2015

How to check your NIC (network interface card) mode

Occassionally you'll want to verify that your gigabit network is actually running in gigabit mode.  Or, to futureproof this little post, maybe we're talking about kajigabits.

Anyway, normally there are little mode LEDs on your network hardware that indicate what speed it's operating at.  However, if you aren't in a position to physically see these little lights on the back of your network card or router, or you are but you're too lazy to get off your cushy chair and go look, here are a couple of quick and totally unintuitive ways of examining things using nothing but your trusty shell.

Use mii-tool (as root):
$ mii-tool -v eth0
Look at the 'negotiated' value.  It'll indicate what speed the interface is using.  'FD' means 'Full Duplex'.  'HD' means 'Half Duplex'.  For example, '100baseTx-FD' means 100mbit Full Duplex.

Use tcpdump (as root):
$ tcpdump -v -i eth0 -c 1
Look at what it says for 'link-type', although I'm not convinced this is an accurate method for speed reporting.  But tcpdump is awesome for other stuff, and maybe I just don't know how to interpret what it's saying.

Use dmesg:
$ dmesg | grep eth0
Look for a line similar to one of the following:
eth0: link up, 100Mbps, full-duplex
e1000e: eth0 NIC Link is Up 100 Mbps Full Duplex
It may be that your dmesg buffer doesn't go back far enough to include that.  If so, try one of the other methods.  Or I guess you could cycle the interface down/up, and then peak at dmesg again.

Use ethtool:
$ ethtool eth0
Look for the 'Speed:' value.  Lots of other good info is listed as well.  This technique  is the one that produces the most self-explanatory output, but it probably also the least likely to already be available on your system.

Source: http://negativesum.net/

Sunday, 6 September 2015

Sunday One Liner

Today's one liner is to display the watch. Thanks to AltoidNerd.



In this one liner, we have used the following programs:

watch: The watch will execute the program periodically and display the result on complete screen.

figlet: the figlet provides different style and size of font

 TOIlet - display large colourful characters (heard this command today itself) :))
 watch -tcpn.5 'figlet -f big -W "$(echo "coding\n-\nscripting")" |  toilet -f term --metal'