0

My current code is

#! /bin/sh
yday=$(/usr/bin/date  --date="yesterday" +"%Y-%m-%d")
outfile='/var/www/api.example.com/seb/imports/'${yday}'.csv'
infile="https://example.com/export/vendors?lower_date=${yday}&higher_date=${yday}"
echo $outfile
echo $infile

The output is

.csvr/www/api.example.com/seb/imports/2022-01-06
&higher_date=2022-01-06com/export/vendors?lower_date=2022-01-06

Also tried as

outfile=$("/var/www/api.example.com/seb/imports/$yday.csv")

and

outfile=$('/var/www/api.example.com/seb/imports/'${yday}'.csv')

These generate

.csv: not found.example.com/seb/fetch_csv.sh: 3: /var/www/apidev.example.com/seb/fetch_csv.sh: /var/www/api.example.com/seb/imports/2022-01-06

The intended use of the variables is in /usr/bin/wget -q -O $outfile $infile

How do I get the intended path and url correctly interpolated into my variables?

0stone0
  • 34,288
  • 4
  • 39
  • 64
jerrygarciuh
  • 21,158
  • 26
  • 82
  • 139
  • Not sure that your question is, the `echo $outfile` shows the desired string: `/var/www/api.example.com/seb/imports/2022-01-06.csv`? – 0stone0 Jan 07 '22 at 16:44
  • 1
    `/bin/sh` is not bash. A script you run with `sh` is a POSIX sh script, not a bash script: Even if `sh` is a symlink to `bash`, it runs in compatibility mode with some features turned off when started under that name. (Think of sh-vs-bash like C-vs-C++; two different languages, one being a superset of the other, and some implementations of one supporting features borrowed from the other, but very distinct nonetheless). – Charles Duffy Jan 07 '22 at 17:11
  • Also, `echo $varname` is not a safe and effective way to check a variable's contents. If your shell is bash (`sh` doesn't support any of these features), you can use `declare -p varname`, or `printf 'varname=%q\n' "$varname"`, or for new enough versions of bash, `printf '%s\n' "varname=${varname@Q}"`. See [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566), and [Why is printf better than echo?](https://unix.stackexchange.com/questions/65803) – Charles Duffy Jan 07 '22 at 17:16
  • 1
    ...any of the approaches suggested above would show the `$'\r'`s -- aka carriage returns -- in your data, which are what are sending the cursor back to the front of the line when printed. – Charles Duffy Jan 07 '22 at 17:18

1 Answers1

1

Please make sure the lines of your script does not end in CRLF. Run dos2unix against it. The mixed output is likely caused by it.

Also you need to quote your variables properly, and uniformly if possible:

yday=$(/usr/bin/date --date=yesterday '+%Y-%m-%d')
outfile="/var/www/api.example.com/seb/imports/${yday}.csv"
infile="https://example.com/export/vendors?lower_date=${yday}&higher_date=${yday}"
echo "$outfile"
echo "$infile"
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • 1
    In vi I am not seeing the ^M which sometimes indicates Windows carriage returns but i did write in Win IDE so rewrote from scratch in vi on Debian box and that did fix it so I presume you are correct. Thanks! – jerrygarciuh Jan 07 '22 at 17:23