[英]Binary operator expected error when running a bash script against a directory other than the one the script is currently located in
I am very new to bash scripting first and foremost.首先,我对 bash 脚本非常陌生。 I wrote a script that checks for entries inside of a directory and prints information about the entries.
我编写了一个脚本来检查目录中的条目并打印有关条目的信息。 If the entries are files it prints the file size and if there are directories it prints how many items are in those directories.
如果条目是文件,它会打印文件大小,如果有目录,它会打印这些目录中有多少项目。 When I run the script against the directory it is located in the script completes successfully.
当我针对它位于脚本中的目录运行脚本时,它成功完成。 If I run the script against another directory I receive the error "Binary operator expected".
如果我对另一个目录运行脚本,我会收到错误“Binary operator expected”。 Is this to be expected?
这是可以预料的吗? Here is the code:
这是代码:
#!/bin/bash
# Print the contents of a directory with details on files and directories
files=( $1* )
count=0
for entry in $1/*
do
if [ -d $entry ]
then
for f in $entry/*
do
let count=count+1
done
echo $entry ":" "This is a directory with $count files present."
let count=0
else [ -f $entry ]
echo $entry "This is a file of" $(wc -c <"$entry") "bytes."
fi
done
I'm sure this code isn't perfect and most likely has an easier method so I am just looking for some advice.我确信这段代码并不完美,而且很可能有一个更简单的方法,所以我只是在寻找一些建议。 Thank you for any help.
感谢您的任何帮助。
FurmanTheGerman弗曼德国人
Fixed script:固定脚本:
#!/usr/bin/env sh
for entry in "$1/"*; do
if [ -d "$entry" ]; then
count="$(printf '%s\0' "$entry/"* | grep -cF '')"
printf '%s : This is a directory with %d files present.\n' "$entry" "$count"
else
if [ -f "$entry" ]; then
size="$(wc -c <"$entry")"
printf '%s This is a file of %d bytes.\n' "$entry" "$size"
fi
fi
done
$1
argument:$1
参数周围缺少引号的未使用赋值:files=( $1* )
$entry
variable:$entry
变量周围缺少引号的问题:if [ -d $entry ]
Replaced loop with single assignment, broken indentation and missing quotes around the non-globbing part of the path $entry/
:将循环替换为单个赋值、损坏的缩进和路径
$entry/
的非通配部分周围缺少引号:
for f in $entry/* do let count=count+1 done
echo
mixing literal and variables with missing quotes by a proper printf
: echo $entry ":" "This is a directory with $count files present."
printf
替换echo
混合文字和变量缺少引号: echo $entry ":" "This is a directory with $count files present."
echo $entry "This is a file of" $(wc -c <"$entry") "bytes."
$entry
variable and added missing if
block: else [ -f $entry ]
$entry
变量周围添加了缺少的引号并添加了缺少的if
块: else [ -f $entry ]
Explanations about counting the entries in the directory without running a shell loop:关于在不运行 shell 循环的情况下计算目录中的条目的说明:
printf '%1s\0' "$entry/"* | grep -cF ''
printf '%1s\0' "$entry/"* | grep -cF ''
: printf '%1s\0' "$entry/"* | grep -cF ''
:
printf '%1s\0' "$entry/"*
: Prints a null delimited list of all files. printf '%1s\0' "$entry/"*
:打印所有文件的 null 分隔列表。| grep -cF ''
| grep -cF ''
: Pipe it into grep
to count the null
characters (1 per entry) | grep -cF ''
: Pipe 它进入grep
以计算null
字符)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.