我想将文本字符串从文件传递到对话框-使用bash的菜单

问题描述 投票:0回答:1

我正在尝试获取此文本字符串:

0 "Item: Bitterweed, Amount: 4" 1 "Item: Agarita, Amount: x" 2 "Item: Wild Rhubarb, Amount: x" 3 "Item: Cardinnal Flower, Amount: x" 4 "Item: Creek Plum, Amount: x" 5 "Item: Blood Flower, Amount: " 6 "Item: Chocolate Daisy, Amount: x" 7 "Item: Wisteria, Amount: x"

以bash传递给对话框应用程序。

我正在尝试以此创建菜单。

字符串由函数更新。

该字符串放在变量$ mitem中。

然后我跑

dialog --menu "Choose item to modify" 15 55 5 $mitem

但是这一直告诉我“ 错误:预期有2个参数,找到了另外的1。

当我复制变量中的字符串并在没有$ mitem变量的情况下运行对话框时,它确实起作用。

dialog --menu "Choose item to modify" 15 55 5 0 "Item: Bitterweed, Amount: 4" 1 "Item: Agarita, Amount: x" 2 "Item: Wild Rhubarb, Amount: x" 3 "Item: Cardinnal Flower, Amount: x" 4 "Item: Creek Plum, Amount: x" 5 "Item: Blood Flower, Amount: " 6 "Item: Chocolate Daisy, Amount: x" 7 "Item: Wisteria, Amount: x"

任何人都可以向我解释为什么这对我的变量不起作用,但是当我手动添加字符串时起作用,例如,在这种情况下可以正常工作:

var=$(df -hT | awk '{print v++,$7}')
dialog --menu "Please choose a mounted Partition" 15 55 5 $var

我谢谢你。

更完整的图片:

echo "\"Item: $item, Amount: $amount\"" >> ~/tmp2

cat ~/tmp2
"Item: Bitterweed, Amount: 4"
"Item: Agarita, Amount: x"
"Item: Wild Rhubarb, Amount: x"
"Item: Cardinnal Flower, Amount: x"
"Item: Creek Plum, Amount: x"
"Item: Blood Flower, Amount: "
"Item: Chocolate Daisy, Amount: x"
"Item: Wisteria, Amount: x"

mitem=$(gawk '{print v++,$0}' ~/tmp2)     

echo $mitem
0 "Item: Bitterweed, Amount: 4" 1 "Item: Agarita, Amount: x" 2 "Item: Wild Rhubarb, Amount: x" 3 "Item: Cardinnal Flower, Amount: x" 4 "Item: Creek Plum, Amount: x" 5 "Item: Blood Flower, Amount: " 6 "Item: Chocolate Daisy, Amount: x" 7 "Item: Wisteria, Amount: x"

dialog --menu "Choose item to modify" 15 55 5 $mitem
bash shell dialog
1个回答
0
投票

由于传递给dialog的某些参数包含空格(例如"Item: Bitterweed, Amount: 4",所以我会将参数准备为数组,

mitem=(0 "Item: Bitterweed, Amount: 4" ....)

并使用]进行插值>

dialog --menu "Choose item to modify" 15 55 5 "${mitem[@]}"

以确保在数组元素内不会发生单词拆分。

© www.soinside.com 2019 - 2024. All rights reserved.