Score:1

How to do sudo grep in a bash script?

in flag

I would like to check if the given ip address is already in the config file. But /etc/ can only be accessed by root, and I'm running the a script as admin. It seems I can't just do sudo

#!/bin/bash
CLIENT_WG_IPV4="10.66.66.1"

DOT_EXISTS=$(sudo grep -c "$CLIENT_WG_IPV4/22" "/etc/wireguard/wg0.conf")
if [[ ${DOT_EXISTS} == '0' ]]; then
   ...
fi

I get the error message:

grep: /etc/wireguard/wg0.conf: Permission denied

What can I do?

dominix avatar
gf flag
sudo is certainly asking for a password. you should run your script as an authorized user or as root without sudo.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.