1

I just started learning bash and have a few scripts working together fine from the command line and via udev.

However I just installed AT so I could get around some udev limitations.

It's taken me forever to first notice that AT is very obvious about using sh - not bash, and that was causing my source command to fail. I since replaced it with the more portable ., that should work with all of the shells.

But now I get a sh syntax error when I declare variables using brackets to surround sourced variables that have double quotes in them.

I'd like to fix this syntax error in sh "(" unexpected - and if someone could suggest an alternative that works for both sh and bash, that would be great. Otherwise a sh fix would at least get me going again.

These are the few lines in the working bash script:

#!/bin/bash
#
# inlinetest.sh
#
CONFIG_FILE=/home/pi/autostart/autostart-settings.cfg

# Check if file exists
if [ ! -f "$CONFIG_FILE" ]; then
    exit 1
else
   # process stream settings - source is only in bash
   # use . for sh and bash shell functionality (AT uses sh)
  . "$CONFIG_FILE"
fi

# Function to wrap arecord and avconv into one command
pipe_cmd() {
   sudo nohup sudo /home/pi/aplay/arecord2 -f cd -D plughw:1,0 -q | /usr/bin/avconv "$@" >/dev/null 2>&1 &
} 

stream_parameters=(-ice_name "$icecast_show" -f mp3)   
icecast_setup="icecast://$icecast_user:$icecast_password@$icecast_server:$icecast_port$icecast_mount_url"
stream_args=(-re -i - -c:a libmp3lame -ac 2 -ar 44100 -content_type audio/mpeg -loglevel quiet -b:a "$stream_bitrate" -legacy_icecast "$icecast_legacy") 

pipe_cmd "${stream_args[@]}" "${stream_parameters[@]}" "$icecast_setup"

Syntax error occurs on this line:

stream_parameters=(-ice_name "$icecast_show" -f mp3)

and will probably do same thing on this one:

stream_args=(-re -i - -c:a libmp3lame -ac 2 -ar 44100 -content_type audio/mpeg -loglevel quiet -b:a "$stream_bitrate" -legacy_icecast "$icecast_legacy") 

EDIT

This message is returned by ShellCheck when I change the shell to /bin/sh

In POSIX sh, arrays are undefined
dbmitch
  • 5,361
  • 4
  • 24
  • 38
  • It works fine in bash. The issue is when using sh shell. The syntax error gets described in ShellCheck as `In POSIX sh, arrays are undefined` if that helps anyone – dbmitch Jul 14 '16 at 05:18
  • This should have a bash tag - It is bash code and I'd like it to work in bash after it's fixed to work in sh. – dbmitch Jul 14 '16 at 05:41

1 Answers1

2

sh does not have arrays (or, rather, it has only one), and stream_parameters=(-ice_name "$icecast_show" -f mp3) is a syntax error. If you want to use the one array that you have available, you can do something like

set -- -ice_name "$icecast_show" -f mp3   
icecast_setup="icecast://$icecast_user:$icecast_password@$icecast_server:$icecast_port$icecast_mount_url"
set -- "$@" -re -i - -c:a libmp3lame -ac 2 -ar 44100 -content_type audio/mpeg -loglevel quiet -b:a "$stream_bitrate" -legacy_icecast "$icecast_legacy" 


pipe_cmd "$@"  "$icecast_setup"
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • so what's happening here? The first `set` defines the "one array" and the second `set` appends to it? That should work in bash as well? – dbmitch Jul 14 '16 at 05:40
  • Which array does `sh` have? – Jonathan Leffler Jul 14 '16 at 05:52
  • 1
    @Jonathan I'm referring to the array of positional parameters. (I'm not sure it's technically an array, but you can reference it with "$@" as if it is.) @dbmitch; yes, the first sets assigns the positional parameters, and the second one appends to it. – William Pursell Jul 14 '16 at 05:55
  • OK — stretching it a bit, but … well, it isn't really an array because you can't use subscripts on it. However, that explains your thought. Looking at Bash's [POSIX mode](https://www.gnu.org/software/bash/manual/bash.html#Bash-POSIX-Mode), I don't see a prohibition on arrays in there. And `/bin/sh` (for Bash 3.2) on Mac OS X allows `x=(a b c)` and `echo "${x[@]}"`. OTOH, maybe `/bin/sh` is something other than `bash` — perhaps `dash` — on the OP's machine. – Jonathan Leffler Jul 14 '16 at 05:58