brig start n 命令应该在传递程序脚本中创建由 n 个传递程序进程组成的存储桶旅,但是当我运行此脚本开始该过程时,出现以下错误:
./brig: line 121: syntax error near unexpected token `else'
./brig: line 121: `else'
以下是脚本:感谢您提供的任何帮助!:
#!/bin/bash
#bucket brigade - brigade script
#the user defines a fire
#the user defines number of passers
#then passers extinguish the fire
if [[ "$2" = *[!0-9]* ]];
then
echo "The second argument may only contain digits"
exit
fi
#$2 must be empty or contain only digits
#to pass this point
case "$1" in
fire)
if [ -z "$2" ];
then
echo Please provide a fire size
echo 'exe: brig fire 10'
else
echo "$2" > .fire.size
echo "A fire of $2 intensity has started"
fi
;;
alarm)
rm .report.pas.* 2> /dev/null
if [ -z "$2" ];
then
echo Please provide the number of passers
echo 'exe: brig alarm 5'
elif [ "$2" -lt 2 ];
then
echo "There needs to be more than one passer"
else
passvar="$2"
export totalpass="$2"
#passers check exported variable to see if they
#are high number
touch .keep.passing
echo "$totalpass Passers have been activated"
while [ "$passvar" -gt 0 ];
do
bash passer "$passvar" &
((passvar--))
done
fi
;;
status)
firenow=0
clear
echo "Report of situation as of"
echo $(date)
if [ -e .fire.size ];
then
firenow=$(cat .fire.size)
fi
if [ "$firenow" -gt 0 ];
then
echo "The fire is blazing with $firenow intensity"
else
echo "No fire is active"
fi
if [ -e .keep.passing ];
then
touch .passers.pause
sleep 3
ls | grep bucket. > .current.buckets
cat bucket.* > .bucket.contents
echo "The current active buckets and contents:"
paste .current.buckets .bucket.contents | tr '\t' '\n'
rm .current.buckets
rm .bucket.contents
rm .passers.pause
else
echo "There are no buckets currently active"
fi
;;
quit)
rm .keep.passing 2> /dev/null
sleep 1
rm .fire.size 2> /dev/null
rm bucket.* 2> /dev/null
if [ -e .report.pas.1 ];
then
cat .report.pas.* >> .unified.report
fi
clear
echo 'Generating Report for this session'
sleep 3
cat .unified.report | more
else
echo "No reports exist at this time"
fi
;;
*)
echo 'Usage: brig [fire (n)|alarm (n)|status|report|quit]'
esac
答案1
fi
114 行中不应该存在 。正确的代码应该是这样的:
#!/bin/bash
# Bucket brigade - brigade script
# The user defines a fire
# The user defines number of passers
# Then passers extinguish the fire
if [[ "$2" = *[!0-9]* ]]; then
echo "The second argument may only contain digits"
exit
fi
# $2 must be empty or contain only digits to pass this point
case "$1" in
fire)
if [ -z "$2" ]; then
echo Please provide a fire size
echo 'exe: brig fire 10'
else
echo "$2" > .fire.size
echo "A fire of $2 intensity has started"
fi
;;
alarm)
rm .report.pas.* 2> /dev/null
if [ -z "$2" ]; then
echo Please provide the number of passers
echo 'exe: brig alarm 5'
elif [ "$2" -lt 2 ]; then
echo "There needs to be more than one passer"
else
passvar="$2"
export totalpass="$2"
#passers check exported variable to see if they are high number
touch .keep.passing
echo "$totalpass Passers have been activated"
while [ "$passvar" -gt 0 ]; do
bash passer "$passvar" &
((passvar--))
done
fi
;;
status)
firenow=0
clear
echo "Report of situation as of"
echo $(date)
if [ -e .fire.size ]; then
firenow=$(cat .fire.size)
fi
if [ "$firenow" -gt 0 ]; then
echo "The fire is blazing with $firenow intensity"
else
echo "No fire is active"
fi
if [ -e .keep.passing ]; then
touch .passers.pause
sleep 3
ls | grep bucket. > .current.buckets
cat bucket.* > .bucket.contents
echo "The current active buckets and contents:"
paste .current.buckets .bucket.contents | tr '\t' '\n'
rm .current.buckets
rm .bucket.contents
rm .passers.pause
else
echo "There are no buckets currently active"
fi
;;
quit)
rm .keep.passing 2> /dev/null
sleep 1
rm .fire.size 2> /dev/null
rm bucket.* 2> /dev/null
if [ -e .report.pas.1 ]; then
cat .report.pas.* >> .unified.report
# This is the wrong "fi" ######################################## ;-)
clear
echo 'Generating Report for this session'
sleep 3
cat .unified.report | more
else
echo "No reports exist at this time"
fi
;;
*)
echo 'Usage: brig [fire (n)|alarm (n)|status|report|quit]'
esac
请注意,我已经缩进你的代码,就我个人而言,我可以给你一些建议:
看在上帝的份上,求求你吧!不要放分号和换行符!;-) 如果您想制定一个好的条件,请考虑以下示例之一:
then
在同一行中:if [ "my coding style" == "can be improved" ]; then echo "I accept suggestions ;-)" fi
then
在单独的一行中:if [ "my coding style" == "can be improved" ] then echo "I accept suggestions ;-)" fi
我不知道你为什么要把简短的评论分成几行,我不会这样做。
- 请将代码缩进 2、4 或 8 个空格(我建议 4 个)。
- 当你什么都不做的时候,不要睡三秒钟。
如果我犯了任何错误,请抱歉,英语不是我的母语。
希望能帮助到你。