如何在Bourne shell中使用通配符检查文件是否存在?
我要找的是:如何在Bourne shell中使用通配符检查文件是否存在?,shell,unix,scripting,Shell,Unix,Scripting,我要找的是: if [ -f "filenam*" ]; then ... fi 但通配符(*)会导致问题 我也试过: if [ `ls filenam* > /dev/null 2>&1` ]; then ... fi 它可能在其他shell中工作,但在伯恩shell(sh)中似乎不工作 谢谢你的帮助 编辑:对不起,不是C shell,Bourne shell(sh)。由于这是基于C的,您可能需要glob命令: ls `glob filenam*` 你在正
if [ -f "filenam*" ]; then
...
fi
但通配符(*)会导致问题
我也试过:
if [ `ls filenam* > /dev/null 2>&1` ]; then
...
fi
它可能在其他shell中工作,但在伯恩shell(sh)中似乎不工作
谢谢你的帮助
编辑:对不起,不是C shell,Bourne shell(sh)。由于这是基于C的,您可能需要glob命令:
ls `glob filenam*`
你在正确的轨道上。试试这个:
if [ ! -z `ls filenam*` ] ; then
...
fi
这将检查ls是否返回任何内容。因为这是csh,那么:
foreach i (`ls -d filenam*`)
...
end
与其使用
test-n$(ls filenam*)
,您可能更喜欢:
if ls filenam* 2> /dev/null | grep . > /dev/null; then
...
fi
我喜欢这样:
function first() { echo "$1" ; }
[ -e $(first filenam*) ] && echo "yes" || echo "no"
对不起,大家都感到困惑。我的意思是Bourne shell而不是C shell;然后。。。给我错误“test:argument expected”。有什么想法吗?而不是-z、 你可以用-nI,我以为你用的是普通的外壳。我不知道csh有多坏。我试着使用一个教程来解决这个问题,但我仍然无法使用任何逻辑结构来实现它。似乎没有办法执行ls并查看其输出。