【问题标题】:Can a .sh file be a dependency in a makefile?.sh 文件可以成为 makefile 中的依赖项吗?
【发布时间】:2017-10-02 14:31:00
【问题描述】:

我在 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

【问题讨论】:

    标签: bash ubuntu makefile


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢@Thomas 特别是 $@ 小费。
    猜你喜欢
    • 2016-10-13
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    相关资源
    最近更新 更多