6

I understand this has been asked before but the answer don't quite give me what I need. I pgrep for a given string which returns a list of PID's containing that string in a variable ($testpid in this case). I then try and split each one of the PID's out, they are sepereated with a space like so:

PIDS:

17717 172132 2138213

Code:

IFS=" " read -a pidarray <<< "$testpid"
echo pidarray[0]

*instead of the echo above i would be assigning each item in the array to its own variable

But I get the following error:

syntax error: redirection unexpected
5
  • 4
    That "redirection unexpected" is what you would get if your shell were not in fact bash. Commented Dec 16, 2014 at 14:13
  • Your question and the title both seem to indicate that your problem was splitting the PIDs into multiple variables, but you've accepted an answer that deals with a different problem. If this is in fact what you wanted, please consider rephrasing the title and question accordingly. Commented Dec 16, 2014 at 14:44
  • @TTT, an excellent point -- I quite glossed over the note that the actual intent was then to assign the array elements to their own variables; there's no need to use an array at all in that case. Answer amended appropriately. Commented Dec 16, 2014 at 15:34
  • @CharlesDuffy Do you think that is, indeed what he meant? I had begun to answer that question, when I saw he marked your original answer as complete. Regardless, your answer solves both now! Commented Dec 16, 2014 at 17:19
  • @TTT, the "instead of the echo above i would be assigning each item in the array to its own variable" makes it fairly clear. Commented Dec 16, 2014 at 18:18

2 Answers 2

5

Your syntax was almost correct:

IFS=' ' read -r -a pidarray <<< "$testpid"
echo "${pidarray[0]}"

Note the curly braces needed for the array dereference.

More importantly, check that your shebang is #!/bin/bash, or that you executed your script with bash yourscript, not sh yourscript. The error given is consistent with a shell that doesn't recognize <<< as valid syntax -- which any remotely modern bash always will when invoked as bash; even if /bin/sh points to the same executable, it tells bash to disable many of its extensions from the POSIX spec.


Now, that said -- if your goal is to assign each entry to its own variable, you don't need (and shouldn't use) read -a at all!

Instead:

IFS=' ' read -r first second third rest <<<"$testpid"
printf '%s\n' "First PID is $first" "Second PID is $second"
Sign up to request clarification or add additional context in comments.

4 Comments

Also, /bin/sh doesn't support reading array, only :read [-p prompt] [-r] variable [...]
@jujj, indeed; sh doesn't support arrays at all, but we don't get to that if we're hitting a syntax error before invoking read, so I didn't consider it worth mentioning.
And if you have a variable number of arguments coming in, you can handle them via looping and the eval command, e.g. as for i in {1..3}; eval "var$i=$i"; done. Though, I don't know why you'd want to get away from an array in such an instance, anyway.
@TTT, there are much better ways to do indirect assignments than eval. For instance, printf -v "var$i" %s "$i". See 1.3.3 of mywiki.wooledge.org/BashFAQ/006 for more (and mywiki.wooledge.org/BashFAQ/048 for notes on the eval-considered-harmful premise).
1

You can try this one.

testpid="17717 172132 2138213"
set -- $testpid
echo -e $1 $2

After that use the $1,$2,$3 to get that separately.

8 Comments

What does that do that set -- $testpid would not, without all the overhead of the subshell?
To be clear -- I think that if modified to fix the inefficiency (and, maybe, with more explicit note of the side effects -- since it does overwrite the argument list) this is a good answer.
...though it also has a problem in the general case of globbing -- the read -r -a approach will put a * into an array element as a literal, whereas this will replace it with a list of filenames in the current directory. Not a problem with PIDs, but maybe a problem elsewhere.
Much better. I still might use -- before the $testpid to prevent arguments that start with dashes from being treated as shell options -- what if $1 were -e? -- but that's a relatively minor quibble. (Similar quibbles apply to quoting expansions on arguments to the echo).
Sorry I missed that one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.