awk syntax error in bash. Works fine in zsh

awk syntax error colon
awk syntax error period
awk syntax error at source line 1
awk examples
bash awk
awk: bailing out near line 1
awk illegal redirection near line 1
awk 0602-502 the statement cannot be correctly parsed the source line is 1

I have written the following script that extracts a number from an rss file.

#!/bin/sh
wget -O selic https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros
line=$(grep 'dailyratevalue' selic)
index=$(awk -v var=$line 'BEGIN {print index(var, "dailyratevalue") }')
end=$((index+21))
echo $line | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic

In zsh it works perfectly, but i need it to work in bash, too. I have tried running it on bash but i get the following error

awk: cmd. line:1: <content
awk: cmd. line:1: ^ syntax error

The error pattern <content comes from the line that is being fed as a parameter to awk, which makes no sense to me, since awk is just supposed to get me the position of the pattern i want.

What could this be?

@DiegoTorresMilano's answer is probably better overall, but if you want to do it in bash, the main thing you need to do is double-quote your variable references. Without double-quotes around them, bash (and most shells other than zsh) splits variables into "words", and also expands anything that looks like a wildcard expression into a list of matching filenames. You almost never want this, so use double-quotes. In your case, there are two places they're needed: around $line here:

index=$(awk -v var="$line" 'BEGIN {print index(var, "dailyratevalue") }')

and here:

echo "$line" | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic

Note that you don't need double-quotes around the $( ) expressions, because they're on the right side of an assignment statement, and that isn't subject to word splitting and wildcard expansion. If they occurred elsewhere, you'd probably want double-quotes around them too.

BTW, shellcheck.net is really good at pointing out common mistakes like this, so I recommend running your scripts through it (even when they seem to be working correctly).

awk: line 1: syntax error at or near >, The reason you get a syntax error in your Awk script is because when $getPrice is empty, then the script is actually just. BEGIN{if (>0) exit 1}. As you can see, one may pass variables to awk script using `-v' option. I'd like to dissuade you from writing awk programs in C (as well as C++ programs in C etc) IMHO, each language has it own preferable thought patterns, and you should learn them, not only the syntax.

index=$(awk -v var="$line" 'BEGIN {print index(var, "dailyratevalue") }')

should fix it.

awk syntax error in bash. Works fine in zsh, awk syntax error in bash. Works fine in zsh. I have written the following script that extracts a number from an rss file. #!/bin/sh wget -O selic  If those shells are not available, you can install them or alternatively, translate that script to sh syntax which should be straightforward here. The sh language (both Bourne or POSIX) has no <() operator. That comes from ksh and is supported by bash and zsh as well. echo -e is non-standard and even with bash and ksh only works in some

awk can do all of the extra steps. You can just

wget -qO - https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros | \
    awk -F '&[gl]t;' '/dailyratevalue/ {sub(",", ".", $25); print $25;}'

and obtain the value you want.

This is setting the FS and getting the field you want for the line that matches dailyratevalue.

Advanced Bash-Scripting Guide, The syntax is simple -- even austere -- similar to that of invoking and chaining Prints error message #+ if one or more of essential environmental variables not set. Well-behaved UNIX commands, programs, and utilities return a 0 exit code this script with ksh or zsh -y. exit 0 # This example script by Stephane Chazelas​,  Rather than creating a new issue I'll add my 2c here. If you launch bash.exe from C:\Windows\sysnative\bash.exe rather than the name of an installed distro (eg: Ubuntu.exe from C:\Users\<user>\AppData\Local\Microsoft\WindowsApps\ubuntu.exe) the shell specified in /etc/passwd isn't honored when the distro launches.

command line, git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) The <() operator you are using is a bash feature and isn't available in the sh script.sh: line 1: syntax error near unexpected token `<' . on Ubuntu as well, but next time, please ask on http://linux.stackexchange.com instead. That isn't what I meant, and quotes are fine to use. My point was, is your answer works. The other answers here work. I tested yours and it works fine, and I gave you +1. Someone else downvoted you on your answer that works fine. All of this really is optional. I was just letting you know about your downvote is all. – Terrance Jun 12 '15 at 14:06

scripts - Loop variable error in for loop, for(( i = 0; i<=5; i++)) is Bash specific and doesn't work with plain Bourne shell ( /​bin/sh ). If you remove the shebang the script is run by your  Bash vs zsh: Common features between bash and zsh. For the most part, bash vs zsh share many convenient features that qualify both as highly efficient shells. The z-command. One highly useful feature that both share is the z command, which allows developers to essentially keep track of their directories.

Get Current User in Shell Scripts on macOS – Scripting OS X, loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! While zsh is a great replacement for bash for interactive terminal use and scripting on the And while bash in sh compatibility mode will recreate the odd syntax and quirks of sh for Syntax error: redirection unexpected. SHELLNAME is zsh if the shell is zsh and bash if the shell is bash. Each of the above functions contains code that works with zsh, but not bash. When I source this file, I receive the following errors.

Comments
  • Using grep to find a line number to pass to Awk is completely useless. Awk can match regular expressions just fine on its own, and the line number itself doesn't appear to add any intrinsic value here.
  • @RCS : Parameter passing works differently in zsh and bash. In your case, you could double-quote "$line", but in general, it does not make much sense to construct a program which will work in zsh AND bash alike - there are too many differences. Note also that if you want to run a script in bash or zsh, it is dangerous to put /bin/sh in the #! line.
  • That worked! But my output is not the desired one. Somehow the position of characters is different on bash and i ended up with junk instead of the number i need.
  • I am getting 249 for $index with both bash and zsh. What's yours?
  • 249, too. The characters outputted to the file are different, tho. I get the 3 chars immediately before the number i need.