[英].sh script will not let me loop through files
I am trying to print out my files on /var/www/ but for some reason I get this error: "temp.sh: line 3: FILES: command not found" 我试图在/ var / www /上打印出我的文件,但是由于某种原因,我收到此错误:“ temp.sh:第3行:FILES:未找到命令”
Code: 码:
#loop through files FILES = /var/www/* for f in $FILES do echo "processing $f" done
I am running this from the home directory by command line like this sh script.sh (if that matters) 我正在通过主目录通过命令行运行,例如sh script.sh(如果这样的话)
To set a variable in sh, there cannot be any spaces before or after the =
character. 要在sh中设置变量,在
=
字符之前或之后不能有空格。 Change your code to 将您的代码更改为
FILES=/var/www/*
However, this will have problems if any of the files in /var/www
have spaces in their names, as they'll be split after variable substitution in your for
line. 但是,如果
/var/www
任何文件的名称中都有空格,则会出现问题,因为它们将在您for
行中替换变量后被拆分。 So if there's a file like name with space
, you'll get 因此,如果有一个像
name with space
这样的文件, name with space
,您将得到
processing /var/www/name
processing with
processing space
You can avoid that by putting the pattern directly in the for
command, as pathname expansion occurs after word splitting: 您可以通过将模式直接放在
for
命令中来避免这种情况,因为路径名扩展是在单词拆分之后发生的:
for f in /var/www/*; do
echo "processing $f"
done
and you'll get processing /var/www/name with space
并且您将
processing /var/www/name with space
Alternately, you can use a variable for the directory -- you just need to insure that the wildcards are in the for
line to get the expansion/splitting in the correct order: 或者,您可以在目录中使用变量-您只需要确保通配符在
for
行中即可以正确的顺序进行扩展/拆分:
FILE_DIR=/var/www
for f in $FILE_DIR/*; do
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.