简体   繁体   English

解析bash命令结果的一部分以将其存储到变量中

[英]Parse a part of the result of a bash command to store it into a variable

My question is really a bash question even if it talks about mounting a disk. 我的问题实际上是一个bash问题,即使它涉及安装磁盘。

During the creation of a VM, by a script, I mount this disk: 在创建VM的过程中,通过脚本安装了该磁盘:

sudo mount -t ext4 /dev/sdb /data

And I would like to keep it every time my system start. 而且我想在每次系统启动时都保留它。 Make this mount become automatic. 使此安装变为自动。 I learnt that I had to edit the /etc/fstab file for that : append a line on it. 我了解到我必须为此编辑/etc/fstab文件:在其上添加一行。

When my disk is mounted, I have to run a command: 挂载磁盘后,必须运行命令:

$ sudo blkid /dev/sdb
/dev/sdb: UUID="238d1293-918e-42e1-a081-a41f497636d0" TYPE="ext4"

To get the UUID I need to mention in the line I append on my /etc/fstab file: 要获取UUID,我需要在我的/etc/fstab文件中附加的行中提及:

UUID=238d1293-918e-42e1-a081-a41f497636d0       /data      ext4    defaults      0       0

My question is: May I parse by bash the result of the blkid command, catch the part UUID="238d1293-918e-42e1-a081-a41f497636d0" from the /dev/sdb: UUID="238d1293-918e-42e1-a081-a41f497636d0" TYPE="ext4" content and store it into a variable? 我的问题是:我可以通过bash解析blkid命令的结果,从/dev/sdb: UUID="238d1293-918e-42e1-a081-a41f497636d0" TYPE="ext4"捕获部分UUID="238d1293-918e-42e1-a081-a41f497636d0" /dev/sdb: UUID="238d1293-918e-42e1-a081-a41f497636d0" TYPE="ext4"内容并将其存储到变量中?

Use parameter substitution: 使用参数替换:

uuid=$(sudo blkid /dev/sdb)
uuid=${uuid#*UUID=\"}  # Remove from left up to UUID="
uuid=${uuid%%\"*}      # Remove from right from the leftmost "
echo "$uuid"

Bash's built-in regex support is suited to task. Bash的内置正则表达式支持适用于任务。 In the below function, we're testing the output of blkid against the regex UUID="([^"]+)" , and emitting the match group contents (everything inside the parenthesis) if a match is found: 在下面的函数中,我们针对正则表达式UUID="([^"]+)"测试blkid的输出,如果找到匹配项,则发出匹配组内容(括号内的所有内容):

uuid_for_device() {
  local uuid_re blkid_text      # Declare our locals so we don't leak into global scope
  uuid_re='UUID="([^"]+)"'      # Save the regex to a variable; less gotchas this way
  blkid_text=$(sudo blkid "$1") || return # Collect the data we're going to match against
  [[ $blkid_text =~ $uuid_re ]] && echo "${BASH_REMATCH[1]}" # Emit output if regex matches
}

...will emit your desired UUID given uuid_for_device /dev/sda , which you can capture into a variable as usual ( sda_uuid=$(uuid_for_device /dev/sda) ). ...将在给定uuid_for_device /dev/sda发出所需的UUID,您可以像往常一样将其捕获到变量中( sda_uuid=$(uuid_for_device /dev/sda) )。


That said, for your real-world use case, you're better off just using a more appropriate tool for the job: 也就是说,对于您的实际用例,最好只使用一种更合适的工具来完成工作:

uuid_for_device() { findmnt -n -o UUID "$1"; }

sda_uuid=$(uuid_for_device /dev/sda)

Or, of course, simply: 或者,当然,简单地:

sda_uuid=$(findmnt -n -o UUID /dev/sda)

Use sed: 使用sed:

YOUR_COMMAND | sed -e 's/.*UUID="\([0-9a-f-]*\)".*/\1/'

You can use backticks for example to store it: 您可以使用反引号来存储它:

a=`YOUR_COMMAND | sed -e 's/.*UUID="\([0-9a-f-]*\)".*/\1/'`

echo $a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM