0

I am working on a bash script which iterates over mdb files and exports them to a csv.

Here is my code:

#!/bin/bash

YEAR="2001/"
INFOLDER="/local/data/datasets/Convergence/"
OUTFOLDER="~/Workspaces/Ventilator_Repository/dataset/csv/"
for f in "$INFOLDER$YEAR*.mdb";
do
    absname=$INFOLDER$YEAR$(basename $f)
    echo "input file: $absname"
    streamingFile="$OUTFOLDER$YEAR$(basename $f)_streaming.csv"
    echo "output file: $streamingFile"
    streamcommand="mdb-export -d , $absname STREAMING > ${streamingFile}"
    echo "command: $streamcommand"
    mdb-export "-d , " $absname " STREAMING" > $streamingFile
done

However, I am getting this error:

input file: /local/data/datasets/Convergence/2001/14a_25Sep2001_102404.mdb

output file: ~/Workspaces/Ventilator_Repository/dataset/csv/2001/14a_25Sep2001_102404.mdb_streaming.csv

command: mdb-export -d , /local/data/datasets/Convergence/2001/14a_25Sep2001_102404.mdb STREAMING > ~/Workspaces/Ventilator_Repository/dataset/csv/2001/14a_25Sep2001_102404.mdb_streaming.csv

However, I get the following error:

line 14: ~/Workspaces/Ventilator_Repository/dataset/csv/2001/14a_25Sep2001_102404.mdb_streaming.csv: No such file or directory

If I run the command in the terminal, the export works fine! I do not know why in the bash script it does not work.

I am using mdb-tools to export mdb MS Access files into csv and my machine runs on Ubuntu 16.04.

Community
  • 1
  • 1
Matin Kh
  • 5,192
  • 6
  • 53
  • 77
  • `~` loses its special meaning inside quotes. – Charles Duffy Sep 06 '17 at 16:15
  • And are you certain `-d ,` is supposed to be in quotes? That's very unusual. Generally a program expects the `,` to be either passed as the immediately next character from `-d`, or as a subsequent argument list entry. – Charles Duffy Sep 06 '17 at 16:15
  • Similarly, leaving your variable expansions *unquoted* is almost certainly wrong. Consider running your code through http://shellcheck.net/ and fixing what it finds. – Charles Duffy Sep 06 '17 at 16:16
  • A correct invocation is more likely to look like: `mdb-export -d , "$absname" STREAMING >"$streamingFile"` – Charles Duffy Sep 06 '17 at 16:17
  • ...and btw, all-caps variable names are used by variables with meaning to the OS or system, whereas names with lowercase characters are guaranteed safe for application use (or, rather, guaranteed not to conflict with names meaningful to POSIX-specified tools). See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, fourth paragraph, keeping in mind that setting a shell variable overwrites any like-named environment variable (thus making the namespace shared). – Charles Duffy Sep 06 '17 at 16:20
  • Good point! Removed double quotes. Thanks! – Matin Kh Sep 06 '17 at 16:20

0 Answers0