不知道为什么我得到这个。我意识到这一定是一个常见问题,但无法弄清楚。
#!/bin/bash
#Checks word count of each text file directory and deletes if less than certain amount of words
#Lastly, displays number of files delter
count = 0 #Set counter to 0
limit = 2000
for file in *.txt
do
words = wc -w > $file
if words < $limit
rm $file
count = $count + 1
end
end
print "Number of files deleted: $count"
Run Code Online (Sandbox Code Playgroud)
恐怕你的脚本充满了语法错误。您看到的具体错误是因为您没有for正确关闭循环,但还有很多很多:
=给变量赋值时不能有空格(算术表达式除外);var=`command`或者var=$(command);$var, not var,一般情况下,需要用引号 ( "$var");-lt的的[命令,不<,除非你使用双括号;command > file格式将被file命令的输出覆盖。您可能打算使用wc < "$file"而不是wc > $file;var=$var+1除非该变量之前已声明为整数,否则您无法向变量添加值,您需要((var=var+1)),var=$((var+1))或declare -i var; var=var+1。要加 1,您还可以使用((var++));if语法是错误的。正确的格式是if condition; then do something; fifor循环,正确的语法是for loop-specification; do something; done;print命令(无论如何都没有内置在 bash 中),只有printfand echo;因此,稍微改进的脚本的工作版本将是:
#!/bin/bash -
# Checks word count of each text file directory and deletes if less than certain amount of words
# Lastly, displays number of files deleted
count=0 # Set counter to 0
limit=2000
for file in *.txt
do
words=$(wc -w < "$file")
if [ "$words" -lt "$limit" ]
then
rm -- "$file"
((count++))
fi
done
echo "Number of files deleted: $count"
Run Code Online (Sandbox Code Playgroud)
下次,我建议您在尝试使用一种语言进行编码之前先熟悉它。每种语言都有自己的规则和语法。
| 归档时间: |
|
| 查看次数: |
1504 次 |
| 最近记录: |