Удалить последнее вхождение строки в bash - PullRequest
0 голосов
/ 22 апреля 2020

Есть файл с именем /etc/rc.local, который заканчивается exit 0. Мне нужно добавить некоторый код в файл, но exit 0 препятствует его выполнению.

Используя sed, awk и / или tac, как я могу удалить только последний exit 0, чтобы можно было добавить еще один скрипт в конец файла?

r c .local файл до запуска моего скрипта:

if [ -e /boot/disable_rc_setup.txt]; then
  exit 0
fi

if [ -e /boot/setup.txt]; then
  bash /home/pi/setup.sh
  rm /boot/setup.txt
fi

exit 0

Текущий r c .local после запуска сценария:

if [ -e /boot/disable_rc_setup.txt]; then
  exit 0
fi

if [ -e /boot/setup.txt]; then
  bash /home/pi/setup.sh
  rm /boot/setup.txt
fi

exit 0

curl 192.168.1.5/online
if [ -e /boot/first_boot.txt]; then
  curl 192.168.1.5/first_boot
  rm /boot/first_boot.txt
fi

exit 0

Вы можете увидеть exit 0 над начальной командой curl.

Желаемая цель r c .local:

if [ -e /boot/disable_rc_setup.txt]; then
  exit 0
fi

if [ -e /boot/setup.txt]; then
  bash /home/pi/setup.sh
  rm /boot/setup.txt
fi

curl 192.168.1.5/online
if [ -e /boot/first_boot.txt]; then
  curl 192.168.1.5/first_boot
  rm /boot/first_boot.txt
fi

exit 0

Ответы [ 2 ]

2 голосов
/ 22 апреля 2020

Вместо выполнения echo "CODE" >> /etc/rc.local попробуйте:

sed -i '/^exit 0/d' /etc/rc.local
echo "CODE" >> /etc/rc.local

Будет удалена только последняя строка кода, содержащая exit 0, поскольку в начале нет завершающих пробелов. Если вы не уверены, что так будет всегда, измените код, чтобы удалить только последний случай exit 0.

1 голос
/ 22 апреля 2020

как заменить последнее вхождение строки exit 0 с помощью awk. Немного изменился rc.local с нежелательным кодом после последнего exit 0:

$ cat rc.local
if [ -e /boot/disable_rc_setup.txt]; then
  exit 0
fi

if [ -e /boot/setup.txt]; then
  bash /home/pi/setup.sh
  rm /boot/setup.txt
fi

exit 0

# unwanted code commented out with the above exit

The awk:

$ awk '{   
    a[NR]=$0                            # hash records to a index on NR
    if($0~/^ *exit +0 *$/ && NR==FNR)   # remember last exit 0 of the 1st file
        nope=NR
    if(NR==FNR)                         # also remember where the 1st file ended
        last=NR
}
END {
    for(i=1;i<nope;i++)                 # output upto last line before the exit
        print a[i]
    for(i=last+1;i<=NR;i++)             # then output the replacement 
        print a[i]
    for(i=nope;i<=last;i++)             # and from exit to the end of 1st file
        print a[i]
}' rc.local code_to_append

Вывод:

if [ -e /boot/disable_rc_setup.txt]; then
  exit 0
fi

if [ -e /boot/setup.txt]; then
  bash /home/pi/setup.sh
  rm /boot/setup.txt
fi

curl 192.168.1.5/online
if [ -e /boot/first_boot.txt]; then
  curl 192.168.1.5/first_boot
  rm /boot/first_boot.txt
fi
exit 0

# unwanted code commented out with the above exit
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...