gpt4 book ai didi

bash - 如何在 Linux for 循环中设置变量

转载 作者:行者123 更新时间:2023-11-29 09:12:28 25 4
gpt4 key购买 nike

我正在尝试使用 for 循环确定每个系统中的现有 HDD,如下所示,问题是当我尝试使用以下代码设置变量时我得到 sda=true: command not found。执行此操作的正确方法是什么?

#!/bin/bash
for i in a b c d e f
do
grep -q sd$i /proc/partitions
if [ $? == 0 ]
then
sd$i=true
else
sd$i=false
fi
done

最佳答案

您需要使用数组或声明:

declare sd$i=true

关于bash - 如何在 Linux for 循环中设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5237587/

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