Shell 用于在用户定义目录中搜索文件的Unix脚本
我需要制作一个unix脚本来检查用户定义目录中的文件,即脚本将采用文件名,例如abc.txt,还将从用户输入目录,例如/home/user/abc&脚本将检查特定文件abc.txt是否在该目录中可用 我试过使用:-Shell 用于在用户定义目录中搜索文件的Unix脚本,shell,unix,Shell,Unix,我需要制作一个unix脚本来检查用户定义目录中的文件,即脚本将采用文件名,例如abc.txt,还将从用户输入目录,例如/home/user/abc&脚本将检查特定文件abc.txt是否在该目录中可用 我试过使用:- echo "Enter your directory" read directory echo "Enter file name" read name if [ -s $directory/$name ] then echo 0 else echo "File not avail
echo "Enter your directory"
read directory
echo "Enter file name"
read name
if [ -s $directory/$name ]
then
echo 0
else
echo "File not available"
我想看看unix stack exchange站点,这对您更有帮助: 这可能会有帮助:
system("[ ! -e file ]; echo $?")
尝试以下方法:
find $directory -name "$name" | egrep '.*'
if [[ $? -eq 0 ]]
then
echo 0
else
echo "File not available"
fi
或者你可以用
if [ -e $directory/$name ]
要检查文件是否存在。您的代码几乎正确,下面是一个固定/增强版本:
printf "Enter your directory : "
read directory
printf "Enter file name : "
read name
if [ -f "$directory/$name" ] ; then
echo "File found"
else
echo "File not found"
fi
那不管用吗?你的问题是什么?使用ls$directory | grep$name@AshwaniDausodia不,不要。更好的方法是使用[[-r${directory}/${name}]];然后…请注意,find方法还将查找${directory}/some/sub/directory/${name},这可能不是有意的…是的,在这种情况下,OP可能需要使用maxDepth作为1。在运行此脚本时,我没有收到这样的文件或目录错误。