Linux shell脚本;改为;命令

Linux shell脚本;改为;命令,linux,bash,shell,command,sh,Linux,Bash,Shell,Command,Sh,所以,我对脚本编写还不熟悉,我遇到了一些问题。我需要执行的命令是: read -p Enter_the_DEVICE_Bssid "device1" ; read -p Enter_the_DEVICE_Bssid "device2" ; read -p Enter_the_DEVICE_Bssid "device3" 该命令有效,但当我将其设置为变量时,即: com="read -p Enter_the_DEVICE_Bssid "device1" ; read -p Enter_th

所以,我对脚本编写还不熟悉,我遇到了一些问题。我需要执行的命令是:

read -p Enter_the_DEVICE_Bssid "device1" ; 
read -p Enter_the_DEVICE_Bssid "device2" ; 
read -p Enter_the_DEVICE_Bssid "device3"
该命令有效,但当我将其设置为变量时,即:

com="read -p Enter_the_DEVICE_Bssid "device1" ; 
read -p Enter_the_DEVICE_Bssid "device2" ; 
read -p Enter_the_DEVICE_Bssid "device3"" 
并将其作为$com执行,因为它不起作用。可能是因为read命令试图将我的输入设置为变量device1和。
关于如何修复它有什么想法吗?

您在shell扩展的顺序上遇到了问题

一个简单的例子:

$ command='echo one ; echo two'
$ $command
one ; echo two
$command
值中的分号作为
echo
参数的一部分,而不是作为两个
echo
命令之间的分隔符

也许有一种方法可以解决这个问题,让它按照您想要的方式工作,但是为什么要麻烦呢?只需定义一个shell函数。使用我的简单示例:

$ command() { echo one ; echo two ; }
$ command
one
two
$ 
或使用您的:

com() {
    read -p "Enter_the_DEVICE_Bssid: " device1
    read -p "Enter_the_DEVICE_Bssid: " device2
    read -p "Enter_the_DEVICE_Bssid: " device3
}

请注意,我在提示的末尾添加了“:”。我还删除了变量名周围不必要的分号和引号(因为参数必须是有效的变量名,所以不需要引用)。

您没有完成引号

 com="read -p Enter_the_DEVICE_Bssid "device1"
引号总是寻找一对,而你却错过了

> com="read -p Enter_the_DEVICE_Bssid:  device1"
> $com
Enter_the_DEVICE_Bssid:abc123
> echo $device1
abc123

这里我使用的是bash shell。

执行为
bash-c“$com”
为什么要尝试混合数据和代码?也许您需要阅读上的手册部分,或者也可以参阅,(U&L.SE),(SO),(AskU)等。