1

When I try this simple script:

#!/bin/bash
#append csv

FILES= /home/stef/test/*

for f in $FILES do
        cat $f >> ~/test.txt done

I get following error:

./append.sh: line 4: /home/stef/test/test2.txt: Permission denied.

What am I doing wrong?

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
user1345883
  • 451
  • 1
  • 8
  • 24

4 Answers4

5

Replace

FILES= /home/stef/test/*

with

FILES=/home/stef/test/*

The script is trying to evaluate the first match of /home/stef/test/*. It says permission denied because the file is not executable.

./append.sh: line 4: /home/stef/test/test2.txt: Permission denied.

That's line 4. Try with FOO= * in a non-empty directory.

And your for ... do; done syntax is borked as well, done needs a separator (; or newline) and so does "do". That's not what produces this erroe message however.

sapht
  • 2,789
  • 18
  • 16
  • @sapht it looks like mistake made by editor not author. Main question in antoher. – Laser Sep 14 '12 at 10:20
  • 1
    No, bash is picky about whitespace in variable assignments, which causes this issue. – sapht Sep 14 '12 at 10:28
  • @sehe My tests suggest that it ignores the variable assigment and tries to evaluate the expanded wildcard. In a dir with the file "bar" it would try to execute "bar" from the bin path. I've never seen whitespace after = in production code and consider it an syntax pitfall. – sapht Sep 14 '12 at 10:38
  • @sapht Thanks for providing the rationale. +1 – sehe Sep 14 '12 at 11:18
  • @sapht: you can prefix simple commands with variable assignments to effect changes to that commnands environments. `FOO= cmd` simply runs `cmd` with `FOO` set to the empty string in its environment; `FOO` in the current environment is left unchanged. – chepner Sep 14 '12 at 13:03
2

You need to do this changes in your code:

#!/bin/bash
#append csv

FILES=/some_path/*

for f in $FILES
do
        cat $f >> ~/test.txt
done

Don't forget to remove whitepsace before path in FILE line,

Laser
  • 6,652
  • 8
  • 54
  • 85
0

Missing statement delimiter: Fix it either like

for f in $FILES do
        cat $f >> ~/test.txt 
done

Or

for f in $FILES do
        cat $f >> ~/test.txt; done
sehe
  • 374,641
  • 47
  • 450
  • 633
0

Note that if any of your filenames have whitespace, you will have problems. When you iterate over a variable with for, you are iterating over the whitespace-separated words in that variable. To safely iterate over files, regardless whether they have spaces in the names, don't store the list of files in a plain variable.

Most straight-forward way:

for f in /home/stef/test/*; do
  # ...

If you want to hold the filenames in some kind of container, you need to use an array:

files=( /home/stef/test/* )
for f in "${files[@]}"; do
  # ...
glenn jackman
  • 238,783
  • 38
  • 220
  • 352