Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Cat文件执行从文件输出追加到第一个文件的操作_Linux_Bash_Cat - Fatal编程技术网

Linux Cat文件执行从文件输出追加到第一个文件的操作

Linux Cat文件执行从文件输出追加到第一个文件的操作,linux,bash,cat,Linux,Bash,Cat,我正在努力实现以下目标: #!/bin/bash if [ -f file2 ]; then cat file2 > file1 sed -i 's#some operation' file1 cat file3 >> file1 exit 0 elif [ ! -f file2 ]; then cat file1 > file2 sed -i 's#some operation' file1 cat file3 >&

我正在努力实现以下目标:

  #!/bin/bash
  if [ -f file2 ]; then

  cat file2 > file1
  sed -i 's#some operation' file1
  cat file3 >> file1

  exit 0

  elif [ ! -f file2 ]; then

  cat file1 > file2
  sed -i 's#some operation' file1
  cat file3 >> file1

  exit 0

  else

  echo "something"
  exit 1

  fi
有没有办法让它更简单? 不用那么多猫和文件


谢谢

对于您最初的问题:

sed -i 's# some operation' file1
cat file2 >> file1
cat file1 > file1.bak
sed -i 's# some operation' file1.bak
cat file2 >> file1.bak
echo -n > file1
cat file1.bak > file1
rm -f file1.bak
更简单的答案是(正如我在评论中所说):

对于已编辑的问题-更多解释

在你的情况下:

#!/bin/bash
if [ -f file2 ]; then
...
elif [ ! -f file2 ]; then
...
else
...
fi
否则
永远不会发生。如果
文件1
存在并且是常规文件。
if-then
将运行,如果您否定上述操作,则将运行
elif-then
。你可以把它简化为

if [ -f file2 ]; then
...
else
...
fi
现在谈谈行动:

委员会:

同:

sed 's#some operation' <file2 >file1
cat file3 >> file1
正常-您正在创建
file1
file2
的备份(副本)。这也可以写成
cp file1 file2

比较这两个部分,您可以看到两个部分都在做相同的事情:

cat file3 >> file1
所以,擦干(不要重复自己的操作)-在
之后进行,因为这两个部分都很常见

因此,我们得到:

if [ -f file2 ]; then
    sed 's#some operation' <file2 >file1
else
    cp file1 file2  #make the copy
    sed -i 's#some operation' file1
fi
cat file3 >> file1
也可以写成

cp file1 file2
sed 's#some operation' <file2 >file1
但是,
什么都不做
部分是不必要的,因此我们得到:

if [ ! -f file2 ]; then
    cp file1 file2  #make the copy
fi
sed 's#some operation' <file2 >file1
cat file3 >> file1
if[!-f文件2];然后
cp文件1文件2#复制
fi
sed的“某些操作”文件1
cat文件3>>文件1
这可以使用
[[condition]]&&……
缩短,但目前不需要。:)


此外,更准确地命名您的文件可能会非常有帮助。
file1
等等-不告诉您任何内容。

sed-i的#一些操作“file1;cat file2>>file1
?您的
echo-n>file1
在这里做什么?这似乎是多余的,但我猜这是我还没有抓住的奇怪把戏之一。真的,对不起,我的白痴。。。我只是重新思考了一下,这才是我真正需要实现的。真的,对不起,我的白痴。。。我只是重新思考了一下,这才是我真正需要实现的。如果你介意并有时间的话,我还有一个问题需要你的解释:@DaWe4444-享受,你也可以阅读。
if [ -f file2 ]; then
    sed 's#some operation' <file2 >file1
else
    cp file1 file2  #make the copy
    sed -i 's#some operation' file1
fi
cat file3 >> file1
sed 's#some operation' <file2 >file1
#and
sed -i 's#some operation' file1
cat file1 > file2
sed -i 's#some operation' file1
cp file1 file2
sed 's#some operation' <file2 >file1
if [ -f file2 ]; then
    : #do nothing here...
else
    cp file1 file2  #make the copy
fi
sed 's#some operation' <file2 >file1
cat file3 >> file1
if [ ! -f file2 ]; then
    cp file1 file2  #make the copy
fi
sed 's#some operation' <file2 >file1
cat file3 >> file1