This issue is related to Using bash shell function inside AWK
I have this code
#!/bin/bash
function emotion() {
#here is function code end with return value...
echo $1
}
export -f emotion
#I've put all animals in array
animalList=($(awk '{print $1}' animal.csv))
#loop array and grep all the lines form the file
for j in ${animalList[@]}
do
: #here I'am running a bash script calling emotion function
grep $j animal.csv | awk '{for(i=2;i<=NF;i++){system("bash -c '\''emotion "$i"'\''")}}'
done
and I have this file:
cat smile happy laugh
dog angry sad
mouse happy
wolf sad cry
fox sleep quiet
The output should like this:
smile
happy
laugh
angry
sad
happy
sad
cry
sleep
quiet
The issue it tells me bash: emotion: command not found
According to akarilimano's comment here
this is not working on my Ubuntu 16.04. This is strange, because it used to work "on Ubuntu 14.04.
So how to do it in newer versions?
Best Answer
That's probably not the best way to approach the problem.
From
awk
, all you can do is build a command line thatsystem()
passes tosh
. So, you need the arguments to be formatted in thesh
syntax.So you'd need:
Here quoting awk's
$1
so it can be safely embedded in thesh
command line that ends up runningbash
with the content of$1
as last argument, which then passes it toemotion
.That assumes your
sh
and yourawk
don't strip the special environment variables thatbash
uses to export functions (likepdksh
and derivatives (such asmksh
) do for instance, ordash
since 0.5.8 which explains your 14.04 vs 16.04 issue), and that your distribution has not disabled exported functions inbash
.If it does, you could do it like for
ksh
/zsh
, and pass the definition of the function some other way, like:In both cases, that means running one sh and one bash for it. Maybe you can pass the
$i
tobash
some other way than via asystem()
that executes two instances of a shell each time. Like:Or do the word splitting in
bash
directly: