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
sudo -u blueray echo "Running script as user $(whoami)"that$(whoami)is expanded beforesudoruns?chshcommand, unless you are trying to change root's default shell. Is there something there that you do not want to run as root?root, notblueray; and this was(?) a part of the problem.