mtryingtocode
mtryingtocode

Reputation: 969

ksh script syntax error on date

My shell script fails on this portion

dt=$(date +%Y%m%d)

syntax error at line 35: `dt=$' unexpected

It's a bit strange as I'm using the same to get the datetime and don't get any problem

timestamp=$(date +%Y%m%d%H%M%S)

20170911105251

I've checked in Notepad++ as well that I don't have any dirty characters as suggested by @Frank below

enter image description here

Below is the sample script I have. Please note I have omitted some portions that are confidential. Basically the script transfers files from Server A to B based on the dt specified. If no date parameter is specified during execution, it takes the current date.

if [ -z "$1" ]
then
    dt=$(date +%Y%m%d)
else
    dt=$1
fi

export rundate

timestamp=$(date +%Y%m%d%H%M%S)
logfile=${log_path}/${batch}-${dt}-${timestamp}.log
mail_file=${log_path}/${batch}-mail-${timestamp}.txt

export mail_file

filecount=$(wc -l ${batch_filelist} | cut -d " " -f 1)

sftp ${dest} <<EOF >> $logfile
cd $destdir
lcd $sourcedir
put -p *.txt
exit
EOF


mail -s "SFTP Done (dt:$dt)" $(cat $email_accounts) < $mail_file

Upvotes: 0

Views: 684

Answers (1)

Frank
Frank

Reputation: 881

Becarful , when you copy text from anywhere and paste in file (i think you did it) , it can add "dirty" characters . If you have a text editor like notepad++ , by clicking on :

enter image description here

It will show all characters .

Via notepad++ you can change the file format from windows to unix , by right click on bottom-right in page :

enter image description here

Save the file and send again in to your machine . The syntax seems correct so it should work .

Anyway i mentioned notepad++ but you can do this with other text editors.

dt=$(date +'%Y%m%d') and not dt=$(dt +'%Y%m%d')

A working example below :

!/usr/bin/bash
if [ -z $1 ]
then
    dt=$(date +'%Y%m%d')
else
    dt=$1
fi
echo $dt

Result :

xxxxxxxxxxxxxx:/pathtosh> prova.sh

20170911

xxxxxxxxxxxxxx:/pathtosh> prova.sh 1

1

Upvotes: 1

Related Questions