How to redirect mdfind output to if else statament in bash
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I am trying to create a script that look for some files, listed in an input file, all over the computer and then copy those files in a specific folder.
So far all ok with a for loop and mdfind "command" | xargs.
But the if i add an if statement to create an output file with the list of the files that mdfind didn't find, here comes the problems.
Here is my actual code:
if [ $# -eq 0 ]
then
echo "Please introduce the name of the input file after runnig the command.
For this script you need an INPUT.txt file containing only the list of the
samples you want to copy."
else
for i in $(cat $1);
do mdfind "kMDItemDisplayName == $i*.ab1" | xargs -I '' cp ''
/Users/xxxxx/xxxxx/xxxxx/xxxxxxx/test_moving/;
if [ $? -eq 0 ]
then
echo $i >> extractions_not_found.txt
else
echo $i >> extractions_found.txt
fi
done
fi
I am using $? to redirect the output of
mdfind "kMDItemDisplayName == $i*.ab1" | xargs -I '' cp '' /Users/xxxxx/xxxxx/xxxxx/xxxxxxx/test_moving/;
but it is alway = 0, even when it cant find the one file listed in the input file.
Anyone can help me understand where is the error??
Thank you!
bash output
add a comment |Â
up vote
0
down vote
favorite
I am trying to create a script that look for some files, listed in an input file, all over the computer and then copy those files in a specific folder.
So far all ok with a for loop and mdfind "command" | xargs.
But the if i add an if statement to create an output file with the list of the files that mdfind didn't find, here comes the problems.
Here is my actual code:
if [ $# -eq 0 ]
then
echo "Please introduce the name of the input file after runnig the command.
For this script you need an INPUT.txt file containing only the list of the
samples you want to copy."
else
for i in $(cat $1);
do mdfind "kMDItemDisplayName == $i*.ab1" | xargs -I '' cp ''
/Users/xxxxx/xxxxx/xxxxx/xxxxxxx/test_moving/;
if [ $? -eq 0 ]
then
echo $i >> extractions_not_found.txt
else
echo $i >> extractions_found.txt
fi
done
fi
I am using $? to redirect the output of
mdfind "kMDItemDisplayName == $i*.ab1" | xargs -I '' cp '' /Users/xxxxx/xxxxx/xxxxx/xxxxxxx/test_moving/;
but it is alway = 0, even when it cant find the one file listed in the input file.
Anyone can help me understand where is the error??
Thank you!
bash output
This is because$?
is getting the exit code ofxargs
, not ofcp
ormdfind
.
â DopeGhoti
Jan 23 at 18:25
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to create a script that look for some files, listed in an input file, all over the computer and then copy those files in a specific folder.
So far all ok with a for loop and mdfind "command" | xargs.
But the if i add an if statement to create an output file with the list of the files that mdfind didn't find, here comes the problems.
Here is my actual code:
if [ $# -eq 0 ]
then
echo "Please introduce the name of the input file after runnig the command.
For this script you need an INPUT.txt file containing only the list of the
samples you want to copy."
else
for i in $(cat $1);
do mdfind "kMDItemDisplayName == $i*.ab1" | xargs -I '' cp ''
/Users/xxxxx/xxxxx/xxxxx/xxxxxxx/test_moving/;
if [ $? -eq 0 ]
then
echo $i >> extractions_not_found.txt
else
echo $i >> extractions_found.txt
fi
done
fi
I am using $? to redirect the output of
mdfind "kMDItemDisplayName == $i*.ab1" | xargs -I '' cp '' /Users/xxxxx/xxxxx/xxxxx/xxxxxxx/test_moving/;
but it is alway = 0, even when it cant find the one file listed in the input file.
Anyone can help me understand where is the error??
Thank you!
bash output
I am trying to create a script that look for some files, listed in an input file, all over the computer and then copy those files in a specific folder.
So far all ok with a for loop and mdfind "command" | xargs.
But the if i add an if statement to create an output file with the list of the files that mdfind didn't find, here comes the problems.
Here is my actual code:
if [ $# -eq 0 ]
then
echo "Please introduce the name of the input file after runnig the command.
For this script you need an INPUT.txt file containing only the list of the
samples you want to copy."
else
for i in $(cat $1);
do mdfind "kMDItemDisplayName == $i*.ab1" | xargs -I '' cp ''
/Users/xxxxx/xxxxx/xxxxx/xxxxxxx/test_moving/;
if [ $? -eq 0 ]
then
echo $i >> extractions_not_found.txt
else
echo $i >> extractions_found.txt
fi
done
fi
I am using $? to redirect the output of
mdfind "kMDItemDisplayName == $i*.ab1" | xargs -I '' cp '' /Users/xxxxx/xxxxx/xxxxx/xxxxxxx/test_moving/;
but it is alway = 0, even when it cant find the one file listed in the input file.
Anyone can help me understand where is the error??
Thank you!
bash output
asked Jan 23 at 18:00
WalterC85
1
1
This is because$?
is getting the exit code ofxargs
, not ofcp
ormdfind
.
â DopeGhoti
Jan 23 at 18:25
add a comment |Â
This is because$?
is getting the exit code ofxargs
, not ofcp
ormdfind
.
â DopeGhoti
Jan 23 at 18:25
This is because
$?
is getting the exit code of xargs
, not of cp
or mdfind
.â DopeGhoti
Jan 23 at 18:25
This is because
$?
is getting the exit code of xargs
, not of cp
or mdfind
.â DopeGhoti
Jan 23 at 18:25
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
After you run mdfind ... |ÃÂ xargs cp ...
, the error code in $?
is that of xargs
, since it's the last command in the pipeline. xargs
returns 123 if any command it executed failed, but if mdfind
doesn't produce any output, then xargs
doesn't do anything, so it also doesn't fail.
However, in Bash, you can find the exit codes of all commands in the last pipeline in the array variable PIPESTATUS
. The exit code of the first command is $PIPESTATUS[0]
etc.
$ false | true | xargs false
$ echo "$PIPESTATUS[*]"
1 0 123
So, instead of using $?
, which gives you the exit status of xargs
, you could use $PIPESTATUS[0]
giving you the exit status of mdfind
. Or save the lot to another variable and test both. (saved=("$PIPESTATUS[@]")
)
Alternatively, use set -o pipefail
so that $?
gives you the exit code of last failing command of the pipeline, if any of them fail. (false |ÃÂ true
would result in $?=1
.)
You can alsoset -o pipefail
andset -e
.
â DopeGhoti
Jan 23 at 20:47
@DopeGhoti, ahpipefail
of course, thanks.set -e
only helps here with pipefail, but I don't think they want that at all.
â ilkkachu
Jan 23 at 20:51
so i did the echo $PIPESTATUS[@] and this is how it looks like: 0 0 0 0 0 0 0 0 0 0 so basically is always 0 even if is not copying any file because mdfind didn't found (in my case i have an input file with 5 files to look for and the last file doesn't exist on my computer so cant be find and/or copied)
â WalterC85
Jan 23 at 22:15
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
After you run mdfind ... |ÃÂ xargs cp ...
, the error code in $?
is that of xargs
, since it's the last command in the pipeline. xargs
returns 123 if any command it executed failed, but if mdfind
doesn't produce any output, then xargs
doesn't do anything, so it also doesn't fail.
However, in Bash, you can find the exit codes of all commands in the last pipeline in the array variable PIPESTATUS
. The exit code of the first command is $PIPESTATUS[0]
etc.
$ false | true | xargs false
$ echo "$PIPESTATUS[*]"
1 0 123
So, instead of using $?
, which gives you the exit status of xargs
, you could use $PIPESTATUS[0]
giving you the exit status of mdfind
. Or save the lot to another variable and test both. (saved=("$PIPESTATUS[@]")
)
Alternatively, use set -o pipefail
so that $?
gives you the exit code of last failing command of the pipeline, if any of them fail. (false |ÃÂ true
would result in $?=1
.)
You can alsoset -o pipefail
andset -e
.
â DopeGhoti
Jan 23 at 20:47
@DopeGhoti, ahpipefail
of course, thanks.set -e
only helps here with pipefail, but I don't think they want that at all.
â ilkkachu
Jan 23 at 20:51
so i did the echo $PIPESTATUS[@] and this is how it looks like: 0 0 0 0 0 0 0 0 0 0 so basically is always 0 even if is not copying any file because mdfind didn't found (in my case i have an input file with 5 files to look for and the last file doesn't exist on my computer so cant be find and/or copied)
â WalterC85
Jan 23 at 22:15
add a comment |Â
up vote
1
down vote
After you run mdfind ... |ÃÂ xargs cp ...
, the error code in $?
is that of xargs
, since it's the last command in the pipeline. xargs
returns 123 if any command it executed failed, but if mdfind
doesn't produce any output, then xargs
doesn't do anything, so it also doesn't fail.
However, in Bash, you can find the exit codes of all commands in the last pipeline in the array variable PIPESTATUS
. The exit code of the first command is $PIPESTATUS[0]
etc.
$ false | true | xargs false
$ echo "$PIPESTATUS[*]"
1 0 123
So, instead of using $?
, which gives you the exit status of xargs
, you could use $PIPESTATUS[0]
giving you the exit status of mdfind
. Or save the lot to another variable and test both. (saved=("$PIPESTATUS[@]")
)
Alternatively, use set -o pipefail
so that $?
gives you the exit code of last failing command of the pipeline, if any of them fail. (false |ÃÂ true
would result in $?=1
.)
You can alsoset -o pipefail
andset -e
.
â DopeGhoti
Jan 23 at 20:47
@DopeGhoti, ahpipefail
of course, thanks.set -e
only helps here with pipefail, but I don't think they want that at all.
â ilkkachu
Jan 23 at 20:51
so i did the echo $PIPESTATUS[@] and this is how it looks like: 0 0 0 0 0 0 0 0 0 0 so basically is always 0 even if is not copying any file because mdfind didn't found (in my case i have an input file with 5 files to look for and the last file doesn't exist on my computer so cant be find and/or copied)
â WalterC85
Jan 23 at 22:15
add a comment |Â
up vote
1
down vote
up vote
1
down vote
After you run mdfind ... |ÃÂ xargs cp ...
, the error code in $?
is that of xargs
, since it's the last command in the pipeline. xargs
returns 123 if any command it executed failed, but if mdfind
doesn't produce any output, then xargs
doesn't do anything, so it also doesn't fail.
However, in Bash, you can find the exit codes of all commands in the last pipeline in the array variable PIPESTATUS
. The exit code of the first command is $PIPESTATUS[0]
etc.
$ false | true | xargs false
$ echo "$PIPESTATUS[*]"
1 0 123
So, instead of using $?
, which gives you the exit status of xargs
, you could use $PIPESTATUS[0]
giving you the exit status of mdfind
. Or save the lot to another variable and test both. (saved=("$PIPESTATUS[@]")
)
Alternatively, use set -o pipefail
so that $?
gives you the exit code of last failing command of the pipeline, if any of them fail. (false |ÃÂ true
would result in $?=1
.)
After you run mdfind ... |ÃÂ xargs cp ...
, the error code in $?
is that of xargs
, since it's the last command in the pipeline. xargs
returns 123 if any command it executed failed, but if mdfind
doesn't produce any output, then xargs
doesn't do anything, so it also doesn't fail.
However, in Bash, you can find the exit codes of all commands in the last pipeline in the array variable PIPESTATUS
. The exit code of the first command is $PIPESTATUS[0]
etc.
$ false | true | xargs false
$ echo "$PIPESTATUS[*]"
1 0 123
So, instead of using $?
, which gives you the exit status of xargs
, you could use $PIPESTATUS[0]
giving you the exit status of mdfind
. Or save the lot to another variable and test both. (saved=("$PIPESTATUS[@]")
)
Alternatively, use set -o pipefail
so that $?
gives you the exit code of last failing command of the pipeline, if any of them fail. (false |ÃÂ true
would result in $?=1
.)
edited Jan 23 at 20:49
answered Jan 23 at 20:41
ilkkachu
49.8k674137
49.8k674137
You can alsoset -o pipefail
andset -e
.
â DopeGhoti
Jan 23 at 20:47
@DopeGhoti, ahpipefail
of course, thanks.set -e
only helps here with pipefail, but I don't think they want that at all.
â ilkkachu
Jan 23 at 20:51
so i did the echo $PIPESTATUS[@] and this is how it looks like: 0 0 0 0 0 0 0 0 0 0 so basically is always 0 even if is not copying any file because mdfind didn't found (in my case i have an input file with 5 files to look for and the last file doesn't exist on my computer so cant be find and/or copied)
â WalterC85
Jan 23 at 22:15
add a comment |Â
You can alsoset -o pipefail
andset -e
.
â DopeGhoti
Jan 23 at 20:47
@DopeGhoti, ahpipefail
of course, thanks.set -e
only helps here with pipefail, but I don't think they want that at all.
â ilkkachu
Jan 23 at 20:51
so i did the echo $PIPESTATUS[@] and this is how it looks like: 0 0 0 0 0 0 0 0 0 0 so basically is always 0 even if is not copying any file because mdfind didn't found (in my case i have an input file with 5 files to look for and the last file doesn't exist on my computer so cant be find and/or copied)
â WalterC85
Jan 23 at 22:15
You can also
set -o pipefail
and set -e
.â DopeGhoti
Jan 23 at 20:47
You can also
set -o pipefail
and set -e
.â DopeGhoti
Jan 23 at 20:47
@DopeGhoti, ah
pipefail
of course, thanks. set -e
only helps here with pipefail, but I don't think they want that at all.â ilkkachu
Jan 23 at 20:51
@DopeGhoti, ah
pipefail
of course, thanks. set -e
only helps here with pipefail, but I don't think they want that at all.â ilkkachu
Jan 23 at 20:51
so i did the echo $PIPESTATUS[@] and this is how it looks like: 0 0 0 0 0 0 0 0 0 0 so basically is always 0 even if is not copying any file because mdfind didn't found (in my case i have an input file with 5 files to look for and the last file doesn't exist on my computer so cant be find and/or copied)
â WalterC85
Jan 23 at 22:15
so i did the echo $PIPESTATUS[@] and this is how it looks like: 0 0 0 0 0 0 0 0 0 0 so basically is always 0 even if is not copying any file because mdfind didn't found (in my case i have an input file with 5 files to look for and the last file doesn't exist on my computer so cant be find and/or copied)
â WalterC85
Jan 23 at 22:15
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f419151%2fhow-to-redirect-mdfind-output-to-if-else-statament-in-bash%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
This is because
$?
is getting the exit code ofxargs
, not ofcp
ormdfind
.â DopeGhoti
Jan 23 at 18:25