食谱似乎忽略了循环内的条件

时间:2014-09-28 03:05:34

标签: shell loops if-statement gnu-make

我的make文件中有这个食谱:

.PHONY: matlab                                         
matlab: $(matlab_files)                                
  set -x;for f in `echo "$^"`; do\                     
    base=`basename $$f`;\                              
    if [[ ! -f "$(matlab_dir_name)/$${base}" ]]; then\ 
      ln -s $$f $(matlab_dir_name)/$${base}; \         
    fi;\                                               
  done;set +x                                          

也就是说,我想在制作之前检查链接是否存在。但if测试似乎被忽略了。使用上面显示的调试选项,我得到以下几行:

+ for f in '`echo "MatlabD3PDImport/matlab/d3pd_export.m MatlabD3PDImport/matlab/matd3pd.m MatlabD3PDImport/matlab/net2py.m MatlabRingerTrain/matlab/cluster_size.m MatlabRingerTrain/matlab/create_cluster.m MatlabRingerTrain/matlab/getEffAtThres.m MatlabRingerTrain/matlab/getThresAndEff.m MatlabRingerTrain/matlab/scanparam.m MatlabRingerTrain/matlab/script_normal_train_offline.m MatlabRingerTrain/matlab/trainBinaryClassifier.m MatlabRingerTrain/matlab/trainRinger.m MatlabRingerTrain/matlab/xValidateBinaryClassifier.m"`'
++ basename MatlabRingerTrain/matlab/scanparam.m
+ base=scanparam.m
+ [[ ! -f matlab/scanparam.m ]]
+ ln -s MatlabRingerTrain/matlab/scanparam.m matlab/scanparam.m
ln: matlab/scanparam.m: File exists
+ for f in '`echo "MatlabD3PDImport/matlab/d3pd_export.m MatlabD3PDImport/matlab/matd3pd.m MatlabD3PDImport/matlab/net2py.m MatlabRingerTrain/matlab/cluster_size.m MatlabRingerTrain/matlab/create_cluster.m MatlabRingerTrain/matlab/getEffAtThres.m MatlabRingerTrain/matlab/getThresAndEff.m MatlabRingerTrain/matlab/scanparam.m MatlabRingerTrain/matlab/script_normal_train_offline.m MatlabRingerTrain/matlab/trainBinaryClassifier.m MatlabRingerTrain/matlab/trainRinger.m MatlabRingerTrain/matlab/xValidateBinaryClassifier.m"`'
++ basename MatlabRingerTrain/matlab/script_normal_train_offline.m
+ base=script_normal_train_offline.m
+ [[ ! -f matlab/script_normal_train_offline.m ]]
+ ln -s MatlabRingerTrain/matlab/script_normal_train_offline.m matlab/script_normal_train_offline.m
ln: matlab/script_normal_train_offline.m: File exists
+ for f in '`echo "MatlabD3PDImport/matlab/d3pd_export.m MatlabD3PDImport/matlab/matd3pd.m MatlabD3PDImport/matlab/net2py.m MatlabRingerTrain/matlab/cluster_size.m MatlabRingerTrain/matlab/create_cluster.m MatlabRingerTrain/matlab/getEffAtThres.m MatlabRingerTrain/matlab/getThresAndEff.m MatlabRingerTrain/matlab/scanparam.m MatlabRingerTrain/matlab/script_normal_train_offline.m MatlabRingerTrain/matlab/trainBinaryClassifier.m MatlabRingerTrain/matlab/trainRinger.m MatlabRingerTrain/matlab/xValidateBinaryClassifier.m"`'
++ basename MatlabRingerTrain/matlab/trainBinaryClassifier.m
+ base=trainBinaryClassifier.m
+ [[ ! -f matlab/trainBinaryClassifier.m ]]
+ ln -s MatlabRingerTrain/matlab/trainBinaryClassifier.m matlab/trainBinaryClassifier.m
ln: matlab/trainBinaryClassifier.m: File exists
+ for f in '`echo "MatlabD3PDImport/matlab/d3pd_export.m MatlabD3PDImport/matlab/matd3pd.m MatlabD3PDImport/matlab/net2py.m MatlabRingerTrain/matlab/cluster_size.m MatlabRingerTrain/matlab/create_cluster.m MatlabRingerTrain/matlab/getEffAtThres.m MatlabRingerTrain/matlab/getThresAndEff.m MatlabRingerTrain/matlab/scanparam.m MatlabRingerTrain/matlab/script_normal_train_offline.m MatlabRingerTrain/matlab/trainBinaryClassifier.m MatlabRingerTrain/matlab/trainRinger.m MatlabRingerTrain/matlab/xValidateBinaryClassifier.m"`'
++ basename MatlabRingerTrain/matlab/trainRinger.m
+ base=trainRinger.m
+ [[ ! -f matlab/trainRinger.m ]]
+ ln -s MatlabRingerTrain/matlab/trainRinger.m matlab/trainRinger.m
ln: matlab/trainRinger.m: File exists
+ for f in '`echo "MatlabD3PDImport/matlab/d3pd_export.m MatlabD3PDImport/matlab/matd3pd.m MatlabD3PDImport/matlab/net2py.m MatlabRingerTrain/matlab/cluster_size.m MatlabRingerTrain/matlab/create_cluster.m MatlabRingerTrain/matlab/getEffAtThres.m MatlabRingerTrain/matlab/getThresAndEff.m MatlabRingerTrain/matlab/scanparam.m MatlabRingerTrain/matlab/script_normal_train_offline.m MatlabRingerTrain/matlab/trainBinaryClassifier.m MatlabRingerTrain/matlab/trainRinger.m MatlabRingerTrain/matlab/xValidateBinaryClassifier.m"`'
++ basename MatlabRingerTrain/matlab/xValidateBinaryClassifier.m
+ base=xValidateBinaryClassifier.m
+ [[ ! -f matlab/xValidateBinaryClassifier.m ]]
+ ln -s MatlabRingerTrain/matlab/xValidateBinaryClassifier.m matlab/xValidateBinaryClassifier.m
ln: matlab/xValidateBinaryClassifier.m: File exists
+ set +x

为什么shell忽略if测试?

1 个答案:

答案 0 :(得分:1)

-f FILE检查文件是否存在且是否为常规文件。要在测试中包含符号链接,请使用-h / -L(仅限符号链接)或-e(所有文件)。

if [[ ! -e "$(matlab_dir_name)/$${base}" ]]; then