[英]Linux bash script file concatenation
Hi in my script i've concatenated two files into a new file called output.txt. 嗨,在我的脚本中,我将两个文件串联成一个名为output.txt的新文件。 Am having trouble checking that output.txt file does exist to then print a "concatenation successful" message.
无法检查是否确实存在output.txt文件,然后无法打印“连接成功”消息。 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
Should be: 应该:
file3="output.txt"
cat $file1 $file2 >> $file3
if [ -f $file3 ]; then
echo "concatenation of files successful"
fi
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. 将使
$file3
存在。
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. 因此,您的文件在第一次操作后将始终存在。
单行
cat $file1 $file2 >> output.txt && echo 'Success' || echo 'Failed'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.