Issue
My below command throwing error like : ksh: NF: not found.
sshpass -p 'pass' ssh -o StrictHostKeyChecking=no user awk -v columnCount=64 -F '\\|' 'NF && NR > 1 && NR < 12058 && NF != columnCount+1 {exit 1}' /var/prod/scms/landing/store_data_20201202010002.dat && echo 'success' || echo 'failed'
Can anyone please help me here, what I have done wrong.
Solution
Original code is:
sshpass -p 'pass' ssh -o StrictHostKeyChecking=no user awk -v columnCount=64 -F '\\|' 'NF && NR > 1 && NR < 12058 && NF != columnCount+1 {exit 1}' /var/prod/scms/landing/store_data_20201202010002.dat && echo 'success' || echo 'failed'
Relevant part is: ssh user awk -F '\\|' 'NF && NR > 1'
- The local shell strips the single-quotes (while parsing the commandline) and so ssh never sees them.
- ssh concatenates its arguments into a single string and passes it to the remote shell.
- The remote shell receives from ssh:
awk -F \\| NF && NR > 1
- This is a pipeline (
|
) followed by a control command (&&
) - Therefore the remote shell tries to run
awk
andNF
in parallel, withawk
stdout connected toNF
stdin. - No program called
NF
is found so the remote shell throws the error seen. - Because the pipeline fails, the command after the
&&
is not executed and so the redirection (> 1
) does not happen and no attempt is made to run a commandNR
Answered By - jhnc