Skip to main content
Became Hot Network Question
Changed the question to match the expected answer
Source Link

I'm trying to reboot a number of Ubuntu servers via script when a reboot is necessary.

When I execute bash with the test as a noninteractive command I get the result that no reboot is required, even though the file /var/run/reboot-required exists.

usera@client:~$ ssh server02 bash -c 'test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"'
server02: no reboot required

When I log on to the same server via SSH and run my test manually I get the correct result, sudo reboot.

usera@client:~$ ssh server02
Last login: Tue Jun 14 08:03:00 2022 from 146.140.16.1
usera@server02:~$ test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"
sudo reboot
usera@server02:~$ bash -c 'test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"'
sudo reboot

Why is bash behaving differently when being invoked byWhat do I need to change to get the ssh command versus an interactive logincorrect result?

I'm trying to reboot a number of Ubuntu servers via script when a reboot is necessary.

When I execute bash with the test as a noninteractive command I get the result that no reboot is required, even though the file /var/run/reboot-required exists.

usera@client:~$ ssh server02 bash -c 'test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"'
server02: no reboot required

When I log on to the same server via SSH and run my test manually I get the correct result, sudo reboot.

usera@client:~$ ssh server02
Last login: Tue Jun 14 08:03:00 2022 from 146.140.16.1
usera@server02:~$ test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"
sudo reboot
usera@server02:~$ bash -c 'test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"'
sudo reboot

Why is bash behaving differently when being invoked by the ssh command versus an interactive login?

I'm trying to reboot a number of Ubuntu servers via script when a reboot is necessary.

When I execute bash with the test as a noninteractive command I get the result that no reboot is required, even though the file /var/run/reboot-required exists.

usera@client:~$ ssh server02 bash -c 'test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"'
server02: no reboot required

When I log on to the same server via SSH and run my test manually I get the correct result, sudo reboot.

usera@client:~$ ssh server02
Last login: Tue Jun 14 08:03:00 2022 from 146.140.16.1
usera@server02:~$ test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"
sudo reboot
usera@server02:~$ bash -c 'test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"'
sudo reboot

What do I need to change to get the correct result?

Post Reopened by ilkkachu bash
Post Closed as "Duplicate" by muru bash
Source Link

bash test wrong result when run noninteractively

I'm trying to reboot a number of Ubuntu servers via script when a reboot is necessary.

When I execute bash with the test as a noninteractive command I get the result that no reboot is required, even though the file /var/run/reboot-required exists.

usera@client:~$ ssh server02 bash -c 'test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"'
server02: no reboot required

When I log on to the same server via SSH and run my test manually I get the correct result, sudo reboot.

usera@client:~$ ssh server02
Last login: Tue Jun 14 08:03:00 2022 from 146.140.16.1
usera@server02:~$ test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"
sudo reboot
usera@server02:~$ bash -c 'test -f /var/run/reboot-required && echo sudo reboot || echo "$(hostname): no reboot required"'
sudo reboot

Why is bash behaving differently when being invoked by the ssh command versus an interactive login?