我正在遍历目录中的每个文件,并尝试使用以下代码查找/替换文件的路径部分...
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)
我做错了什么?
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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