LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-03-2011, 06:22 PM   #1
FireRaven
Member
 
Registered: Apr 2006
Location: Australia
Distribution: Debian Squeeze
Posts: 135

Rep: Reputation: 18
xargs take string as single argument?


Hi, does anyone know if it's possible for xargs to take it's stdin and create 1 single parameter for it.

Example I want this python script's first argument to say 'hello world' like this:
Code:
$ python -c 'import sys;print sys.argv[1]' 'hello world'
hello world
If however I pipe a 'hello world' string to xargs I only get the 'hello' as the first argument and 'world' as the second, but I want them both as the 1st argument:
Code:
$ echo 'hello world' | xargs python -c 'import sys;print sys.argv[1]'
hello
$ echo 'hello world' | xargs python -c 'import sys;print sys.argv[2]'
world
Is there a way for xargs to take a literal input and use just as the first parameter. The idea is to pipe long messages (from emails etc) to the first parameter of a program, so treading all special characters as literals from xargs is what I want.
 
Old 10-03-2011, 06:40 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Hello,

Would one of the following work?
Code:
echo "\"hello world\"" | xargs python -c 'import sys;print sys.argv[2]'

echo "'hello world'" | xargs python -c 'import sys;print sys.argv[2]'
Cheers,

Josh
 
Old 10-03-2011, 08:23 PM   #3
FireRaven
Member
 
Registered: Apr 2006
Location: Australia
Distribution: Debian Squeeze
Posts: 135

Original Poster
Rep: Reputation: 18
Both of those work, but I'm getting my input not from the echo command and the input varies, so quoting is not an option.
The input will be something like:
Code:
$ cat emailmessage | xargs python -c 'import sys;print sys.argv[1]'
 
Old 10-03-2011, 08:30 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
What you could do is create a bash script to cat whatever file you need to a single variable, and then try to pass the variable over to the xargs python part. I'm not on linux right now, so I can't test this and mess around with it for you.
 
Old 10-03-2011, 08:48 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,796

Rep: Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087
xargs can use NUL as an argument separator:
Code:
{ echo 'hello world'; echo -ne '\0' } | xargs -0 python -c 'import sys;print sys.argv[1]'
 
Old 10-03-2011, 09:09 PM   #6
FireRaven
Member
 
Registered: Apr 2006
Location: Australia
Distribution: Debian Squeeze
Posts: 135

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by ntubski View Post
xargs can use NUL as an argument separator:
Code:
{ echo 'hello world'; echo -ne '\0' } | xargs -0 python -c 'import sys;print sys.argv[1]'
That works and will probably do for what I need.
Thanks guys.
 
Old 10-03-2011, 09:35 PM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
alternative

Hi,

just another alternative:
Code:
echo 'hello world' | xargs -I{} python -c 'import sys;print sys.argv[1]' '{}'
 
Old 10-10-2011, 08:36 AM   #8
tange
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Rep: Reputation: 9
Use GNU Parallel:
Code:
$ echo 'hello world' | parallel python -c 'import sys;print sys.argv[1]'
Watch the intro videos to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ
 
  


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
xargs: unmatched single quote akudewan Linux - Newbie 5 07-28-2014 12:16 PM
Passing string as a single argument in shell script sinoob Linux - General 3 09-14-2011 03:21 AM
xargs removes single quotes after awk Poki Linux - Newbie 4 06-26-2009 09:25 AM
One argument version of xargs? adamcannon Linux - Newbie 3 02-06-2008 11:41 AM
xargs: unmatched single quote fillatre Linux - Software 2 06-05-2004 10:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:52 AM.

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