Reputation: 719
I'm trying to append the result of gawk
to the respective file, with a for
loop to all the files in the directory baselines:
#! /bin/sh
for file in baselines/*; do numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }') echo "Number of Threads: $numThreads" >> $file; done
But the numThreads
variable is not assigned to anything, so it's appending "Number of Threads: " to the file.
The folder baselines contains files like is.A.2.baseline
, is.A.4.baseline
and is.A.8.baseline
, so I'm expecting to append "Number of Threads: 2", "Number of Threads: 4" and "Number of Threads: 8" respectively...
Upvotes: 0
Views: 125
Reputation: 754650
Your problem is a missing newline (or, in extremis, semicolon). Your script is:
for file in baselines/*; do numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }') echo "Number of Threads: $numThreads" >> $file; done
You need a newline or semicolon before the echo "Number of Threads..."
part. Like this for readability:
for file in baselines/*
do
numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }')
echo "Number of Threads: $numThreads" >> $file
done
Like this if you insist on writing unreadable code:
for file in baselines/*; do numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }'); echo "Number of Threads: $numThreads" >> $file; done
but please don't send code like that to Stack Overflow for review/revision again because it is hard to read and people won't bother to answer questions where it is hard to read the code.
Incidentally, what your code did is quite subtly wrong. It ran the echo
command with the environment variable numThreads
set to the correct value, but the expansion of the argument list occurred before that so that numThreads
was empty as the "Number of Threads: $numThreads"
string was evaluated. One way to demonstrate that is:
for file in baselines/*
do
numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }') env | grep numThreads
done
which will print the value of the environment variable. Another way to demonstrate it would be:
for file in baselines/*
do
numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }') sh -c 'echo numThreads=$numThreads'
done
Upvotes: 1
Reputation: 782107
Do the variable assignment and echo in separate statements:
for file in baselines/*; do
numThreads=$(echo $file | gawk 'BEGIN { FS = "." } ; {print $3 }')
echo "Number of Threads: $numThreads" >> $file
done
Variable expansion happens before executing anything, so when you have them on one line the variable is expanded before it's assigned. That syntax should only be used when you're temporarily setting an environment variable that will be accessed in a child process, not when setting a shell variable for the current process.
Upvotes: 1