您现在的位置: 爱51代码网 >> 范文 >> 文章正文
将文件插入到文件的shell代码

al2文件插入到helloworld.py文件的的python2的后面,然后输出到文件combine.py。
可以用命令 awk '1;/python2/{while (getline < "al2")print}' helloworld.py > combine.py。

但是,我把al2换成变量,怎么就出错了呢?
脚本addal2.sh
#!/bin/bash

set -e
set -o xtrace

TOPDIR=$(cd $(dirname "$0")&& pwd)
file= $TOPDIR/helloworld.py
temp= $TOPDIR/combine.py

al2= $TOPDIR/al2

awk '1;/python2/{while (getline < "$al2")print}' $file  > $temp

set +o xtrace

helloworld.py内容
#!/usr/bin/python2

print "Hello,world!"
name = raw_input("What is your name?")
print "Hello, "+ name + "!"

al2的内容
#Licensed to the Apache Software Foundation (ASF) under one
#or more contributor license agreements.  See the NOTICE file
#distributed with this work for additional information
#regarding copyright ownership.  The ASF licenses this file
#to you under the Apache License, Version 2.0 (the
#"License"); you may not use this file except in compliance
#with the License.  You may obtain a copy of the License at

#  #Unless required by applicable law or agreed to in writing,
#software distributed under the License is distributed on an
#"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#KIND, either express or implied.  See the License for the
#specific language governing permissions and limitations
#under the License.

xtrace output说明al2是空值,所以应该检查看你的al2赋值是如何做的

这是我的的版本,楼主比较一下

#!/bin/bash

set -e
set -o xtrace

TOPDIR=$(cd $(dirname "$0")&& pwd)
file="$TOPDIR/helloworld.py"
temp="$TOPDIR/combine.py"

al2="$TOPDIR/al2"

awk "1;/python2/{while (getline < \"$al2\")print}" $file

set +o xtrace

$./addal2.sh
+++ dirname ./addal2.sh
++ cd .
++ pwd
+ TOPDIR=/var/temp
+ file=/var/temp/helloworld.py
+ temp=/var/temp/combine.py
+ al2=/var/temp/al2
+ awk '1;/python2/{while (getline < "/var/temp/al2")print}' /var/temp/helloworld.py
#!/usr/bin/python2
#Licensed to the Apache Software Foundation (ASF) under one
#or more contributor license agreements.  See the NOTICE file
#distributed with this work for additional information
#regarding copyright ownership.  The ASF licenses this file

print "Hello,world!"
name = raw_input("What is your name?")
print "Hello, "+ name + "!"
+ set +o xtrace

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    maven如何加自定义的包
    redhat 2.6 (santigo 5.6) vsftp
    shell如何实现自动填写操作执行下
    linux shell 文件配置sh:color:
    shell script语法一定要加path吗
    SecureCRT如何访问虚拟机vmWare中
    C#如何读取WINDOWS的放大系数
    cximge的图如何存入数据库并提取
    DBGRID控件显示查询结果文本类型
    TChart控件如何把表中右边的系列
    maven如何加自定义的包
    redhat 2.6 (santigo 5.6) vsftp
    shell如何实现自动填写操作执行下
    linux shell 文件配置sh:color:
    shell script语法一定要加path吗
    SecureCRT如何访问虚拟机vmWare中
    C#如何读取WINDOWS的放大系数
    cximge的图如何存入数据库并提取
    DBGRID控件显示查询结果文本类型
    TChart控件如何把表中右边的系列
    pcre-config for libpcre no
    makefile每运行一个命令都会
    mkdir()每天建一个以日期位名
    扩展lvm时分配空间为0
    error: implicit declaratio
    SMTP server problem No rou
    怎么用sed或awk来进行替换XM
    三星4412开发板uboot烧写板子
    redhat 2.6 (santigo 5.6) v
    shell如何实现自动填写操作执
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |