0

The script below echoes the lines with 'me'. But myarray=($line) fails when uncommented with syntax error: got (, expecting done What's up?

#!/bin/bash
filename='/u/me/deltmp.lst'
ls -la /tmp | grep me > $filename
while read line
do
echo $line
#  myarray=($line)
#  echo "My array: ${#myarray[@]}"
done < $filename
  • 2
    How are you running the script? – jordanm Aug 17 '21 at 15:08
  • With bash you can read directly into an array with `read -a` – jordanm Aug 17 '21 at 15:10
  • 1
    [Don't use `ls`](http://mywiki.wooledge.org/ParsingLs) or a temporary file for this anyway. `for line in /tmp/*me*; do` or if you insist on an array (which is just a waste of memory in this example) `myarray=( /tmp/*me* )` – tripleee Aug 17 '21 at 15:17
  • 1
    That error message looks like it's running under dash, not bash (although the exact error message I get is slightly different; are you reporting the *exact* error message you get? Read [this](https://meta.stackoverflow.com/questions/359146/why-should-i-post-complete-errors-why-isnt-the-message-itself-enough)). Are you running the script with `sh scriptname` or something like that? – Gordon Davisson Aug 17 '21 at 15:23
  • These are all great. I'll be playing with them. – Papa Dilbert Aug 17 '21 at 15:26
  • tripleee's hint of difference between sh & bash gave me the clue. I was executing the script from a sh (bourne) shell assuming that the declaration of the #!/bin/bash would suffice. Only done because I couldn't figure out how to parse into an array with bourne. Switching to a bash shell and then executing the script worked. So maybe the problem is the #!/bin/bash is not getting me to a bash shell. Y'all are awesome. Thanks! – Papa Dilbert Aug 17 '21 at 15:35
  • On my system, IBM mainframe z/OS USS, I found that bash is not in /bin. In fact, it's hiding in /usr/local/bin. So the solution is to modify the the #!/usr/local/bin/bash. Or, learn how to parse into an array using Bourne. Thanks again!! – Papa Dilbert Aug 17 '21 at 16:04
  • 1
    @PapaDilbert, some folks use `#!/usr/bin/env bash` (me included) as the shebang, to work around cases like yours. – Jetchisel Aug 17 '21 at 16:24
  • @PapaDilbert, how's the program actually being run? `sh yourscript` ignores the shebang and forces `sh`; `./yourscript` should always honor the shebang, and fail to run at all if the shebang is invalid. – Charles Duffy Aug 17 '21 at 16:42
  • POSIX sh doesn't have any such thing as an array, so "learn how to parse into an array using POSIX sh" (there is no Bourne on modern systems -- Bourne is a shell from the 1970s) isn't possible. – Charles Duffy Aug 17 '21 at 16:44
  • BTW, if you want to satisfy yourself that your copy of `sh` isn't actually Bourne, try running `echo hello ^ cat`. On 1970s Bourne, `^` sets up a pipe, so the output is just `hello`; on 1990s POSIX sh, `^` is just another character, so the output is `hello ^ cat`. "Bourne again" is a fun play on words, but bash isn't _actually_ compatible with Bourne; it's (almost completely) compatible with POSIX sh. – Charles Duffy Aug 17 '21 at 16:45

0 Answers0