Bash Shell代码不响应while、loop、date
为了测试它,我对while循环进行了注释,但是我的shell脚本有一些地方出了问题Bash Shell代码不响应while、loop、date,bash,shell,Bash,Shell,为了测试它,我对while循环进行了注释,但是我的shell脚本有一些地方出了问题 name = "" while [test -z name]; do echo "Please enter your name:" read name done echo "Hello $name" echo $date hour = $(date +H) if test $hour -ge 9 then echo "Welcome you are on time again!" else echo "You a
name = ""
while [test -z name]; do
echo "Please enter your name:"
read name
done
echo "Hello $name"
echo $date
hour = $(date +H)
if test $hour -ge 9
then
echo "Welcome you are on time again!"
else
echo "You are running behind your scheduled time!"
echo "Employees must be logged in by 9 am."
echo "$name arrived late on $date" >> checkin
fi
未注释while时,它不会进入循环。我想这与我的条件反射有关。它也不能正确地输入if语句。我不确定这是什么正确的格式
输出如下所示:
project2: line 1: name: command not found
project2: line 2: [test: command not found
Hello
project2: line 8: hour: command not found
project2: line 9: [test: command not found
You are running behind your scheduled time!
Employees must be logged in by 9 am.
编辑2:我不知道这是否合法,但它是这样的:
while循环已修复,谢谢各位。你知道我的if陈述是怎么回事吗
新代码:
name = ""
while test -z "$name"
do
echo "Please enter your name:"
read name
done
echo "Hello $name"
echo $date
hour = $(date +H)
if test "$hour" -ge "9"
then
echo "Welcome you are on time again!"
else
echo "You are running behind your scheduled time!"
echo "Employees must be logged in by 9 am."
echo "$name arrived late on $date" >> checkin
fi
输出
Please enter your name:
Please enter your name:
Please enter your name:
Jack
Hello Jack
project2: line 9: hour: command not found
project2: line 10: test: : integer expression expected
You are running behind your scheduled time!
Employees must be logged in by 9 am.
请输入您的姓名,根据代码和输入重复正确。你知道if语句是怎么回事吗?这就是bash shell,我可能应该提到。这是您的代码的修订版,它充分利用了bash的功能-所有更改都用注释标记:
# NO spaces allowed around `=` in ASSIGNMENTS.
# Note: By contrast, when `=` or `==` are used
# in CONDITIONALS for COMPARISON, you MUST have spaces around them.
name=""
# Use `[[ ... ]]` rather than `[ ... ]` or `test` in bash":
# It's more robust (mostly no need for quoting) and
# has more features.
# You MUST have a space after `[[` and before `]]` (same goes for `[` and `]`).
while [[ -z $name ]]
do
echo "Please enter your name:"
read name
done
echo "Hello, $name"
# There is no `$date` variable. But you can use
# COMMAND SUBSTITUTION - `$(...)` to capture the
# `date` utility's output:
dateNow=$(date)
# NO spaces around `=`; `H` must be prefixed with `%`
# to return the hour (thanks, @alvits).
hour=$(date +%H)
# Use ARITHMETIC EVALUATION - `(( ... ))`
# for numerical comparisons.
# You can refer to variables without the $ prefix,
# and use C-style arithmetic expressions.
if (( hour > 9 ))
then
echo "Welcome, you are on time again!"
else
echo "You are running behind your scheduled time!"
echo "Employees must be logged in by 9 am."
echo "$name arrived late on $dateNow" >> checkin
fi
下面是代码的修订版,它充分利用了bash的功能—所有更改都用注释标记:
# NO spaces allowed around `=` in ASSIGNMENTS.
# Note: By contrast, when `=` or `==` are used
# in CONDITIONALS for COMPARISON, you MUST have spaces around them.
name=""
# Use `[[ ... ]]` rather than `[ ... ]` or `test` in bash":
# It's more robust (mostly no need for quoting) and
# has more features.
# You MUST have a space after `[[` and before `]]` (same goes for `[` and `]`).
while [[ -z $name ]]
do
echo "Please enter your name:"
read name
done
echo "Hello, $name"
# There is no `$date` variable. But you can use
# COMMAND SUBSTITUTION - `$(...)` to capture the
# `date` utility's output:
dateNow=$(date)
# NO spaces around `=`; `H` must be prefixed with `%`
# to return the hour (thanks, @alvits).
hour=$(date +%H)
# Use ARITHMETIC EVALUATION - `(( ... ))`
# for numerical comparisons.
# You can refer to variables without the $ prefix,
# and use C-style arithmetic expressions.
if (( hour > 9 ))
then
echo "Welcome, you are on time again!"
else
echo "You are running behind your scheduled time!"
echo "Employees must be logged in by 9 am."
echo "$name arrived late on $dateNow" >> checkin
fi
变量赋值不应在=”周围包含空格 使用[…]的语句必须在开括号后有空格,在闭括号前有空格 变量的前缀必须为$。而[-z$name]应该足以测试name是否为空 您应该从hour=$date+H更改为hour=$date+%H,以实际分配小时值,而不是字母H
echo$date引用名为date的变量。但是您没有名为date的变量,因此它将打印为空。变量赋值不应在=”周围包含空格 使用[…]的语句必须在开括号后有空格,在闭括号前有空格 变量的前缀必须为$。而[-z$name]应该足以测试name是否为空 您应该从hour=$date+H更改为hour=$date+%H,以实际分配小时值,而不是字母H
echo$date引用名为date的变量。但是您没有名为date的变量,因此它将打印为空。变量赋值不应在=”周围包含空格。使用[…]的语句必须在开括号后有空格,在闭括号前有空格。变量的前缀必须为$。while[-z$name]应该足以测试名称是否为空。谢谢,修复了while循环。你知道if语句是怎么回事吗?应该是if[$hour-ge 9]。它不起作用,因为$hour的值是H。你应该改为hour=$date+%H。echo$date引用了一个名为date的变量。但是您没有名为date的变量,因此它将打印为空。变量赋值不应在=(日期)周围包含空格。使用[…]的语句必须在开括号后有空格,在闭括号前有空格。变量的前缀必须为$。while[-z$name]应该足以测试名称是否为空。谢谢,修复了while循环。你知道if语句是怎么回事吗?应该是if[$hour-ge 9]。它不起作用,因为$hour的值是H。你应该改为hour=$date+%H。echo$date引用了一个名为date的变量。但您没有名为date的变量,因此它将打印为空。@YohanesKhosiawan许先汉: 谢谢你这么说。这是一个伟大的分解所有问题与上述代码在一个奇妙的格式。谢谢你。@JFA:我的荣幸;我很高兴你觉得它很有用。@YohanesKhosiawan许先汉: 谢谢你这么说。这是一个伟大的分解所有问题与上述代码在一个奇妙的格式。谢谢你。@JFA:我的荣幸;我很高兴你觉得它很有用。