[英]Displaying file content with bash scripting
I am trying to write a bash script (display) that will allow me to access a directory, list the files, and then display the content of all of the files.我正在尝试编写一个 bash 脚本(显示),它允许我访问一个目录,列出文件,然后显示所有文件的内容。 So far I am able to access the directory and list the files.到目前为止,我能够访问目录并列出文件。
#!/bin/bash
#Check for folder name
if [ "$#" -ne 1 ]; then
echo " Usage: count [folder name]"
exit 1
fi
#Check if it is a directory
if [ ! -d "$1" ]; then
echo "Not a valid directory"
exit 2
fi
#Look at the directory
target=$1
echo "In Folder: $target"
for entry in `ls $target`; do
echo $entry
done
So if I use the command./display [directory] it will list the files.因此,如果我使用命令 ./display [directory] 它将列出文件。 I want to display the contents of all of the files as well but I am stuck.我也想显示所有文件的内容,但我被卡住了。 Any help would be appreciated thanks!任何帮助将不胜感激谢谢!
Use find
to find files.使用find
查找文件。 Use less
to display files interactively or cat
otherwise.使用less
以交互方式显示文件或cat
以其他方式显示文件。
find "$target" -type f -exec less {} \;
I thin a loop similar to your "look at the directory" loop would suffice, but using the cat
command instead of ls
我细化一个类似于“查看目录”循环的循环就足够了,但是使用cat
命令而不是ls
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.