gpt4 book ai didi

linux - Bash - 命令调用移植到内部有另一个变量的变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:34:28 25 4
gpt4 key购买 nike

我认为这对我来说是一个简单的语法问题,但我无法找到另一个与我正在尝试做的类似的示例。我有一个接受特定磁盘位置的变量,我需要在 hdparm/grep 命令中使用该位置来提取最大 LBA

targetDrive=$1   #/dev/sdb

maxLBA=$(hdparm -I /dev/sdb |grep LBA48 |grep -P -o '(?<=:\s)[^\s]*') #this works perfect

maxLBA=$(hdparm -I $1 |grep LBA48 |grep -P -o '(?<=:\s)[^\s]*') #this fails

我也试过

maxLBA=$(hdparm -I 1 |grep LBA48 |grep -P -o '(?<=:\s)[^\s]*')
maxLBA=$(hdparm -I "$1" |grep LBA48 |grep -P -o '(?<=:\s)[^\s]*')

感谢帮助

最佳答案

所以我认为这是您问题的解决方案。我做的基本上和你一样,但改变了我将结果传递给另一个人的方式。

  • 用正则表达式grep查找包含LBA48的行
  • 当结果字符串除以“:”列时,剪切以检索第二个字段
  • 然后从结果中删除所有的leasing 空格

这是我生成的 bash 脚本。

#!/bin/bash

target_drive=$1
max_lba=$(sudo hdparm -I "$target_drive" | grep -P -o ".+LBA48.+:.+(\d+)" | cut -d: -f2 | tr -d ' ')

echo "Drive: $target_drive MAX LBA48: $max_lba"

关于linux - Bash - 命令调用移植到内部有另一个变量的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21915146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com