我想检查在执行脚本时传递的参数是否与目录中文件名的前缀匹配。我的代码面临binary operator expected
错误。有没有人有其他方法?
./test.sh abc
fucn_1(){
if [ -e $file_name* ] ; then
func_2
else
echo "file not found"
exit
fi
}
if [ $1 == abc ];
then
file_name=`echo $1`
fucn_1
elif [ $1 == xyz ];
then
file_name=`echo $1`
fucn_1
运行时,我将abc作为参数传递,这样脚本就可以检查目录中是否存在以'abc'开头的文件名。目录中有以下文件:-
abc_1234.txt
abc_2345.txt
glob$file_name*
展开为文件列表。您运行[ -e abc_1234.txt abc_2345.txt ]
,它给出了一个错误,因为[ -e
只期望一个文件,而不是两个。
试试…
#! /usr/bin/env bash
shopt -s nullglob
has_args() { (( "$#" > 0 )); }
if has_args "$1"*; then
echo "$1 is a prefix of some file or dir"
else
echo "$1 is not a prefix of any file or dir"
fi