Syntax error: invalid arithmetic operator (error token is ".")

kitsune

I have to search word occurrences in a plain text file using a script. The script that I wrote is:

#!/bin/bash

if [ $# -ne 1 ]
then
        echo "inserire il file che si vuole analizzare: "
        read
        fileIn=$REPLY
else
        fileIn=$1
fi

echo "analisi di $fileIn"

for parola in $( cat $fileIn )
do
        freq=${vet[$parola]}
        vet[$parola]=$(( freq+1 ))
done

for parola in ${!vet[*]}
do
        echo $parola ${vet[$parola]}
done

unset array

But when I run it I get this error:

./script02.sh: line 16: qua.: syntax error: invalid arithmetic operator (error token is ".")

What's wrong and how do I fix it? Thanks.

konsolebox

In the first attempt, you don't get a value from ${vet[$parola]}. This causes the syntax error for the arithmethic operation Use a default value instead:

for parola in $(cat "$fileIn")
do
    freq=${vet[$parola]}
    [[ -z $freq ]] && freq=0
    vet[$parola]=$(( freq + 1 ))
done

You might also need to declare your array as associative if you want to store non-integer keys:

declare -A vet

for ...

Suggestion:

#!/bin/bash

if [[ $# -ne 1 ]]; then
    read -p "inserire il file che si vuole analizzare: " fileIn
else
    fileIn=$1
fi

echo "analisi di $fileIn"

declare -A vet  ## If you intend to use associative arrays. Would still work in any way.

for parola in $(<"$fileIn"); do
    freq=${vet[$parola]}
    if [[ -n $freq ]]; then
        vet[$parola]=$(( freq + 1 ))
    else
        vet[$parola]=1
    fi
done

for parola in "${!vet[@]}"; do
    echo "$parola ${vet[$parola]}"
done

unset vet  ## Optional

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Syntax error using =~ operator in bash

分類Dev

ternary operator syntax error in class

分類Dev

syntax error near unexpected token `<'

分類Dev

Syntax error near unexpected token '{'

分類Dev

Syntax error near unexpected token `then'

分類Dev

syntax error: operand expected (error token is " ")

分類Dev

Invalid syntax using += operator

分類Dev

Cron syntax error near unexpected token

分類Dev

GNU Bison: Syntax Error, unexpected <token>

分類Dev

bash: syntax error near unexpected token

分類Dev

bash: syntax error near unexpected token '<'

分類Dev

Token refresh fails with invalid_client error

分類Dev

Self in Python Syntax Error: Invalid Syntax, String's Attributes Inexisting?

分類Dev

plot.lm Error: $ operator is invalid for atomic vectors

分類Dev

Getting RE error: repetition-operator operand invalid on osx sed

分類Dev

React-redux Spread operator in reducer returning error "unexpected token"

分類Dev

Linux Kernel error: missing binary operator before token "("

分類Dev

jupyter notebook python version 2.7.13 (print invalid syntax error)

分類Dev

syntax error near unexpected token `(' (possible bash bug)

分類Dev

syntax error near unexpected token `<' for shell script block in Jenkinsfile

分類Dev

Syntax error near unexpected token when installing Yara

分類Dev

Getting "syntax error near unexpected token `fi'" in my bash script

分類Dev

Syntax Error near Unexpected Token in a bash function definition

分類Dev

Syntax error near unexpected token 'fi', bash script

分類Dev

syntax error near unexpected token `>' in script run by cron

分類Dev

Circumstances of the "invalid_grant" error when refreshing an access token?

分類Dev

Syntax error on token(s) "product1. "VariableDeclaratorId expected after this token

分類Dev

{"Error":"Invalid JSON syntax at offset 2"} - receiving this error while trying to get everflow report

分類Dev

bundling error: syntax error Unexpected token while using React-native-android-wifi

Related 関連記事

  1. 1

    Syntax error using =~ operator in bash

  2. 2

    ternary operator syntax error in class

  3. 3

    syntax error near unexpected token `<'

  4. 4

    Syntax error near unexpected token '{'

  5. 5

    Syntax error near unexpected token `then'

  6. 6

    syntax error: operand expected (error token is " ")

  7. 7

    Invalid syntax using += operator

  8. 8

    Cron syntax error near unexpected token

  9. 9

    GNU Bison: Syntax Error, unexpected <token>

  10. 10

    bash: syntax error near unexpected token

  11. 11

    bash: syntax error near unexpected token '<'

  12. 12

    Token refresh fails with invalid_client error

  13. 13

    Self in Python Syntax Error: Invalid Syntax, String's Attributes Inexisting?

  14. 14

    plot.lm Error: $ operator is invalid for atomic vectors

  15. 15

    Getting RE error: repetition-operator operand invalid on osx sed

  16. 16

    React-redux Spread operator in reducer returning error "unexpected token"

  17. 17

    Linux Kernel error: missing binary operator before token "("

  18. 18

    jupyter notebook python version 2.7.13 (print invalid syntax error)

  19. 19

    syntax error near unexpected token `(' (possible bash bug)

  20. 20

    syntax error near unexpected token `<' for shell script block in Jenkinsfile

  21. 21

    Syntax error near unexpected token when installing Yara

  22. 22

    Getting "syntax error near unexpected token `fi'" in my bash script

  23. 23

    Syntax Error near Unexpected Token in a bash function definition

  24. 24

    Syntax error near unexpected token 'fi', bash script

  25. 25

    syntax error near unexpected token `>' in script run by cron

  26. 26

    Circumstances of the "invalid_grant" error when refreshing an access token?

  27. 27

    Syntax error on token(s) "product1. "VariableDeclaratorId expected after this token

  28. 28

    {"Error":"Invalid JSON syntax at offset 2"} - receiving this error while trying to get everflow report

  29. 29

    bundling error: syntax error Unexpected token while using React-native-android-wifi

ホットタグ

アーカイブ