Open the way
Open the way

Reputation: 27329

Bash directory variable error

In a bash script I get to this point

read ENE CX CY CZ <<< $(head -n 1 RESULTS_${lach}tal2)
echo $ENE
SED_ARG="-e 's/-/m/g'"
read CX2 <<< $( echo ${CX} | eval sed "$SED_ARG")
read CY2 <<< $( echo ${CY} | eval sed "$SED_ARG")
read CZ2 <<< $( echo ${CZ} | eval sed "$SED_ARG")
DIREC="${CX2}_${CY2}_${CZ2}"
echo $DIREC
cd "$DIREC"

the value of variable DIREC is the name of a directory and I get things like

m25.1240_m22.1250_m5.1540

this directory does exist, and if I do directly in bash cd m25.1240_m22.1250_m5.1540 it works and I can get inside. But on the script it does not work and I get the error:

: No such file or directory: cd: m25.1240_m22.1250_m5.1540

I do not understand why the error

PS:

echo "$DIREC" | od -c

gives

0000000   m   2   5   .   1   2   4   0   _   m   2   2   .   1   2   5
0000020   0   _   m   5   .   1   5   4   0  \r  \n
0000033

Upvotes: 2

Views: 240

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

Does your RESULTS_${lach}tal2 file have windows-style line endings? Does CZ end with a carriage return? What does this show:

echo "$DIREC" | od -c

Additionally, there's a lot of unnecessary eval'ing going on. Bash can do replacements in variable substitution:

read ENE CX CY CZ <<< $(head -n 1 RESULTS_${lach}tal2 | sed 's/\r$//')
DIREC="${CX/-/m}_${CY/-/m}_${CZ/-/m}"

Upvotes: 1

Hai Vu
Hai Vu

Reputation: 40723

I suspect that inside the script, your working directory is elsewhere, thus you cannot cd. Try this: instead of

cd "$DIREC"

replace it with

echo current directory is $PWD
cd "m25.1240_m22.1250_m5.1540"

and see if you still have the same problem.

Upvotes: 1

Related Questions