user1345883
user1345883

Reputation: 451

linux appending text gives command not found

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?

Upvotes: 1

Views: 1390

Answers (4)

glenn jackman
glenn jackman

Reputation: 247210

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
  # ...

Upvotes: 0

sehe
sehe

Reputation: 393934

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

Upvotes: 0

sapht
sapht

Reputation: 2829

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.

Upvotes: 5

Laser
Laser

Reputation: 6960

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,

Upvotes: 2

Related Questions