0

I have this Shell script here:

###
# Create a folder dynamically
mkdir archived_PA_"$(date -d "6 months ago - 1 day" +%Y-%m-%d)"_"$(date -d "1 day ago" +%Y-%m-%d)"

# Move files to new folder dynamically
find ./VA -newermt $(date +%Y%m%d -d '6 months ago') ! -newermt $(date +%Y%m%d -d 'today') -exec mv -t /var/log/pentaho/archived_PA_"$(date -d "6 months ago - 1 day" +%Y-%m-%d)"_"$(date -d "1 day ago" +%Y-%m-%d)" {} +

# Archive dynamic folder 
zip -r archived_PA_"$(date -d "6 months ago - 1 day" +%Y-%m-%d)"_"$(date -d "1 day ago" +%Y-%m-%d)".zip /var/log/pentaho/archived_PA_"$(date -d "6 months ago - 1 day" +%Y-%m-%d)"_"$(date -d "1 day ago" +%Y-%m-%d)"

At first, every line runs fine on command lines but when I run this Shell script with this command ./script_name.sh then I would get the following error:

./HIX-170061.sh: line 4: $'\r': command not found
find: missing argument to `-exec'
./HIX-170061.sh: line 7: $'\r': command not found
  adding: var/log/pentaho/archived_PA_2023-01-09_2023-07-09^M/ (stored 0%)

In short, I am able to execute other lines (except for line 4 and 7 but it's empty line so I assume it doesn't matter) but line 6 is where I get the error, which is find: missing argument to `-exec' error.

2
  • You're missing the first line that should define the interpreter to use. For example #!/bin/sh. This won't fix the errors you describe but it is an important thing to get right Commented Jul 10, 2023 at 21:50
  • @roaima Thanks. I will take note of this. Commented Jul 10, 2023 at 22:54

2 Answers 2

4

The $'\r' messages mean that the script contains Windows line endings consisting of carriage return (CR) characters along with newline characters, and bash treats carriage returns as non-space characters.

The find error occurs because the last argument passed to it will be a plus followed by a carriage return character instead of the expected plus sign on its own that would terminate the -exec command.

Running dos2unix on the script should solve the problem.

1
  • 1
    I could not install dos2unix so I use Notepad++ to convert my script to Unix and it works. Thanks for your suggestion, it actually helps me find out that I can finally solve this with Notepad++. Commented Jul 10, 2023 at 22:55
0

I was able to solve it with Notepad++. I just simply go to Edit -> EOL Conversion -> Unix, and then I'm able to run the script.

3
  • Where do you get Unix/Linux version of that program? Its web site suggests it's for Windows only. Commented Jul 11, 2023 at 8:22
  • Maybe the OP is using WSL, or transferring the file from Windows to Linux.
    – ak2
    Commented Jul 11, 2023 at 11:09
  • @TobySpeight I use WSL, convert the script to Unix with Notepad++ and then run the script on WSL. Commented Jul 12, 2023 at 9:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .