0

I'm trying to run some commands that require date begin and end arguments, but when I combine dateFrom and dateTo variables it's missing from start.

Please check my code and output last line, Thanks.

#! /bin/bash
dateFrom=$(date +"%Y-%m-%d")
dateTo=$(date --date="-7 day" +"%Y-%m-%d")
echo "dateFrom: $dateFrom"
echo "dateTo: $dateTo"
echo "dateFrom: $dateFrom, dateTo: $dateTo"

output

dateFrom: 2020-04-23
dateTo: 2020-04-16
, dateTo: 2020-04-16
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
W. San
  • 13
  • 3
  • Your file has dos line endings. Remove them. – KamilCuk Apr 23 '20 at 06:56
  • 1
    Your script file has DOS/Windows line endings, which is putting carriage return characters in your variables. See: [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Gordon Davisson Apr 23 '20 at 06:56
  • i have removed. but output still same. – W. San Apr 23 '20 at 07:36
  • How did you remove dos line endings? Try: `tr -d '\r' <{file.dos} >{file}` where `{file.dos}` is the "wrong" file and `{file}` shall be the new file without the DOS line-endings. – Sir Jo Black Apr 23 '20 at 07:55
  • just go to very last line and hit backspace :D . i don't know how to use it. but problem solved thank you guys all. – W. San Apr 23 '20 at 08:27

1 Answers1

0

It seems a problem due to the presence of DOS line terminators in the bash-script file.

I wrote and use the bash-script in the following lines to solve the issue.

This script will remove the \r chars from a specified file after it will have generate a copy of the original file.

You may copy this script as dos2unix.sh and call it as:

/home/user: ./dos2unix.sh filename<enter>

The execution will generate two files: filenames.dos, that is the original, and filename that is the file cleaned from the \r chars.

#!/bin/bash

program=$(basename $0)

if test $# -lt 1
then
  echo "usage: $program filename"
  echo -e "\nThe original file will be copied as filename.dos"
  exit 1
fi

cp $1 $1.dos

tr -d '\r' <$1.dos >$1

Note: Remember to execute: chmod +x dos2unix.sh!

Sir Jo Black
  • 2,024
  • 2
  • 15
  • 22