繁体   English   中英

使用 bash 脚本显示文件内容

[英]Displaying file content with bash scripting

我正在尝试编写一个 bash 脚本(显示),它允许我访问一个目录,列出文件,然后显示所有文件的内容。 到目前为止,我能够访问目录并列出文件。

#!/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

因此,如果我使用命令 ./display [directory] 它将列出文件。 我也想显示所有文件的内容,但我被卡住了。 任何帮助将不胜感激谢谢!

使用find查找文件。 使用less以交互方式显示文件或cat以其他方式显示文件。

find "$target" -type f -exec less {} \;

我细化一个类似于“查看目录”循环的循环就足够了,但是使用cat命令而不是ls

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM