On Wed, Nov 20, 2024 at 9:42 PM Bitfox <h...@bitfox.ddns.net> wrote:
>
> In my bash shell script, when I say:
>
> sudo echo "something" >>/etc/postfix/virtual_alias_maps
>
> it could not run with the prompts:
>
> bin/mask.sh: line 18: /etc/postfix/virtual_alias_maps: Permission denied
>
>
> but, if I just say:
>
> echo "something" >>/etc/postfix/virtual_alias_maps
>
> and run that script with the form of "sudo bin/mask.sh", it successes.
>
> Can you help me why the first sudo failed?

Greg gave you the answer.

What I do in this case is, I don't use sudo in the script. Instead I
run the script with sudo like Keith suggested. However, I add this to
the beginning of the script to gracefully exit:

<SNIP>
#!/usr/bin/env bash

# Control our PATH
PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH
export PATH

if [[ "${UID}" -ne 0 ]]; then
   echo "This command has to be run under the root user."
   exit 1
fi
...
</SNIP>

Jeff

Reply via email to