LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-08-2009, 03:11 AM   #1
john lee
Member
 
Registered: Jun 2001
Location: Sydney, Australia
Posts: 42

Rep: Reputation: 15
Cannot get $? (ret code) of cat file.lst | xargs rm -


I cannot get a correct $? (ret code) of:

cat file.lst | xargs rm -

It always return 0 for me even though the files to be deleted do not exist.

Any thoughts?
 
Old 04-08-2009, 04:33 AM   #2
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware
Posts: 572

Rep: Reputation: 154Reputation: 154
What OS are you using?


Try

# cat file.list | xargs rm
 
Old 04-08-2009, 04:43 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello John

Works for me (although rm doesn't recognise the - option so takes it as a file name):
Code:
c@CW8:~$ echo foo > trash
c@CW8:~$ cat trash | xargs rm -
rm: cannot remove `-': No such file or directory
rm: cannot remove `foo': No such file or directory
c@CW8:~$ echo $?
123
c@CW8:~$ cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=8.04
DISTRIB_CODENAME=hardy
DISTRIB_DESCRIPTION="Ubuntu 8.04.2"
c@CW8:~$ rm --version 
rm (GNU coreutils) 6.10
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin, David MacKenzie, Richard Stallman, and Jim Meyering.
Best

Charles
 
Old 04-08-2009, 06:38 AM   #4
john lee
Member
 
Registered: Jun 2001
Location: Sydney, Australia
Posts: 42

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ChrisAbela View Post
What OS are you using?
Try
# cat file.list | xargs rm
I am using RHEL5. Any suggestion?
 
Old 04-08-2009, 07:40 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
There are two components involved here -- the shell and the rm command.

If you run the rm command on its own with identical arguments, does it set $? as expected?

If you run
echo XXX > /dev/null | false
then check $?, does the shell report 1 as expected from false?

Which rm are you using? Which shell are you using?
 
Old 04-08-2009, 08:11 AM   #6
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
catkin is on the right track here. Whenever you use a pipe on the command line, the return code that is returned is not what you expect. You are getting the return code for part of the command, but not the whole line. I think that you answer will be in restructuring your command so that you don't have that pipe in it. Try ...

Code:
rm $(cat file.list)
 
Old 04-08-2009, 09:18 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
According to http://www.gnu.org/software/bash/man...html#Pipelines "The exit status of a pipeline is the exit status of the last command in the pipeline, unless the pipefail option is enabled (see The Set Builtin). If pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully."

But that's for bash; we don't yet know what shell John Lee is using.
 
Old 04-08-2009, 09:23 AM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by Hobbletoe View Post
catkin is on the right track here. Whenever you use a pipe on the command line, the return code that is returned is not what you expect. You are getting the return code for part of the command, but not the whole line. I think that you answer will be in restructuring your command so that you don't have that pipe in it. Try ...
In bash, the return status of a pipeline is this of the last command, even if some previous part of the pipeline failed. You can easily test with any meaningless command. For example, this should return non-zero:

Code:
stat / | stat /asdf
While this will return 0, because the last command will suceed even if the previous one fails:

Code:
stat /asdf | stat /
In a pipe, all commands run regardless if a previous one failed. Never forget that.

However, since bash 3.0 we can do this:

Code:
set -o pipefail
If you do this, the pipe will return the last non-zero exist status. Hence, the result of the last command in the pipe that failed. Again, regardless of the result, all the commands in the pipeline will run, which might not be the intended result.

Code:
rm $(cat file.list)
Yes, you could do this, but this is not a proper solution. If the list of files is too long it will fail. If you absolutely need something like this, better use xargs, find -exec or a loop like this:

Code:
cat file.list | while read file; do whatever with $file; done
Or something similar.

Last edited by i92guboj; 04-08-2009 at 09:25 AM.
 
Old 04-08-2009, 09:26 AM   #9
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
I was unaware of this. Thank you catkin and j92guboj. I just remember a few years ago having run afoul return status when using a pipe, and have always just tried to stay clear of it. This is good to know.

Last edited by Hobbletoe; 04-08-2009 at 09:35 AM.
 
Old 04-08-2009, 12:52 PM   #10
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware
Posts: 572

Rep: Reputation: 154Reputation: 154
Quote:
tux1@darkstar:~$ touch foo
tux1@darkstar:~$ echo foo > file.list
tux1@darkstar:~$ cat file.list | xargs rm
tux1@darkstar:~$ echo $?
0
tux1@darkstar:~$ cat file.list | xargs rm
rm: cannot remove `foo': No such file or directory
tux1@darkstar:~$ echo $?
123
tux1@darkstar:~$
Frankly, I understood the answers but not the question!
 
  


Reply



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 and spaces in file path mgichoga Linux - Software 2 01-04-2008 12:57 PM
Using xargs to process file from list? jon_k Programming 2 01-14-2007 04:14 PM
File name expansion with {} in gnu find and xargs anamericanjoe Linux - Software 2 09-16-2006 03:31 PM
where can I donwland the source code of 'cat'? bigapple Programming 3 08-17-2005 01:38 PM
copying files and give new unique names to each file by using xargs command gnim66 Programming 6 06-22-2005 08:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:59 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