Ubuntu version: Ubuntu 20.04.2.0 LTS
Shell: bash
I have a shell script which will be executed by root from root’s cron job. But, even when I execute the script manually, it errors out saying the following
# ./fix_wifi.sh
./fix_wifi.sh: line 17: : command not found
./fix_wifi.sh: line 18: : command not found
Lines 17 and 18 are the ones with echo
and service network-manager restart
Providing the absoulte path names (/usr/bin, /usr/sbin) for these binaries in the script did not help. So, I would like to source the startup files at the beginning of the script so that the script will be aware of the PATH variable (and other relevant variables)
But, for root user in Ubuntu, which startup file should I source ? I can see .bashrc
and .profile
files in root's home directory /root
.
#!/bin/bash
Written by xyz
Must be run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
ConnectionStatus=$(nmcli networking connectivity)
#echo "$ConnectionStatus"
if [ "$ConnectionStatus" != "full" ]
then
/usr/bin/echo "Wifi found to be disconnected at " date
" hence restarting the network manager..." | /usr/bin/tee -a /home/john/scripts/wifi_diagnostics.log
/usr/sbin/service network-manager restart
fi
Note:
I did forget to provide absolute path for date
command in Line 17. But, it is not very relevant here as line 18 containing service network-manager restart
still errored out despite providing full path (/usr/sbin)
./fix_wifi.sh
script. – pLumo Jun 09 '21 at 18:03: :
makes me wonder if this is actually a case of carriage returns in your script - rather than anything to do with your path variable – steeldriver Jun 09 '21 at 18:20https://shellcheck.net
, a syntax checker, or installshellcheck
locally. Make usingshellcheck
part of your development process. – waltinator Jun 09 '21 at 19:08service network-manager restart
should work, why not embrace systemd and replace it with/usr/bin/systemctl restart network-manager
? – Jarad Downing Jun 10 '21 at 15:31