Hey everyone! I'm running into a weird issue with a bash script I wrote for backing up files using rclone. I'm trying to implement logging, but when I run the script, I get this error: 'r': command not found. The confusing part is that I can't find any 'r' characters in my script. The rclone sync works just fine, so I'm not sure why this logging part is giving me trouble. Here's the script I'm using:
```bash
#!/bin/bash
LOG_FILE="/var/log/backup.log"
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "${LOG_FILE}"
}
log "Starting the script"
rclone sync -v --create-empty-src-dirs $HOME/Documents Google:Documents
log "Script completed successfully"
```
When I check the result of running the script, I get the following output:
```
barry@barryubuntu:~/sh$ sudo bash backup.sh
[sudo] password for barry:
backup.sh: line 3: $'r': command not found
backup.sh: line 4: syntax error near unexpected token `$'{r''
backup.sh: line 4: `log() {
barry@barryubuntu:~/sh$ cat backup.sh
#!/bin/bash
LOG_FILE="/var/log/backup.log"
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "${LOG_FILE}"
}
log "Starting the script"
rclone sync -v --create-empty-src-dirs $HOME/Documents Google:Documents
log "Script completed successfully"
```
I want to get this logging feature working correctly like I had it set up in Windows, so any insights on how to solve this error would be greatly appreciated! Thanks!
3 Answers
Also, about the text editor you used—if it's the default one in Ubuntu, it sometimes adds extra characters that are not visible. Just make sure to save your files in Unix format when you're done editing, and consider using a code editor that allows you to specify line endings.
I had a similar issue, and just to add on, you can check the line endings of your script using `file backup.sh`. If it shows "CRLF line terminators", then you definitely need to run it through `dos2unix`. It’s a straightforward fix, but it’s surprising how many tutorials miss this detail!
It looks like your script might still have some carriage return characters (r) that often come from editing the script on Windows and transferring it to a Unix system. You can fix this by running your script through `dos2unix`, which will strip out those problematic characters. It's a common issue when moving files between different operating systems, so don’t sweat it too much! Just try that and see if it resolves the problem.
Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically