0

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)

  • Please provide your ./fix_wifi.sh script. – pLumo Jun 09 '21 at 18:03
  • 1
    should not supply any path to echo 1 –  Jun 09 '21 at 18:16
  • 3
    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 – steeldriver Jun 09 '21 at 18:20
  • Always paste your script into https://shellcheck.net, a syntax checker, or install shellcheck locally. Make using shellcheck part of your development process. – waltinator Jun 09 '21 at 19:08
  • While the command service 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

1 Answers1

0

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.