silvio
silvio

Reputation: 85

can't get stdout to a variable

it seems silly... I don't know what I am doing wrong....

I need to change the .txt files encoding to utf-8. here the script:

#!/bin/bash
for NOME_ARQ in *.txt do

##1 - verify file encoding and store in CODIFICACAO
CODIFICACAO=$(file --mime-encoding "$NOME_ARQ" | cut -d" " -f2)

##2 - convert to utf-8
iconv -f "$CODIFICACAO" -t utf-8 "$NOME_ARQ" -o temp01.txt

done    

Have tried several ways for the ##1, but always get the following error:

sc-sini2csv: line 5: sintax error near `token' unexpected `CODIFICACAO=$(file --mime-encoding "$NOME_ARQ" | cut -d" " -f2)'
sc-sini2csv: line 5: `CODIFICACAO=$(file --mime-encoding "$NOME_ARQ" | cut -d" " -f2)'

We see from the error, that the problem occur when assigning the variable $CODIFICACAO

As far as I've looked around there are 2 ways of assigning the STDOUT to a variable:

1- using backtick:

CODIFICACAO=`file --mime-encoding "$NOME_ARQ" | cut -d" " -f2`

or

2- using $():

CODIFICACAO=$(file --mime-encoding "$NOME_ARQ" | cut -d" " -f2)

Both of them will give the same error.

As I wrote, it seems silly, but I'm stucked on this error..... any hel will be much appreciated !!!

PS: using $() or backticks directly from terminal (outside a bash script) will work.... but i need it into a shell script.

In time: I'm using ubuntu with bash shell

Thanks in advance !!! Silvio

Upvotes: 1

Views: 89

Answers (2)

Adrian Pronk
Adrian Pronk

Reputation: 13906

do should be on a new line:

for NOME_ARQ in *.txt
  do

Upvotes: 1

William Pursell
William Pursell

Reputation: 212208

You are missing a semi-colon:

for NOME_ARQ in *.txt; do

Upvotes: 4

Related Questions