LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2013, 10:31 PM   #1
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Rep: Reputation: 85
Linux command input done in Java


Many commands in Linux take input one of two ways, stdin, or as an argument to a file. Examples:


Code:
echo "text from stdin" | lpr
lpr filename.txt
  

echo "text from stdin" | nl
nl filename.txt
The same is true with awk, sed, grep, and many others. How can the same behavior happen from a command line app written in Java? I believe System.in represents stdin. Reading stdin is not difficult. Reading from a file is not difficult, but how can the app act accordingly to how it was called on the command line?
 
Old 08-27-2013, 12:31 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I haven't needed to often but in the past I've just tested whether stdin was a pipe and if not then check for data passed via argument .. here's a perl fragment that may clarify:
Code:
if ( -p STDIN ) {

  while (<STDIN>) {
    push(@pids, (split(/ /, $_)));
  }

}
else {

  $numArgs = @ARGV;
  if ( $numArgs > 0 ) {

    @pids = @ARGV;
    chomp @pids;

  }
...
 
Old 08-27-2013, 02:36 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Check the number of arguments (argv.length). If positive, there are parameters (filenames), otherwise read strin.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why after start of a command the invitation for input next command has not appeared. ukrainet Linux - General 3 06-24-2005 08:16 AM
java - how do you input from the command line zeviddalop Programming 4 11-10-2004 11:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration