0

I'm trying to add a timestamp to the log script that I have based on this solution. I'm new in bash, but I think this supossed to be working but it does not. Which part I did something wrong?

dothing.sh :

#!/usr/bin/env bash

adddate() {
    while IFS= read -r line; do
        printf '%s %s\n' "$(date)" "$line";
    done
}

/home/astden01a000/app/anaconda3/envs/dataengineer/bin/python "/scripts/ixi/bin/framework_develope/script/project_x/engine_trigger_file.py" "/scripts/ixi/bin/properties/CashX.cfg" "bulk" "prod" | adddate > /data04/eva/tax/hi/garu/outstanding/development/cronjob/log_run_ingest_cod.txt

But when I try to run the bash script bash dothing.sh it says syntax error near unexpected token $'{\r''``

  • In other words, you script seems to have windows line endings with carriage returns; run the file through `dos2unix` first. – Benjamin W. Nov 03 '21 at 15:20
  • 1
    @ImBaldingPleaseHelp, which text editor are you using to create your file, on which operating system? Using a Windows editor to create a file you then run on a UNIX system is the easiest way to get this problem. (Some UNIX editors will _keep_ text files in DOS file formats if they were loaded that way, in vim, `:set fileformat=unix` will turn that off). – Charles Duffy Nov 03 '21 at 15:43
  • 1
    fwiw ... from a performance perspective, especially if running a lot of output/lines through this function, you'll want to replace `$(date)` as this will invoke a cpu-/time-intensive subprocess each time it's called; consider replacing with `printf -v logdt '%(%Y/%m/%d %H:%M:%S)T'` (adjust format as desired) and then `printf "%s %s\n" "${logdt}" "${line}"`; the `printf -v ... ` does not invoke a subprocess so on large volumes of calls you can be looking at a difference of a couple magnitudes in performance ... – markp-fuso Nov 03 '21 at 15:57
  • 1
    see the difference with these two tests: `time for i in {1..100}; do echo "$(date)" > /dev/null; done` and `time for i in {1..100}; do printf -v x '%(%Y/%m/%d %H:%M:%S)T'; echo "$x" > /dev/null; done` – markp-fuso Nov 03 '21 at 15:57
  • 1
    re: `\r` (windows/dos line ending); run `od -c dothing.sh` and review the output; if you see `\r` then this confirms what others have said ... the script has windows/dos endings and needs to be edited to remove said characters (eg, `dos2unix dothing.sh`) – markp-fuso Nov 03 '21 at 17:13

0 Answers0