gpt4 book ai didi

bash - .sh 文件可以成为 makefile 中的依赖项吗?

转载 作者:太空宇宙 更新时间:2023-11-03 17:08:12 25 4
gpt4 key购买 nike

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

最佳答案

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

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

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

关于bash - .sh 文件可以成为 makefile 中的依赖项吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46527605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com