2

Hello I'm a total beginner in shell scripting.

I have a log file named logA.log and a B.sh file.

In log file there are some lines and I want to find the number of a spesific word in that log (in last 10 line) by executing the B.sh

In B I wrote

#!/bin/bash

variableString = tail -10f /home/appuser/logA.log

grep ERROR $variableString | wc -l

but the output is:

variableString: command not found

I know "grep" line is working but I cannot reach the logA in b.sh.

How can I define a variable called variableString as last 10 line of logA

Ignacio Correia
  • 3,611
  • 8
  • 39
  • 68
abidinberkay
  • 1,857
  • 4
  • 36
  • 68

2 Answers2

2

Your commands are ok but you have to be aware of the way to store command output: var=$(command). Also, you may get several lines, so quote the return command to keep the format. Hence, you should use:

variableString="$(tail -10f /home/appuser/logA.log)"
grep ERROR "$variableString" | wc -l

When you get the error

variableString: command not found

it is because as you define your syntax, bash interprets that has to execute the variableString command with the = tail -10f /home/appuser/logA.log parameters. See Basic bash script variable declaration - command not found for further information regarding this.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
1
  • tail -f ("follow") will not finish, so it never gets to the next line. You probably meant tail -n 10 (the -n makes it POSIX compatible).
  • You cannot use spaces around equals signs when assigning a variable.
  • Variables are assigned to the string which the right-hand side evaluates to. Without special constructs, the result will simply be the literal string after the equals sign.
  • You should quote variables to avoid expansion.

In summary, you should use:

variableString=$(tail -10 /home/appuser/logA.log)
l0b0
  • 55,365
  • 30
  • 138
  • 223