[SOLVED] Cannot get $? (ret code) of cat file.lst | xargs rm -
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 ...
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.