Score:0

Shell script errors out due to PATH variable not set

gg flag

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)

pLumo avatar
in flag
Please provide your `./fix_wifi.sh` script.
bac0n avatar
cn flag
should not supply any path to echo [1](https://askubuntu.com/a/960826/986805)
hr flag
The fact that there's (apparently) nothing between the `: :` makes me wonder if this is actually a case of carriage returns in your script - rather than anything to do with your path variable
waltinator avatar
it flag
Always paste your script into `https://shellcheck.net`, a syntax checker, or install `shellcheck` locally. Make using `shellcheck` part of your development process.
Jarad Downing avatar
sg flag
While the command `service network-manager restart` should work, why not embrace systemd and replace it with `/usr/bin/systemctl restart network-manager`?
Score:0
gg flag

As steeldriver has suggested, the error was due to 'unicode non-breaking space' on line 17 and 18 within the IF clause (and lines 7,8 which checks if the script is executed by root user). I did the indentation to make the script more readable. I never thought it would give me headache. But, lesson learnt.

screenshot from shellcheck output

I think it is the Windows carriage return characters which is causing this issue.

I deleted these characters and typed space from vim editor and the issue went away.

Thanks everyone.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.