Assigning a valid path value to a bash string variable

Asked by Stephen

The below code gets "ambiguous redirect" error when the variable $target is used to target a file to append another file to. As can be seen by the commented out code I have attempted code variations to find the right way to do this. I always get the same error.

What is the right way to assign the $target variable a value that works as a target of an append operation?

#! /bin/bash

# Description:

# Merges text file directories in this way:

# A filename that is in both the source, and target, directories will have content
# from the source directory's files appended to the content in the the target directory.

# File names in the source directory, and not the target directory, will be copied into
# the target directory.

# Command line arguments:
# $1: The source directory containing the files to merged into the $2 directory's files.
# $2: The target directory containing the files to be merged with the $1 directory's files.

cd $1; # Make the source the current directory to avoid having to separate filename from its path inside the loop.

for file in *.txt; do
    if [ -f "$file" ]; then

        #target="${2}/${file}"
        target=\'${2}/${file}\'
        echo $target

        #printf "\n \'%s/%s\'\n" "$2" "$file"

        #$file >> $2/$file;
        #$file >> "$target";
        $file >> $target; # Error on this line: "$target: ambiguous redirect"
    fi
done

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Stephen
Solved:
Last query:
Last reply:
Revision history for this message
Manfred Hampl (m-hampl) said :
#1

Are you sure that you didn't forget adding a "cat" command at the beginning of all variants of "$file >> $target" lines?

The syntax is
command >> filename
not
filename >> filename

Revision history for this message
Stephen (artist-wavenet) said :
#2

Thank you for your reply. I did not realize that to the left of >> there must be a command's output, not a file.

I changed the line from:

$file >> $target;

To:

cat $file >> $target;

and on that line I still get the same error.

Revision history for this message
Manfred Hampl (m-hampl) said (last edit ):
#3

Is there eventually a space character in one of the file names?

Revision history for this message
ranasab (ranasab) said :
#4

Are you certain that there isn't a missing "cat" command preceding each instance of "$file >> $target" in all lines?
The correct syntax is "command >> filename," not "filename >> filename.

Revision history for this message
Stephen (artist-wavenet) said :
#5

This problem is solved. There were space characters in the file names. Bash uses space characters to delimit arguments, even in the case they are within a string variable. The solution is to put the string variables inside double quotation marks. The corrected code:

cd "$1"
for file in *.txt; do
    if [ -f "$file" ]; then
        cat "$file" >> "${2}"/"${file}"
    fi
done