发现-iname在脚本中不起作用

时间:2016-12-19 14:33:34

标签: bash find

按照find命令。

find Work/Linux4/test/test/test_goal/spyglass_reports/clock-reset/Ac_coherency06/ -iname "Ac_coherency*.csv"
在shell上运行时,

工作正常。 但是在perl脚本中它什么也没有返回。

#!/bin/bash

REPORT_DIR=$1
FIND_CMD=$2

echo "@@";
echo $REPORT_DIR ;
echo $FIND_CMD ;

LIST_OF_CSV=$(find $REPORT_DIR $FIND_CMD)
echo $LIST_OF_CSV

if [ "X" == "X${LIST_OF_CSV}" ]; then
    echo "No files Found for : '$FIND_CMD' in directory "; 
    echo " '$REPORT_DIR' " | sed -e 's;Work/.*/test_reports;Work/PLATFORM/test_reports;g';
    echo;
    exit 0;
fi

脚本输出:

@@
Work/$PLATFORM_SPECIES/test_reports/clock-reset/Ac_coherency06 -iname "Ac_coherency06*.csv"

No files Found for : '-iname "Ac_coherency06*.csv"' in directory     'Work/PLATFORM/test_reports/clock-reset/Ac_coherency06' 

2 个答案:

答案 0 :(得分:1)

如果你有一个在shell上运行良好而不在你的脚本上运行的命令,我首先尝试的是在被调用的命令上指定Bash,看看是否有效:

bash -c 'find Work/Linux4/test/test/test_goal/spyglass_reports/clock-reset/Ac_coherency06/ -iname "Ac_coherency*.csv"'

甚至更好:

/bin/bash -c 'find Work/Linux4/test/test/test_goal/spyglass_reports/clock-reset/Ac_coherency06/ -iname "Ac_coherency*.csv"'

您还可以根据需要将结果存储在变量或其他数据结构中,稍后将其传递给脚本,例如:

ResultCommand="$(bash -c 'find Work/Linux4/test/test/test_goal/spyglass_reports/clock-reset/Ac_coherency06/ -iname "Ac_coherency*.csv"')"

修改:此修补程序不止一次被编辑以修复可能出现的问题。

答案 1 :(得分:1)

如果您允许传递find个谓词的列表,将它们保持在列表格式,则每个脚本参数的find一个参数。作为以这种方式实现的示例:

#!/bin/bash

# read report_dir off the command line, and shift it from arguments
report_dir=$1; shift

# generate a version of report_dir for human consumption
re='Work/.*/test_reports'
replacement='Work/PLATFORM/test_reports'
if [[ $report_dir =~ $re ]]; then
  report_dir_name=${report_dir//${BASH_REMATCH[0]}/$replacement}
else
  report_dir_name=$report_dir
fi

# read results from find -- stored NUL-delimited -- into an array
# using NUL-delimited inputs ensure that even unusual filenames work correctly
declare -a list_of_csv
while IFS= read -r -d '' filename; do
 list_of_csv+=( "$filename" )
done < <(find "$report_dir" '(' "$@" ')' -print0)

# Use the length of that array to determine whether we found contents
echo "Found ${#list_of_csv[@]} files" >&2

if (( ${#list_of_csv[@]} == 0 )); then
  echo "No files found in $report_dir_name" >&2
fi

此处,shift会使用列表中的第一个参数,"$@"表示该点之后剩余的所有其他参数。 这意味着您希望作为单独的find个别参数传递的项目可以(并且必须)作为单独的单个参数传递给您的脚本。

因此,使用yourscript "/path/to/report/dir" -name '*.txt'时,$1最初为/path/to/report/dir$2-name$3为{ {1}}。但是,运行*.txt后,shift将为$1-name将为$2; *.txt将引用这两个,每个都作为单独的单词传递。