在一天中的时间以不同的方式迎接用户 - Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14346342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:12:10  来源:igfitidea点击:

Greet a user differently on the time of day - Bash Script

bashshellunix

提问by J Homard

I need to greet a user (using “Good morning”, “Good afternoon”, or “Good evening”) depending on the time of day.

我需要根据一天中的时间问候用户(使用“早上好”、“下午好”或“晚上好”)。

I have already gained the users details ($userTitle $userName) however I am not sure how to greet someone differently depending on the time... any ideas?

我已经获得了用户详细信息($userTitle $userName),但是我不确定如何根据时间以不同的方式问候某人......有什么想法吗?

回答by DigitalRoss

h=`date +%H`

if [ $h -lt 12 ]; then
  echo Good morning
elif [ $h -lt 18 ]; then
  echo Good afternoon
else
  echo Good evening
fi

回答by jgr

You could get the time like this:

你可以得到这样的时间:

TIME=$(date "+%H")

Then act on that value i.e

然后根据该值采取行动,即

if [ $TIME -lt 12 ]; then
    echo "Good morning"
elif [ $TIME -lt 18 ]]; then
    echo "Good afternoon"
else
    echo "Good evening"
fi

回答by Gilles Quenot

Try doing this :

尝试这样做:

TIME=$(date "+%k")

if ((TIME < 12 )); then
    echo "Good morning"
elif ((TIME < 18 )); then
    echo "Good afternoon"
else
    echo "Good evening"
fi

NOTE

笔记

  • with this syntax, no need to remember -geand such. This is just like arithmetic
  • ((...))is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for let, if side effects (assignments) are needed. See http://mywiki.wooledge.org/ArithmeticExpression
  • 使用这种语法,无需记住之-ge类的。这就像算术
  • ((...))是一个算术命令,如果表达式不为零,则返回退出状态 0,如果表达式为零,则返回 1。let如果需要副作用(赋值),也用作 的同义词。见http://mywiki.wooledge.org/ArithmeticExpression

回答by Nikolai Popov

All other answers is correct except one detail. Command date +%Hreturn number hours in format XX ( for example if time is 09:00:00, then it return "09" ). In bash numbers started with zero is octal numbers. So this nuance can cause errors.

除了一个细节外,所有其他答案都是正确的。命令date +%H以 XX 格式返回小时数(例如,如果时间是 09:00:00,则返回 "09" )。在 bash 中,以零开头的数字是八进制数字。因此,这种细微差别可能会导致错误。

For example:

例如:

if [ 09 > 10 ] 
then
    echo "it's something strange here"
fi

will print "it's something strange here".

将打印“这里有些奇怪”。

Probably you chose time intervals, who are not cause such behavior. But for insurance you can write:

可能你选择了时间间隔,谁不会引起这种行为。但是对于保险,你可以写:

hours=date +"%H" | sed -e 's/^0//g'

小时=date +"%H" | sed -e 's/^0//g'

Be carefull.

小心点。

回答by Adam Sznajder

hour=`date +%H`
if [ $hour -le 12 ]; then
    echo 'good morning'
elif [ $hour -ge 18 ]; then
    echo 'good evening'
else
    echo 'good afternoon'
fi

回答by Mgoular

Whatif in powershell with a function ?

如果在具有功能的 powershell 中呢?

function Get-Greeting
    {
    $Hour = (Get-Date).TimeOfDay.Hours
    if($Hour –ge 0 –and $Hour –lt 12)

    {
        $greet = “Good Morning give me a coffee !!”
    }
    elseif($Hour –ge 12 –and $Hour –lt 16)
    {
        $greet = “Good Afternoon How is the weather today?”
    }
    else
    {
        $greet = “Good Evening sir, want to sip a tea?”
    }

    $Username = $env:USERNAME

    return $(“$greet , You have logged in as User, $Username” )
    }
        enter code here

回答by aishu

echo enter the time 
read time 
if [ $ time - lt 12 ]
then
echo good morning
elif [ $ time - lt 16 ]
then
echo good afternoon
elif [ $ time - lt 20 ]
then
echo good evening
elif [ $ time - lt 25]
then
echo good night
else
echo enter a valid number only 24 hour!!!!!!!
fi

回答by Akshay Shrivastava

h=`date|cut -d" " -f4|cut -d: -f1`
if [ $h -lt 10 ]; then
 echo Good Morning
elif [ $h -gt 10 -o $h -lt 16 ]; then
 echo Good Afternoon
elif [ $h -gt 16 -o $h -lt 20 ]; then
 echo Good Evening
else
 echo Good Night
fi