Bash函数未返回预期的输出

时间:2019-10-01 15:27:36

标签: bash

我正在尝试为我的Bash脚本编写一个函数以使其保持DRY,但是由于某种原因,代码的输出与不在函数内部时的输出不同。

我想念什么?

工作:

#Get file name from file path
fileName="$(basename "$file")";
#Remove " ' and white space from name
fileName=${fileName//[\"\'\ ]/};
convert "$file" -resize $RESOLUTION\> "$OUTPUT_PATH"$fileName;

不起作用:

function cleanUpName() {
  #Get file name from file path
  fileName="$(basename "$1")";
  #Remove " ' and white space from name
  echo ${fileName//[\"\'\ ]/};
}

convert "$file" -resize $RESOLUTION\> "$OUTPUT_PATH"$( cleanUpName $file);

1 个答案:

答案 0 :(得分:1)

正如@ Robin479在评论中建议的那样,我缺少文件变量工作代码的引号如下:

function cleanUpName() {
  #Get file name from file path
  fileName="$(basename "$1")";
  #Remove " ' and white space from name
  echo "${fileName//[\"\'\ ]/}"
}
convert "$file" -resize $RESOLUTION\> "$OUTPUT_PATH$( cleanUpName "${file}")"