Thursday, December 11, 2014

Linux -- XARGS --some programs accept their arguments from STDIN and some of them accept as command line arguments

When you try to use pipe to supply a file name(as an argument) to a command such as "touch", you will receive an error like in the following example:

If we echo a name and use a unnamed pipe (|) to supply the name of the file to touch ; we will receieve a missing file operand error.

echo erm | touch

touch: missing file operand
Try `touch --help' for more information.

When we use an unnamed pipe for doing this kind of work; we acutally connect the stdout of echo command, to the stding of touch command. So, this error is acutally normal, because touch program does not accept its arguments to be passed as stdin..

On the other hand; if we use an unnamed pipe to count the processes running in the system; like the example below;

ps -ef | wc -l
422

We wont encounter errors, as wc command can receive its inputs from stdin..

So far so good..

Well, but what if we want to supply the arguments to a command that accepts its inputs as command line arguments (like touch command in the example above) ? 

We can accomplish this task using xargs command/program in conjunction with a pipe.
"xargs" can build and execute command lines from standard input

For example: to supply an argument from echo command to touch command; we can use;

echo erm3 | xargs touch

Above command will create a file named erm3 in the current working directory of our process.

Lastly, I will give an example to supply an output of Oracle 's Sqlplus program as an input to a touch command ..

We first create a named pipe->

mknode /tmp/erman.pipe p
ls -al /tmp/oracle.pipe

prw-rw-r-- 1 appldev appldev 0 Dec 11 11:02 /tmp/erman.pipe

Then we connect to sqlplus and make it spool to our erman.pipe , while directing what's written to oracle.pipe to touch command as arguments using xargs...

[appldev@ermandev erman]$ sqlplus apps/apps

SQL*Plus: Release 10.1.0.5.0 - Production on Thu Dec 11 11:01:36 2014

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> set escape on;
SQL> host nohup  xargs touch < /tmp/erman.pipe \&

SQL> spool /tmp/oracle.pipe
SQL> nohup: appending output to `nohup.out'

SQL> select * from dual;

D
-
X

SQL> spool off
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

So , we chck our current working directory , and see that touch command have created the files with the names of the strings that sqlplus produced as an output.
[appldev@ermandev erman]$ ls -lrt
total 0
-rw-rw-r-- 1 appldev appldev 0 Dec 11 11:02 X
-rw-rw-r-- 1 appldev appldev 0 Dec 11 11:02 SQL>
-rw-rw-r-- 1 appldev appldev 0 Dec 11 11:02 spool
-rw-rw-r-- 1 appldev appldev 0 Dec 11 11:02 select
-rw-rw-r-- 1 appldev appldev 0 Dec 11 11:02 off
-rw------- 1 appldev appldev 0 Dec 11 11:02 nohup.out
-rw-rw-r-- 1 appldev appldev 0 Dec 11 11:02 from
-rw-rw-r-- 1 appldev appldev 0 Dec 11 11:02 dual;
-rw-rw-r-- 1 appldev appldev 0 Dec 11 11:02 D
-rw-rw-r-- 1 appldev appldev 0 Dec 11 11:02 *

This was just a demonstration, this kind of things can be used to do more complex and useful works.
Consider, deleting some files which reside in the filesystem but deciding which file to delete by querying an Oracle Table.
Consider EBS.. Consider purging Concurrent Request log & out files... 

No comments :

Post a Comment

If you will ask a question, please don't comment here..

For your questions, please create an issue into my forum.

Forum Link: http://ermanarslan.blogspot.com.tr/p/forum.html

Register and create an issue in the related category.
I will support you from there.