0

I am running the following script as sudo.

#!/bin/bash whoami echo $USER ~ sudo -u blueray bash <<"DOF" whoami echo $USER ~ DOF echo "Running script as user $(whoami)" sudo -u blueray echo "Running script as user $(whoami)" sudo -u blueray bash -c 'echo "Running script as user $(whoami)"' 

Getting the following result:

% sudo bash test.sh 2>&1 | tee --append /tmp/log.txt root root /root blueray blueray /home/blueray Running script as user root Running script as user root Running script as user blueray 

My issue is , I am trying to run a script which has sudo and non-sudo commands in it.

The suggestions say that using sudo -u username command in script will run the lines as specified user. But my findings is showing otherwise. In my case only sudo -u blueray bash -c works. But subshell and bash -c works differently.

% var=value % bash -c 'echo $var' % (echo $var) value 

So not understanding if this is what I really want.

Moreover, I want to run my script sequentially and not in parallel. Not sure if sudo -u blueray bash -c will open a different shell and run those in parallel or not.

In summary, I want to view stdout and stderr of all the lines of my scripts in my terminal. I want to run the script as sudo except specific lines. I want to run the script sequentially and not in parallel.

My script looks like:

#!/bin/bash apt update apt upgrade -y apt install -y git zsh zsh-autosuggestions zsh-syntax-highlighting python3 python-is-python3 python3-pip mdadm chsh -s $(which zsh) mdadm --assemble --scan tee -a /etc/fstab << END LABEL=8TBRaid0 /media/blueray/8TBRaid0 ext4 nosuid,nodev,nofail,x-gvfs-show 0 2 LABEL=WDPurple8TB /media/blueray/WDPurple8TB ext4 nosuid,nodev,nofail,x-gvfs-show 0 2 LABEL=WDPurple6TB /media/blueray/WDPurple6TB ext4 nosuid,nodev,nofail,x-gvfs-show 0 2 END curl -fsSLo /usr/share/keyrings/brave-browser-archive-keyring.gpg https://brave-browser-apt-release.s3.brave.com/brave-browser-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/brave-browser-archive-keyring.gpg arch=amd64] https://brave-browser-apt-release.s3.brave.com/ stable main"| tee /etc/apt/sources.list.d/brave-browser-release.list add-apt-repository -y ppa:christian-boxdoerfer/fsearch-stable apt update apt upgrade -y apt install -y foliate brave-browser curl -sSL https://get.docker.com/rootless | sh systemctl --user start docker.service systemctl --user enable docker.service loginctl enable-linger blueray wget -O firefox.tar.bz2 "https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=en-US" tar xjf firefox.tar.bz2 apt remove firefox mv firefox/ /opt/firefox rm firefox.tar.bz2 ln -s /opt/firefox/firefox /usr/bin/firefox desktop-file-install /media/blueray/WDPurple6TB/_Working/_NotesFiltered/linux-mint/supporting-files/Firefox.desktop pip install yapf 
11
  • 2
    An alternative is to run the script as the normal user (without sudo), and use sudo in the script only for the tasks that need sudo. I use that method, and it works for me. Let me know, if you want an example of such scripting. Commented Dec 10, 2021 at 13:33
  • 2
    Are you aware that in sudo -u blueray echo "Running script as user $(whoami)" that $(whoami) is expanded before sudo runs? Commented Dec 10, 2021 at 13:35
  • 1
    See this link. The fixes at the beginning [of the main part of the script] are not necessary, but makes the script more noob-proof. Actually, most Linux systems let you (and scripts) use sudo within a grace period of 10-15 minutes without password after the first time you enter the password. Commented Dec 10, 2021 at 13:38
  • 1
    Please explain what the final objective here is. As far as I can tell, all the commands in your script should be run as root with the exception of the chsh command, unless you are trying to change root's default shell. Is there something there that you do not want to run as root? Commented Dec 10, 2021 at 13:43
  • 2
    "Can I safely use… ?" – It depends on what you want (and what you mean by "safely"). My impression was you were surprised the next-to-last line of your first script had given you root, not blueray; and this was(?) a part of the problem. Commented Dec 10, 2021 at 13:51

1 Answer 1

3

You are misinterpreting the output, but that's very understandable, it is confusing. Specifically, this line:

sudo -u blueray echo "Running script as user $(whoami)" 

That will run echo as the user blueray, BUT, everything in the command is expanded before echo is launched. That's just how the shell works and that's why echo $var will print the value of the variable and not just the string $var. In other words, when you run the command above, what happens is:

  1. The shell expands $(whoami) to the output of the whoami command, which is root.
  2. The shell calls echo passing it the expanded value.

So what you are actually running is

sudo -u blueray echo "Running script as user root" 

To get the behavior you were expecting, you need to call a new shell and tell it to run the command, using single quotes instead of double quotes because single quotes block expansion:

sudo -u blueray bash -c 'echo "Running script as user $(whoami)"' 

To illustrate:

$ sudo -u bib echo "Running script as user $(whoami)" Running script as user terdon $ sudo -u bib sh -c 'echo "Running script as user $(whoami)"' Running script as user bib 

So, if you want to run specific commands as a different user, then yes, this is the way to do it. It is just the output of whoami that is confusing you.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.