[英]Shell script does not find file with space in name
I created two scripts to make a checksums of a lot of files, in one of them i use the 'find' command to search the files in a specific directory, but when the file have a name with space character, 'file one.txt' for example, the script take this as two files: file and one ,I know that error is in the 'find' command line,but I don't know what I do wrong. 我创建了两个脚本来对许多文件进行校验和,在其中一个脚本中,我使用“ find”命令在特定目录中搜索文件,但是当文件的名称带有空格字符时,则使用“ file one.txt”例如,脚本将其作为两个文件: file和一个 ,我知道错误在“ find”命令行中,但是我不知道我做错了什么。
My script: 我的剧本:
#!/bin/bash
find $1 -type f -exec bash ./task2.sh "{}" \;
Thanks in advanced! 提前致谢!
In order to search for a file with spaces in the name you either have to enclose the argument in quotes or escape the spaces. 为了搜索名称中带有空格的文件,您必须将参数用引号引起来或将空格转义。 Like:
喜欢:
find "test file.txt"
or alternatively find test\\ file.txt
. find "test file.txt"
或者find test\\ file.txt
。
The easiest fix in your particular use case is to enclose $1
in quotes: 在特定用例中,最简单的解决方法是将
$1
括在引号中:
find "$1" -type f -exec bash ./task2.sh "{}" \\;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.