带有 fdisk 的 Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35166147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:11:11  来源:igfitidea点击:

Bash script with fdisk

linuxbash

提问by Ele Munjeli

I came across this bash script to expand the fs after making the root volume larger on an ami made with Packer. Can someone please explain the meaning of the fdisk options in the heredoc?

在使用 Packer 制作的 ami 上使根卷变大后,我遇到了这个 bash 脚本来扩展 fs。有人可以解释一下heredoc中fdisk选项的含义吗?

#!/bin/bash
fdisk /dev/xvda <<EEOF
d
n
p
1
1

w
EEOF
exit 0

Thank you!

谢谢!

采纳答案by Charles Duffy

To determine what these mean, look at the built-in help from fdisk. Details may differ based on your implementation; for mine, that looks like this:

要确定这些含义,请查看fdisk. 详细信息可能因您的实施而异;对我来说,看起来像这样:

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

...so:

...所以:

  • ddeletes a partition (presumably your script was developed for a version of fdiskwhere if there's only one partition, there's no prompt over which to delete).
  • ncreates a new partition.
    • pindicates that it's a primary partition being created.
    • 1indicates that it should be primary partition #1
    • 1indicates that it should start at sector #1
    • the following blank line accepts the default end sector
  • wwrites changes to disk.
  • d删除一个分区(大概你的脚本是为一个版本开发的fdisk,如果只有一个分区,没有提示删除哪个)。
  • n创建一个新分区。
    • p表示这是一个正在创建的主分区。
    • 1表示它应该是主分区 #1
    • 1表示它应该从扇区 #1 开始
    • 下面的空行接受默认的结束扇区
  • w将更改写入磁盘。

回答by Jose Carlos Ramos Carmenates

Try this and adjust for your conditions:

试试这个并根据您的条件进行调整:

#!/bin/bash

HEREDOC_VAR_1='p q ' 
echo $HEREDOC_VAR_1

HEREDOC_VAR_2='n q ' 
echo $HEREDOC_VAR_2

echo "$HEREDOC_VAR_1" | fdisk /dev/xvda
echo "$HEREDOC_VAR_2" | fdisk /dev/xvda