There are a number of mistakes here. Let me go through them in order:
RECORD = $1
Spaces are not allowed around the =
in shell assignments, so this (and a number of later assignments) won't work. What this actually does is try to run a command named RECORD
with "=" and the value of $1
as arguments. In the shell spaces are very significant; in most places they're either completely required or completely forbidden. Until you learn where they're optional, be very careful to copy the spaces exactly from any examples you're working from.
echo "$RECORD"|read var1 var 2 var3
Elements of a pipeline (such as the read
command here) run in subshells, which means that variable assignments made in them happen in the subshell... and are lost when the subshell exits. Also, you can't have a space in the middle of a variable name (var 2
).
You can do this without the pipe like this:
read var1 var2 var3 <<<"$RECORD"
(Although note that <<<
is a bashism, and not available in all shells. Be sure to start your script with #!/bin/bash
NOT #!/bin/sh
.)
var1=`echo $var1`
var2 =`echo $var2`
var3=`echo $var3`
Even after removing the stray space from the second line, these don't do anything useful. They take the values of the variables, echo them, capture the output from the echo commands, and stuff them right back into the variables. There are some changes that might happen along the way (like wildcard expansion), but I'm pretty sure they're not things you want to have happen. (Actually, you should almost always put variable references in double-quotes to prevent things like unexpected wildcard expansion and word splitting.) So just lose these.
export var1 var2 var3
export
tells the shell to export the value of those variables to the commands you run from this shell -- that is, they get exported down the process hierarchy, not up to whatever ran this. But in this case, the function is running in the same process as the main part of the script, so there's no importing needed to share variables in either direction. As long as the variables aren't declared local
, they're automatically available to the rest of the script. So, lose this as well.
(Note that there is no way to export variables up the process hierarchy. What happens in a subshell or other subprocess stays in that subprocess.)
function "EACH_RECORD"
You need a $
in front of EACH_RECORD
, or it'll just be passed as a literal string.
BTW, I strongly recommend running your scripts through shellcheck.net -- it'll point out many basic mistakes before they get you into trouble.