.sh文件可以是makefile中的依赖项吗?

时间:2017-10-02 14:31:01

标签: bash ubuntu makefile

我在Ubuntu 16.04上使用bash。我正在尝试使用makefile创建README.md文件,其中guessinggame.sh是依赖项。创建的README.md文件很好。但每次运行make时,即使依赖项未更改,也会重新创建README.md文件。之前我使用.txt文件作为依赖项,它们运行良好。有人能告诉我我在这里失踪了什么吗?

以下是我的代码:

#!/usr/bin/env bash
# File: guessinggame.sh

no_files_guessed=0
no_files_actual=0
difference=0

# Function to prompt the user for next guess
function next_guess {
    echo "Guess again:"
    read no_files_guessed
}

# 1. Count the number of files in the current directory
#    Excluding: Directories and Hidden files
no_files_actual=$(ls -p|grep -v '/$'|wc -l)

# 2. Ask the user to guess the number of files in the curret directory
echo "Guess the number of files in this directory and then press [ENTER]: "

# 3. Prompt the user for a guess
read no_files_guessed

# 4. Give user a clue to guess the correct no. if the initial guess is incorrect
while [[ $no_files_guessed -ne $no_files_actual ]]
do
    difference=$no_files_actual-$no_files_guessed
    if [[ $difference -le 10 ]] && [[ $difference -gt 0 ]]
    then
        echo "Your guess is low."
    elif [[ $difference -ge -10 ]] && [[ $difference -lt 0 ]]
    then
        echo "Your guess is high."
    elif [[ $difference -gt 10 ]]
    then
        echo "Your guess is too low."
    elif [[ $difference -lt -10 ]]
    then
        echo "Your guess is too high."
    fi
    next_guess
done

# 5. Congratulate the user for guessing the correct number
if [[ $no_files_guessed -eq $no_files_actual ]]
then
    echo "CONGRATULATIONS !!! Your guess is correct"
fi

生成文件:

all: README.txt

README.txt: guessinggame.sh
    echo "#GUESSING GAME#" > README.md
    echo >> README.md
    echo "*Time Stamp at which make was run:*" >> README.md
    date >> README.md
    echo >> README.md
    echo "Lines of code in guessinggame.sh:" >> README.md
    wc -l guessinggame.sh|egrep -o '[0-9]+' >> README.md

clean:
    rm README.md

1 个答案:

答案 0 :(得分:0)

它与.sh文件无关,它可以像任何其他文件一样用作依赖项,没有什么特别的。

错误很简单:Makefile食谱声称要创建README.txt,但它实际上会创建一个名为README.md的文件。因此,下次make运行时,它会看到README.txt仍然不存在,并再次运行配方。

为了防止将来出现此类错误,请使用$@代替README.md;它将扩展为目标文件的名称。