0

I'm new to bash script, it is interesting, but somehow I'm struggling with everything.

I have a file, separated by tab "\t" with 2 infos : a string and a number. I'd like to use both info on each line into a bash script in order to look into a file for those infos. I'm not even there, I'm struggling to give the arguments from the two columns as two arguments for bash.

#/!bin/bash
FILE="${1}"
while read -r line
do
READ_ID_WH= "echo ${line} | cut -f 1"
POS_HOTSPOT= echo '${line} | cut  -f 2'
echo "read id is : ${READ_ID_WH} with position ${POS_HOTSPOT}"
done < ${FILE}

and my file is :

ABCD\t1120
ABC\t1121

I'm launching my command with

./script.sh file_of_data.tsv

What I finally get is :

script.sh: line 8: echo ABCD 1120 | cut -f 1: command not found

I tried a lot of possibilities by browsing SO, and I can't make it to divide my line into two arguments to be used separately in my script :(

Hope you can help me :)

Best,

H. Vilbert
  • 119
  • 6

2 Answers2

1

The quotes cause the shell to look for a command whose name is the string between the quotes.

Apparently you are looking for

while IFS=$'\t' read -r id hotspot; do
    echo "read id is: $id with position $hotspot"
done <"$1"

You generally want to avoid capturing things into variables you only use once, but the syntax for that is

id=$(echo "$line" | cut -f1)

See also Correct Bash and shell script variable capitalization and When to wrap quotes around a shell variable?. You can never have whitespace on either side of the = assignment operator (or rather, incorrect whitespace changes the semantics to something you probably don't want).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you so much, you save my evening. I have a lot of issues getting the "philosophy" about bash scripting for reasons, would you recommend a tutorial/course in particular? (I don't have an informatic background) best – H. Vilbert Feb 20 '20 at 18:11
  • The [Stack Overflow `bash` tag info page](/tags/bash/info) has a brief reading list. – tripleee Feb 20 '20 at 20:19
0

You have a space after the equals sign on lines 5 and 6, so it thinks you are looking for an executable file named echo ABCD 1120 | cut -f 1 and asking to execute it while assigning the variable READ_ID_WH to the empty string, rather than assigning the string you want to the variable.

Dwight Guth
  • 759
  • 4
  • 11