我刚刚开始使用linux和bash。我想知道如何创建一个bash函数,该函数可以添加所有以.conf结尾的文件,任何用户都可以将其读入文件中。对于普通用户无法读取的文件,可以添加到另一个文件中。我像这样运行它,但是我得到这个错误代码4:语法错误:"("意外(期待"}")"
add_files () {
readable_file_list=()
unreadable_file_list=()
# Find all files with .conf extension
for file in /*.conf; do
# Check if file is readable by any user
if [[ -r "$file" ]]; then
# Add file to readable list
readable_file_list+=("$file")
else
# Add file to unreadable list
unreadable_file_list+=("$file")
fi
done
# Add readable files to readable_files.txt
printf '%sn' "${readable_file_list[@]}" > readable_files.txt
# Add unreadable files to unreadable_files.txt
printf '%sn' "${unreadable_file_list[@]}" > unreadable_files.txt
}
"
知道为什么吗?
函数如下:
function add_files {
readable_file_list=()
unreadable_file_list=()
# Find all files with .conf extension
for file in /path/to/directory/*.conf; do
# Check if file is readable by any user
if [[ -r "$file" ]]; then
# Add file to readable list
readable_file_list+=("$file")
else
# Add file to unreadable list
unreadable_file_list+=("$file")
fi
done
# Add readable files to readable_files.txt
printf '%sn' "${readable_file_list[@]}" > readable_files.txt
# Add unreadable files to unreadable_files.txt
printf '%sn' "${unreadable_file_list[@]}" > unreadable_files.txt
}