如何替换文件中的字符串

时间:2017-06-16 12:27:48

标签: linux bash shell

我正在编写shell脚本,它将列出驱动程序.ko文件并在init文件中添加条目。文件来自initramfs。

所以下面我在脚本中做了一些事情,我尝试了谷歌,但解决方案没有工作,所以最后我不得不发帖提问。

几个问题,

1)我试图在变量中存储带换行符的字符串,但是它使它成为一行,所以我使用不同的方法编写了一个具有预期数据的文件。

2)现在我想将带有新行字符的内容放在init文件的中间,这是在一些驱动程序条目下面所以我决定找到最后一个驱动程序, 然后用驱动程序替换我的内容,然后再将该驱动程序追加到文件中。

3)但是我无法替换文本,所以我再次使用不同的方法逐行编写新文件,当我发现驱动程序添加我的内容并将驱动程序追加到文件中。但是现在我保存在早期文件中的内容在一行中附加了没有换行符。

我不是shell的专家我正在用谷歌做这一切。下面是我的代码

    #!/bin/bash
    rm tmp.txt
    handled_drivers=()
    for driver in `cat lib/modules/2.6.18-419.el5/modules.dep | tr -d ":" | tr " " "\n"`
    do
            handled=0
            for d in ${handled_drivers[@]}
            do
                    if [ "$d" == "$driver" ]
                    then
                       handled=1
                    fi
            done
            if [ $handled -eq 0 ]
            then
                 handled_drivers+=($driver)
                 echo "insmod $driver" >> tmp.txt
                 echo "echo \"Loading $(basename "$driver") module\"" >> tmp.txt
            fi
    done

    value=`cat tmp.txt`

    find_last_driver=`cat initcopy | grep insmod | tail -1| tr -d "\n"`

    #
    #if [[ -z $find_last_driver ]];then
    #   echo "Not Found driver skip injection"
    #else
    #   echo "here $find_last_driver"
    #   sed -i -e 's/$find_last_driver/$value/g' ./a.txt
    #fi

    while IFS= read -r line;do
        if [ "$line" == "$find_last_driver" ]
        then
        echo $value >>  init_new
        echo $line >> init_new
    else
        echo $line >> init_new
    fi
done < "init"

以下是输出文件的内容:

#!/bin/nash

mount -t proc /proc /proc
setquiet
echo Mounting proc filesystem
echo Mounting sysfs filesystem
mount -t sysfs /sys /sys
echo Creating /dev
mount -o mode=0755 -t tmpfs /dev /dev
mkdir /dev/pts
mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
mkdir /dev/shm
mkdir /dev/mapper
echo Setting up hotplug.
hotplug
insmod /lib/uhci-hcd.ko
mount -t usbfs /proc/bus/usb /proc/bus/usb
echo "Loading jbd.ko module"
insmod /lib/jbd.ko
echo "Loading ext3.ko module"
insmod /lib/ext3.ko
echo "Loading scsi_mod.ko module"
insmod /lib/dm-message.ko
echo "Loading dm-raid45.ko module"
insmod /lib/modules/2.6.18-419.el5/kernel/drivers/ata/ata_piix.ko echo "Loading ata_piix.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/ata/libata.ko echo "Loading libata.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/scsi_mod.ko echo "Loading scsi_mod.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/crypto/crypto_algapi.ko echo "Loading crypto_algapi.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/crypto/crypto_api.ko echo "Loading crypto_api.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-crypt.ko echo "Loading dm-crypt.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-mod.ko echo "Loading dm-mod.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-emc.ko echo "Loading dm-emc.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-multipath.ko echo "Loading dm-multipath.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/device_handler/scsi_dh.ko echo "Loading scsi_dh.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-hp-sw.ko echo "Loading dm-hp-sw.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-log.ko echo "Loading dm-log.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-mem-cache.ko echo "Loading dm-mem-cache.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-message.ko echo "Loading dm-message.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-mirror.ko echo "Loading dm-mirror.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-raid45.ko echo "Loading dm-raid45.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-region_hash.ko echo "Loading dm-region_hash.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-rdac.ko echo "Loading dm-rdac.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-round-robin.ko echo "Loading dm-round-robin.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-snapshot.ko echo "Loading dm-snapshot.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/md/dm-zero.ko echo "Loading dm-zero.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/usb/net/dm9601.ko echo "Loading dm9601.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/usb/net/usbnet.ko echo "Loading usbnet.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/dma/dmaengine.ko echo "Loading dmaengine.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/hwmon/dme1737.ko echo "Loading dme1737.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/i2c/i2c-core.ko echo "Loading i2c-core.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/net/tulip/dmfe.ko echo "Loading dmfe.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/mpt2sas/mpt2sas.ko echo "Loading mpt2sas.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/scsi_transport_sas.ko echo "Loading scsi_transport_sas.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptbase.ko echo "Loading mptbase.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptctl.ko echo "Loading mptctl.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptfc.ko echo "Loading mptfc.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptscsih.ko echo "Loading mptscsih.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptlan.ko echo "Loading mptlan.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptsas.ko echo "Loading mptsas.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/message/fusion/mptspi.ko echo "Loading mptspi.ko module" insmod /lib/modules/2.6.18-419.el5/misc/pvscsi.ko echo "Loading pvscsi.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/device_handler/scsi_dh_alua.ko echo "Loading scsi_dh_alua.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/device_handler/scsi_dh_emc.ko echo "Loading scsi_dh_emc.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/scsi/device_handler/scsi_dh_rdac.ko echo "Loading scsi_dh_rdac.ko module" insmod /lib/modules/2.6.18-419.el5/kernel/drivers/pci/hotplug/shpchp.ko echo "Loading shpchp.ko module" 
insmod /lib/dm-raid45.ko
echo Waiting for driver initialization.
stabilized --hash --interval 1000 /proc/scsi/scsi
mkblkdevs
echo Mounting root filesystem.
mount /sysroot
echo Setting up other filesystems.
setuproot
echo Switching to new root and running init.
switchroot

1 个答案:

答案 0 :(得分:1)

  

1)我试图在变量中存储带换行符的字符串,   但它使它成为一条线,所以我使用了不同的方法   写了一个包含预期数据的文件。

当变量包含空格标签换行符以避免拆分时:环绕双引号

    echo "$value" >>  init_new
    echo "$line" >> init_new

而不是

    echo $value >>  init_new
    echo $line >> init_new