Assigning a valid path value to a bash string variable
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
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: