Reputation: 1
Hi in my script i've concatenated two files into a new file called output.txt. Am having trouble checking that output.txt file does exist to then print a "concatenation successful" message. The concatenation appears to work and create a new file.
cat $file1 $file2 >> output.txt
file3="$output.txt" #incorrect?
if [ -e $file3 ]
then
echo "concatenation of files successful"
fi
Upvotes: 0
Views: 106
Reputation: 774
In single line
cat $file1 $file2 >> output.txt && echo 'Success' || echo 'Failed'
Upvotes: 0
Reputation: 672
file3="output.txt"
cat $file1 $file2 >> $file3
if [ $? == 0 ]; then
echo "concatenation of files successful"
fi
Checking the file's existence doesn't mean that the files concatenated successfully. It means that the file exists.
Consider that:
cat $file1 $file2(missing) >> $file3
cat $file1(missing) $file2 >> $file3
would make $file3
exist.
Checking last operation exit value with $?
accounts for the whole operation working successfully.
Also, unless you're specifically looking to append >>
to existing file, you will ALWAYS append. So your file will always exist after the first operation.
Upvotes: 1
Reputation: 1304
Should be:
file3="output.txt"
cat $file1 $file2 >> $file3
if [ -f $file3 ]; then
echo "concatenation of files successful"
fi
Upvotes: 1