在我的腳本中,我已經定義了以下變數:GRUB_HG=53737
我正在嘗試替換此檔案的內容:
cat grub.text
{...}
GRUB_CMDLINE_LINUX="random_text_here"
我sed用來替換以GRUB_CMDLINE_LINUX=* 開頭的任何內容。
當我使用以下sed腳本時,我將“random_text_here”替換為一個長字串。此字串包含變數$GRUB_HG,但是,當我運行腳本時,我能夠成功替換“random_text_here”,但$GRUB_HG沒有被之前定義的53737替換。
Script:
sed -i 's|\GRUB_CMDLINE_LINUX=.*|GRUB_CMDLINE_LINUX="hugepages=${GRUB_HG} processor.max_cstates=1 idle=poll pcie_aspm=off intel_iommu=on"|' grub.text
這是命令后檔案grub.text的樣子sed:
#[ACTUAL OUTPUT]
cat grub.text
{...}
GRUB_CMDLINE_LINUX="hugepages=${GRUB_HG} processor.max_cstates=1 idle=poll pcie_aspm=off intel_iommu=on"
我試圖得到這樣的東西:
#[DESIRE OUTPUT]
cat grub.text
{...}
GRUB_CMDLINE_LINUX="hugepages=53737 processor.max_cstates=1 idle=poll pcie_aspm=off intel_iommu=on"
uj5u.com熱心網友回復:
您需要使用雙引號才能在sed命令中插入 bash 變數。由于您的替換字串中有雙引號,您需要轉義這些:
cat grub.text
GRUB_CMDLINE_LINUX="random_text_here"
GRUB_HG=53737 sed -i "s|\GRUB_CMDLINE_LINUX=.*|GRUB_CMDLINE_LINUX=\"hugepages=${GRUB_HG} processor.max_cstates=1 idle=poll pcie_aspm=off intel_iommu=on\"|" grub.text
cat grub.text
GRUB_CMDLINE_LINUX="hugepages=53737 processor.max_cstates=1 idle=poll pcie_aspm=off intel_iommu=on"
uj5u.com熱心網友回復:
另一種選擇,如果您有興趣...
將 GRUB_CMDLINE_LINUX 的每個引數放入一個檔案中,每行一個。每當您獲得新的時,只需根據您的選擇將其添加到頂線或底線。
hugepages=53737
processor.max_cstates=1
idle=poll
pcie_aspm=off
intel_iommu=on
然后,當您準備好將它們全部添加到變數中時...
cat parameter_file | tr '\n' ' ' > p2
cat << EOF > ed1
1
s/^/GRUB_CMDLINE_LINUX="/
s/$/"/
1W grub.text
wq
EOF
ed -s p2 < ed1
rm -v ./ed1
這會將您的變數放在檔案的末尾,但是從那里,您可以使用 Ed 或 vim 將其移動到您需要的任何行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/506942.html
上一篇:如何將三次貝塞爾曲線細分為點
