简体   繁体   English

检查目录外壳中是否存在D.userid

[英]Check if D.userid exists in a directory shell

We assume the current directory has a number of directories named D.userid, each of which contains submitted Java files. 我们假设当前目录具有多个名为D.userid的目录,每个目录都包含已提交的Java文件。 How to detect if there is D.userid present in a directory? 如何检测目录中是否存在D.userid? What should be the code. 代码应该是什么。 I dont this mine is rite 我不是我的仪式

#!/bin/bash

if [ -d "$D.*" ]
then


else
    echo "no .java file(s) submitted"
    exit    
fi
done

Since you assume there are files, and only want to be informed that there are none, I think this is reasonable: 因为您假设有文件,并且只想知道没有文件,所以我认为这是合理的:

ls D.* > /dev/null       # try to list the files. we don't need output
return=$?                # save the return value of ls just in case
if [ $return -ne 0 ];    # compare it to 0 (success)
then
    echo "No files."
fi

ls returns 2 if the files are not found, so if you want you can use that directly. 如果找不到文件, ls将返回2,因此如果您愿意,可以直接使用它。 Of course, it doesn't check that they are directories, but it's just another way. 当然,它不会检查它们是否是目录,但这只是另一种方式。

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

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