2

If I run

mapfile -t array_files < <(ssh -i key user@host "find / -name search -type f")

and print the results in $?, I receive 0 even when the command fail.

Flavio Milan
  • 467
  • 3
  • 16
  • the result of find... this command works when the user and host is correct, but when fail I receive 0 in $? – Flavio Milan Aug 07 '19 at 19:40
  • 2
    `$?` contains the exit status of `mapfile`, not `ssh`. Also, what do you mean by "fail"? `find` doesn't consider it failure to find 0 files. – chepner Aug 07 '19 at 19:42
  • 1
    If you want to know if it found anything, check the length of `$array_files`. – Barmar Aug 07 '19 at 19:44

1 Answers1

4

With bash.

# disable job control and enable lastpipe to run mapfile in current environment
set +m; shopt -s lastpipe

# feed array_files with the output of your ssh/find command
ssh ... | mapfile -t array_files

# returncode of first command in pipe (here ssh)
echo "${PIPESTATUS[0]}"

# content of array array_files
declare -p array_files

In a script job control is disabled by default.

Cyrus
  • 84,225
  • 14
  • 89
  • 153