0

I have about 10k (approx 180x50x2) CSV files which I want to join together as following, but the inner for loop fails because of some syntax error; I cannot see the error in lastFile

#!/bin/bash dir='/home/masi/CSV/' targetDir='/tmp/' ids=(118 119 120) channels=(1 2) for id in ids; do for channel in channels; # example filename P209C1T720-T730.csv lastFile="$dir'P'$id'C'$channel'T1790-T1800.csv'" # show warning if last file does not exist if [[ -f $lastFile ]]; then echo "Last file "$lastFile" is missing" exit 1 fi filenameTarget="$targetDir'P'$id'C'$channel'.csv'" cat $dir'P'$id'C'$channel'T'*'.csv' > $filenameTarget done; done 

Error

makeCSV.sh: line 12: syntax error near unexpected token `lastFile="$dir'P'$id'C'$channel'T1790-T1800.csv'"' makeCSV.sh: line 12: ` lastFile="$dir'P'$id'C'$channel'T1790-T1800.csv'"' 

OS: Debian 8.5
Linux kernel: 4.6 backports

3
  • 1
    there is a missing do in your second for loop Commented Oct 29, 2016 at 8:16
  • 1
    I really really don't see a do in the second for loop. Are you really sure that there is a do? Commented Oct 29, 2016 at 8:21
  • 4
    The do in the previous line is the do for the first for loop. The second for loop needs another do. Syntax of for loop: for name in list; do commands; done. Commented Oct 29, 2016 at 8:26

2 Answers 2

2

There is a missing do in your second for loop:

for id in ids; do for channel in channels; do # <----- here ---- # example filename P209C1T720-T730.csv lastFile="$dir'P'$id'C'$channel'T1790-T1800.csv'" # show warning if last file does not exist if [[ -f $lastFile ]]; then echo "Last file "$lastFile" is missing" exit 1 fi filenameTarget="$targetDir'P'$id'C'$channel'.csv'" cat $dir'P'$id'C'$channel'T'*'.csv' > $filenameTarget done; done 

Based on the discussion in the comments I see your confusion with the syntax of the for loop.

This is the rough syntax of the for loop:

for name in list; do commands; done 

There always must be a do before commands and a ; (or newline) followed by done after the commands.

Here is a variation with more newlines:

for name in list do commands done 
3
  • There is still one problem in the lastFile variable. No variables are expanded as expected and * is not used as used as expected. Do you understand why? Commented Oct 29, 2016 at 8:31
  • if bash does not expand * that usually means there are no filenames which match the pattern. If you still have problems with that part then please ask a new question. Commented Oct 29, 2016 at 8:40
  • I expanded the other part here unix.stackexchange.com/q/319689/16920 Commented Oct 29, 2016 at 9:08
1

It's properly working:

#!/bin/bash dir='/home/masi/CSV/' targetDir='/tmp/' ids=(118 119 120) channels=(1 2) for id in ids ; do # Add do after ';' for channel in channels ; do # example filename P209C1T720-T730.csv lastFile="$dir'P'$id'C'$channel'T1790-T1800.csv'" # show warning if last file does not exist if [[ -f $lastFile ]] ; then echo "Last file "$lastFile" is missing" exit 1 fi filenameTarget="$targetDir'P'$id'C'$channel'.csv'" cat $dir'P'$id'C'$channel'T'*'.csv' > $filenameTarget done done 

For the future use bash debugger: bash -x /path/to/your/script.

1
  • How can you use the asterisk correctly? I get cat: /home/masi/CSV/PidsCchannelsT*.csv: No such file or directory where variable expansions are not working and also * does not seem to work as expected. Also, all variable expansions are not working. Commented Oct 29, 2016 at 8:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.