代码之家  ›  专栏  ›  技术社区  ›  Bill Armstrong

使用sed编辑文件的bash脚本不会持续重新启动

  •  0
  • Bill Armstrong  · 技术社区  · 6 年前

    我已经写了一个脚本来迁移 boot 从microSD卡到USB驱动器的目录。这是一个树莓皮4项目。作为该脚本的一部分,有两个文件被更新以删除microSD引用,并使用USB thumb驱动器的UUID引用。脚本按以下方式运行: sudo .用于更新 /boot/cmdline.txt 和 /etc/fstab 文件如下:

    #!/bin/bash
    
    fCMDLINE=/boot/cmdline.txt
    fFSTAB=/etc/fstab
    
    cat $fCMDLINE
    sed -i -r -e 's/PARTUUID=([a-z]\S*)/PARTUUID='"$vPARTUUID"'/g' $fCMDLINE
    cat $fCMDLINE
    
    cat $fFSTAB
    sed -i -r -e 's/PARTUUID=([a-z]\S*)/\/dev\/disk\/by-uuid\/'"$vUUID"'/g' $fFSTAB
    cat $fFSTAB
    

    在运行整个脚本之后;发布文件如下:

    CMDLINE FILE
    
    #ORIGINAL
    console=serial0,115200 console=tty1 root=PARTUUID=d9b3f436-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
    
    #UPDATED
    console=serial0,115200 console=tty1 root=PARTUUID=0b1e4c33-0a73-4c26-aad2-03c1b5fd9266 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
    
    FSTAB FILE
    
    #ORIGINAL
    proc            /proc           proc    defaults          0       0
    PARTUUID=d9b3f436-01  /boot           vfat    defaults          0       2
    PARTUUID=d9b3f436-02  /               ext4    defaults,noatime  0       1
    # a swapfile is not a swap partition, no line here
    #   use  dphys-swapfile swap[on|off]  for that
    
    #UPDATED
    proc            /proc           proc    defaults          0       0
    /dev/disk/by-uuid/5bee13fa-5c62-45b0-91ed-12c544d4b528  /boot           vfat    defaults          0       2
    /dev/disk/by-uuid/5bee13fa-5c62-45b0-91ed-12c544d4b528  /               ext4    defaults,noatime  0       1
    # a swapfile is not a swap partition, no line here
    #   use  dphys-swapfile swap[on|off]  for that
    

    这两个文件的输出都是正确的,sed命令在替换uuid时似乎已按预期执行。但是,当我重新启动树莓皮的时候 fstab 文件是一个持续存在且未更新的文件。这只会在重新启动后发生。在此之前,文件是正确的,所有内容都按预期装载。

    #After Reboot
    
    pi@raspberrypi:~ $ sudo cat /etc/fstab
    proc            /proc           proc    defaults          0       0
    PARTUUID=d9b3f436-01  /boot           vfat    defaults          0       2
    PARTUUID=d9b3f436-02  /               ext4    defaults,noatime  0       1
    # a swapfile is not a swap partition, no line here
    #   use  dphys-swapfile swap[on|off]  for that
    

    这并不重要,但以下是两个具有权限的文件。

    /boot/
    -rwxr-xr-x 1 root root      191 Dec  8 01:01 cmdline.txt
    
    /etc/
    -rw-r--r--  1 root root     314 Sep 26 01:31 fstab
    

    预期的行为将是 fstab 文件以相同的方式保持重新启动 cmdline.txt 文件仍然存在。肯定有什么东西我错过了。有什么想法吗?

    使现代化

    我之前没有注意到,但上面的两个文件的日期不相同——9/26文件是Buster(9/26)图像的原始文件,没有更新。重新启动前,文件是:

    -rw-r--r--  1 root root     382 Dec  8 18:16 fstab
    

    令人困惑——这让我觉得这是一个硬件问题,或者是一个更深层次的操作系统错误。

    0 回复  |  直到 6 年前
    推荐文章