Macos Makefile一直在搞乱BASH变量

Macos Makefile一直在搞乱BASH变量,macos,bash,makefile,Macos,Bash,Makefile,如果我在Makefile中使用这段代码,它会在第3行中不断出错。这是我的密码: dd if=/dev/zero bs=512 count=2880 of=testfloppy.img device=$$(hdid -nomount testfloppy.img) echo $device newfs_msdos -F 12 $device hdiutil detach $device -force device=hdid testfloppy.img|cut -d ' ' -f 1 path=m

如果我在Makefile中使用这段代码,它会在第3行中不断出错。这是我的密码:

dd if=/dev/zero bs=512 count=2880 of=testfloppy.img
device=$$(hdid -nomount testfloppy.img)
echo $device
newfs_msdos -F 12 $device
hdiutil detach $device -force
device=hdid testfloppy.img|cut -d ' ' -f 1
path=mount |grep -w '$device' | cut -d ' ' -f 3- | cut -d '(' -f 1

cp TEST.SYS $path/

hdiutil detach $device
但是,运行此代码时,我会得到以下控制台代码:

dd if=/dev/zero bs=512 count=2880 of=testfloppy.img
2880+0 records in
2880+0 records out
1474560 bytes transferred in 0.004152 secs (355139415 bytes/sec)
device=$(hdid -nomount testfloppy.img)
echo evice
evice
newfs_msdos -F 12 evice
newfs_msdos: /dev/evice: No such file or directory
make: *** [All] Error 1

我尝试过使用反向记号,也尝试过使用$$()部分将其设置为字符串,但这两个部分都没有帮助。

在shell脚本中使用变量的方式与在Makefile中使用变量的方式略有不同

例:

试试下面的makefile

dd if=/dev/zero bs=512 count=2880 of=testfloppy.img
device=hdid -nomount testfloppy.img
echo ${device}
newfs_msdos -F 12 ${device}
hdiutil detach ${device} -force
device=hdid testfloppy.img|cut -d ' ' -f 1
path=mount |grep -w '${device}' | cut -d ' ' -f 3- | cut -d '(' -f 1

cp TEST.SYS ${path}/

hdiutil detach ${device}

尝试
echo$$device
(您必须使用额外的
$
转义shell变量)。这也适用于对
$device
$path
的其他引用(即
'$device'
看起来是错误的,因为单引号将停止变量扩展)。@特洛伊木马我现在已使用$$创建了所有变量,但是在回显行上,它现在不会回显任何内容,并且使用$$device的任何注释都不会在字符串应该出现的地方收到任何内容。@特洛伊木马程序您看到这行有什么问题吗
path=$(mount | grep-w'$device'| cut-d'-f3-| cut-d'('-f1)
我已经将代码移到了a.sh进行测试,它可以转到这一行。听起来命令本身似乎有问题。正如您所说的,在将其放入
生成文件之前,首先使用shell脚本进行测试(更好的是,只需从
Makefile
调用shell脚本即可。)@特洛伊木马我现在就可以使用了,谢谢!
dd if=/dev/zero bs=512 count=2880 of=testfloppy.img
device=hdid -nomount testfloppy.img
echo ${device}
newfs_msdos -F 12 ${device}
hdiutil detach ${device} -force
device=hdid testfloppy.img|cut -d ' ' -f 1
path=mount |grep -w '${device}' | cut -d ' ' -f 3- | cut -d '(' -f 1

cp TEST.SYS ${path}/

hdiutil detach ${device}