我想复制一堆 Verilog/systemverilog 源,所以我使用 CP 和通配符表达式:
cp <some_dir>/*.{v,sv,svh} .
Run Code Online (Sandbox Code Playgroud)
有用。但是当我把它放到一个完全相同行的脚本中时,CP 命令失败并显示日志:
cp: cannot stat `../../mytest/spiTest/*.{v,sv,svh}': No such file or directory
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
PS:我使用 bash 作为外壳。
这是我的脚本:
#!/bin/bash
rdir=../../mytest/spiTest
f1="$rdir/bench.lst"
f2="$rdir/cphex" #the script to copy rom data
f3="$rdir/make*" #makefile scripts
f4="$rdir/*.hex" #rom files
f5="$rdir/*.{v,sv,svh}" #testbench files
echo 'Copying files...'
cp $f1 $f2 $f3 $f4 .
cp $f5 .
Run Code Online (Sandbox Code Playgroud)
我确实将第一行更改为
#!/bin/bash -vx
Run Code Online (Sandbox Code Playgroud)
并再次运行此脚本,我得到:
#!/bin/bash -vx
rdir=../../mytest/spiTest
+ rdir=../../mytest/spiTest
f1="$rdir/bench.lst"
+ f1=../../mytest/spiTest/bench.lst
f2="$rdir/cphex" #the script to copy rom data
+ f2=../../mytest/spiTest/cphex
f3="$rdir/make*" #makefile scripts
+ f3='../../mytest/spiTest/make*'
f4="$rdir/*.hex" #rom files
+ f4='../../mytest/spiTest/*.hex'
f5="$rdir/*.{v,sv,svh}" #testbench files
+ f5='../../mytest/spiTest/*.{v,sv,svh}'
echo 'Copying files...'
+ echo 'Copying files...'
Copying files...
cp $f1 $f2 $f3 $f4 .
+ cp ../../mytest/spiTest/bench.lst ../../mytest/spiTest/cphex ../../mytest/spiTest/makefile ../../mytest/spiTest/makefile.defines ../../mytest/spiTest/rom.hex ../../mytest/spiTest/rom_if.hex .
cp $f5 .
+ cp '../../mytest/spiTest/*.{v,sv,svh}' .
cp: cannot stat `../../mytest/spiTest/*.{v,sv,svh}': No such file or directory
Run Code Online (Sandbox Code Playgroud)
检查脚本的第一行。大概是这样写的:
#!/bin/sh
Run Code Online (Sandbox Code Playgroud)
它将 shell 从 BASH 切换到 Bourne Shell。用
#!/bin/bash
Run Code Online (Sandbox Code Playgroud)
反而。
[编辑]您遇到了扩展问题。BASH 有一定的顺序来扩展模式和变量。这意味着:
f5="$rdir/*.{v,sv,svh}" #testbench files
Run Code Online (Sandbox Code Playgroud)
被引用,因此此时不会发生文件名扩展。仅$rdir扩展变量。什么时候
cp $f5 .
Run Code Online (Sandbox Code Playgroud)
执行后,BASH 首先查找要扩展的文件名,但没有。然后它扩展变量 ( f5),然后cp使用两个参数调用:../../mytest/spiTest/*.{v,sv,svh}和.。由于cp期望 shell 已经执行了文件名扩展,因此您会收到错误消息。
要解决此问题,您必须使用数组:
f5=($rdir/*.{v,sv,svh})
Run Code Online (Sandbox Code Playgroud)
This replaces the variable and then expands the file names and puts everything into the array f5. You can then call cp with this array while preserving whitespaces:
cp "${f5[@]}" .
Run Code Online (Sandbox Code Playgroud)
Every single character here is important. [@] tells BASH to expand the whole array here. The quotes say: Preserve whitespace. {} is necessary to tell BASH that [@] is part of the variable "name" to expand.
| 归档时间: |
|
| 查看次数: |
1035 次 |
| 最近记录: |