如何在 echo 和 bash 中不转义 space 和反斜杠?
how to not escape space and backslashes in echo and while in bash?
我将两个位置参数传递给 运行 的脚本,两个参数都是路径,在分析路径的场景中,问题是有时会有一些路径,例如:m i sc . . . . .. . .
它有点和空格,有时甚至我们在目录名称中有一个反斜杠。
因此尝试通过两个过程获取参数,直接和通过 at sign
。
SOURCE_ARG=
DESTINATION_ARG=
和
ARG_COUNT=0
for POSITIONAL_ARGUMENTS in "${@}"
do
((ARG_COUNT++))
ARGUMENT_ARRAY[$ARG_COUNT]=$POSITIONAL_ARGUMENTS
done
在循环中,我遍历转发给他们的命令的结果。
while IFS= read -r dir
do
echo "${ARGUMENT_ARRAY[1]}"
echo "${dir}"
while IFS= read -r item
do
# do some stuff
done < <(ls -A "$dir"/)
done < <(du -hP "$SOURCE_ARG" | awk '{=""; print [=14=]}' | grep -v "^.$" | sed "s/^ //g")
当我使用 echo "${ARGUMENT_ARRAY[1]}"
时,我得到的路径与我需要检查的路径相同,但是当在这里使用循环迭代变量作为目录时 ->echo "${dir}"
我得到了所有空格,因为其他命令因为那条路无法完成他们的工作。
我要问的是如何在循环中获得 $dir
的输出,就像我上面提到的 echo "${ARGUMENT_ARRAY[1]}"
一样(输入所有空格和反斜杠)
感谢@Barmar 的评论。
The only reason that filenames are without escapes (i.e. you see directories with no special character or special characters have been escaped) is because du
is printing the filenames with escapes, so $dir
variable would have escaped once and special characters are no longer available for the other loop iteration in my problem.
现在我们知道问题是通过在我的脚本中使用 du
引起的:
while IFS= read -r dir
# do sth
done < <(du -hP "$SOURCE_ARG" | awk '{=""; print [=10=]}' | grep -v "^.$" | sed "s/^ //g")
我们可以把du
改成find
,问题就解决了:
while IFS= read -r dir
# do sth
done < <(find "$SOURCE_ARG" -type d –)
PS 1:
另一个问题是 echo
.
,因为我想打印这些行以检查它们是否正常(即在调试应用程序时)。
因此请务必尝试 printf "%s\n" "$dir"
而不是 echo
,因为某些版本的 echo
会处理转义序列。
echo "${dir}"
printf "%s\n" "$dir"
PS 2:
此外,如果文件名有 more than one space in a row
,我使用 awk
的方式是将它们折叠成一个 space。
awk '{=""; print [=13=]}' | grep -v "^.$" | sed "s/^ //g"
我将两个位置参数传递给 运行 的脚本,两个参数都是路径,在分析路径的场景中,问题是有时会有一些路径,例如:m i sc . . . . .. . .
它有点和空格,有时甚至我们在目录名称中有一个反斜杠。
因此尝试通过两个过程获取参数,直接和通过 at sign
。
SOURCE_ARG=
DESTINATION_ARG=
和
ARG_COUNT=0
for POSITIONAL_ARGUMENTS in "${@}"
do
((ARG_COUNT++))
ARGUMENT_ARRAY[$ARG_COUNT]=$POSITIONAL_ARGUMENTS
done
在循环中,我遍历转发给他们的命令的结果。
while IFS= read -r dir
do
echo "${ARGUMENT_ARRAY[1]}"
echo "${dir}"
while IFS= read -r item
do
# do some stuff
done < <(ls -A "$dir"/)
done < <(du -hP "$SOURCE_ARG" | awk '{=""; print [=14=]}' | grep -v "^.$" | sed "s/^ //g")
当我使用 echo "${ARGUMENT_ARRAY[1]}"
时,我得到的路径与我需要检查的路径相同,但是当在这里使用循环迭代变量作为目录时 ->echo "${dir}"
我得到了所有空格,因为其他命令因为那条路无法完成他们的工作。
我要问的是如何在循环中获得 $dir
的输出,就像我上面提到的 echo "${ARGUMENT_ARRAY[1]}"
一样(输入所有空格和反斜杠)
感谢@Barmar 的评论。
The only reason that filenames are without escapes (i.e. you see directories with no special character or special characters have been escaped) is because
du
is printing the filenames with escapes, so$dir
variable would have escaped once and special characters are no longer available for the other loop iteration in my problem.
现在我们知道问题是通过在我的脚本中使用 du
引起的:
while IFS= read -r dir
# do sth
done < <(du -hP "$SOURCE_ARG" | awk '{=""; print [=10=]}' | grep -v "^.$" | sed "s/^ //g")
我们可以把du
改成find
,问题就解决了:
while IFS= read -r dir
# do sth
done < <(find "$SOURCE_ARG" -type d –)
PS 1:
另一个问题是 echo
.
因此请务必尝试 printf "%s\n" "$dir"
而不是 echo
,因为某些版本的 echo
会处理转义序列。
echo "${dir}"
printf "%s\n" "$dir"
PS 2:
此外,如果文件名有 more than one space in a row
,我使用 awk
的方式是将它们折叠成一个 space。
awk '{=""; print [=13=]}' | grep -v "^.$" | sed "s/^ //g"