Why Am I Getting a ‘r’: Command Not Found Error in My Bash Script?

0
12
Asked By CuriousCoder92 On

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

Answered By ScriptNinja00 On

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.

Answered By LinuxLover88 On

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!

Answered By TechSavant21 On

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.