0

我正在遍历目录中的每个文件,并尝试使用以下代码查找/替换文件的路径部分...

for f in /the/path/to/the/files/*
do
    file = $(echo $f | sec 's/\/the\/path\/to\/the\/files\///g`);
done  

但是,我的代码的分配部分出现以下错误...

cannot open `=' (No such file or directory)

我究竟做错了什么?

4

3 回答 3

3

你必须=在它周围没有空格:

for f in /the/path/to/the/files/*
do
 file=$(echo $f | sec 's/\/the\/path\/to\/the\/files\///g');
done  

此外,最好使用另一个符号,而不是 /,作为 sed 分隔符:

for f in /the/path/to/the/files/*
do
 file=$(echo $f | sec 's@/the/path/to/the/files/@@g')
done  
于 2013-03-21T17:57:43.583 回答
1

您不能在等号的任一侧放置空格:

for f in /the/path/to/the/files/*
do
    file=$(echo $f | sed 's/\/the\/path\/to\/the\/files\///g`);
done  

但是,参数扩展是实现此目的的更好方法:

for f in /the/path/to/the/files/*
do
    file=${f#/the/path/to/the/files/}
done  
于 2013-03-21T17:58:20.553 回答
0

尝试:

for f in /the/path/to/the/files/*; do
    # no spaces around = sign
    file=$(echo $f | sed "s'/the/path/to/the/files/''g");
done
于 2013-03-21T19:07:59.293 回答