Подтвердить что ты не робот

Bash script присваивать значение grep переменной


Как назначить результат
grep -c "некоторый текст" /tmp/somePath
в переменную, чтобы я мог ее повторить.
Спасибо!

#!/bin/bash
some_var = grep -c "some text" /tmp/somePath
echo "var value is: ${some_var}"

Я также пробовал:   some_var = 'grep -c\ "некоторый текст \" /tmp/somePath '

Но я продолжаю получать: команда не найдена

Спасибо

4b9b3361

Ответ 1

Чтобы назначить вывод команды, используйте var=$(cmd) (поскольку shellcheck автоматически сообщает вам, вставляете ли вы свой script).

#!/bin/bash
some_var=$(grep -c "some text" /tmp/somePath)
echo "var value is: ${some_var}"

Ответ 2


Найденный вопрос
Его назначение, это будет работать:

some_var=$(command)


Пока это не сработает:

some_var = $(command)


Спасибо за помощь! Я приму первый полезный ответ.

Ответ 3

some_var=$(grep -c "some text" /tmp/somePath)

От man bash:

   Command substitution allows the output of a command to replace the com‐
   mand name.  There are two forms:

          $(command)
   or
          `command`

   Bash performs the expansion by executing command and replacing the com‐
   mand substitution with the standard output of  the  command,  with  any
   trailing newlines deleted.