我开始学习写脚本。今天我想创建一个脚本,它将显示我所要求的文件所在的目录。问题是我无法“进入”目录并搜索文件。
#!/bin/bash
echo "Enter a directory name..."
read directory
echo "enter a file name..."
read file
find -type d -name "$directory"
if [ -f ./$file ]
then
cat $file
else
echo "$file does not exist"
fi
答案1
我会做什么:
#!/bin/bash
read -p 'enter a dir name >>> ' dir
read -p 'enter a filename >>> ' file
res="$(find "$dir" -name "$file")"
if [[ $res ]]; then
cat "$res" # could be multiple same filename for different dirs
else
echo >&2 "file $file doesn't exists in dir $dir"
fi