I am writing a script to automate some tasks and I need to add a user to a group in /etc/group. I'm trying to use this command:
sudo bash -c "awk '{if (/^moli/) {$0=$0"$uservar,"}; print' /etc/group > /etc/group"
The issue I'm running into is I get
awk: cmd. line:1: {if (/^moli/) {-bash=-bashtestuser1,}; print
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: {if (/^moli/) {-bash=-bashtestuser1,}; print
awk: cmd. line:1: ^ unexpected newline or end of string
If I change the ' '
to ""
I get: -bash: syntax error near unexpected token '('
bash -c
here? Can't you just doawk ... | sudo tee /etc/group
? However, I would strongly suggest not to manually edit/etc/group
file. If you do anything wrong, you might lock yourself out of your system. The least you have to do is make a backup of the file. Also, what is$uservar
? Seems you rather want to usesudo sed -i ...
– pLumo Jun 07 '21 at 18:18/etc/group
manually! Rather useusermod
command. See here. – pLumo Jun 07 '21 at 18:23usermod -a -G moli username
as a test. I checked to see if the user showed up in /etc/group but they're not there. – TL_Arwen Jun 07 '21 at 18:34groups username
it shows there. – TL_Arwen Jun 07 '21 at 18:45