Good day allPurpose: checking if a string exists in a file and running actions accordinglyif [ -z 'sudo cat /etc/sudoers | grep $USER ALL=(ALL) NOPASSWD:ALL' ]; echo no output, cont. || echo line exists, skippingThe output always presents line exists, skipping in bot cases where the line exists and does not.Adding the output into a variable e.g. x, substituting the line in the if statement with the variable produces the correct output, How may I 'skip' the variable route and run the command directly?
Checking if string exists in file with cat | grep
shell script;scripting;grep;variable
grep can be used as a condition command. It returns true when the pattern matches. Here, you want a fixed-string search (-F) and probably to match on the full line (-x):if sudo cat /etc/sudoers |  grep -xqFe $USER ALL=(ALL) NOPASSWD:ALLthen  echo foundelse  echo not foundfiOr if the sudoers configuration allows you to run the grep command in addition to the cat one:if sudo grep -xqFe $USER ALL=(ALL) NOPASSWD:ALL /etc/sudoersthen  echo foundelse  echo not foundfi-q tells grep to be quiet, to just return the true/false status via the exit status but not output anything on stdout.Note the sudoers configuration can include other configuration files which that approach will not take into account. Same for permissions granted to groups that the user is member of.You may want to use sudo -lU $USER instead.