FlightAware Discussions

Vnstati Script Issue

When I run a script, vnstati.sh, from $home that contains:
#!/bin/bash
sudo vnstati -hs -o /usr/share/dump1090-fa/html/net/summary.png
sudo vnstati -d -o /usr/share/dump1090-fa/html/net/day.png
sudo vnstati -m -o /usr/share/dump1090-fa/html/net/month.png
sudo vnstati -t -o /usr/share/dump1090-fa/html/net/top.png

The resulting names are strange:
'day.png'$'\r' 'month.png'$'\r' 'summary.png'$'\r' 'top.png'$'\r'

I’ve purged/installed vnstat and vnstati, but the results are the same.
The same script on another RPi configured the same works as expected.
What is the $’\r’? How can I fix this?

I suspect you have written your script using a Windows text editor. The $’\r’ is caused by there being a Dos style carriage return, line feed pair at the end of the lines rather than a Linux style single linefeed.
Edit the script to remove the CR characters and it should fix it.
Alternatively convert your script file using dos2unix
https://linux.die.net/man/1/dos2unix

The script was written using nano on a RPi 4 (Buster).
I wrote a new version of it but the results were the same.

Edit - I rewrote the script on a single line with the commands separated with &&. Sure enough only the last file name contained the extra characters. I padded the line with a few spaces and it worked. Is there some option in nano that inserts the extra characters?

Thanks for your help! :grinning:

1 Like

You can output files in dos or Unix format in nano.
alt D will Let you save it in dos format.
Once you have saved it as dos it will keep treating it as a dos file. Use alt M to swap back and save a dos file in Unix format.

2 Likes

If the script has been written on Windows using Notepad, this problem will occur when the file is used in Linux.

The cause is that Notepad saves files with line break CR LF, while Linux uses LF

To overcome this, install notepad ++ on Windows, open text file using it, and change the line break from Windows (CR LF) to Unix ( LF) and save file. Please see screenshot below:

 

 

 

The script apparently wasn’t written in Windows:

set -e

Put this at the start of the script, then it will exit on error.
Thus you can put the commands back on their individual lines, on the first error it will exit.

@ua549 … it would be nice to know if that fixed the issue.

I suppose you must have pressed Alt-D on accident converting to windows line breaks at some point before?

Thanks everyone for all of the tips. The issue is fixed.