【发布时间】: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
【问题讨论】: