Shell脚本if-else案例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell脚本if-else案例相关的知识,希望对你有一定的参考价值。

我编写了以下脚本,以交互方式获取服务器名称和服务名称。当在while循环结束时给出y作为输入时,这会重复询问输入。

key='y';
service='';
serverName='';
while [[ $key == 'y' ]]; do
    echo -e "
Enter serverName  : "
    read serverName
    echo -e "
Enter Service Name  : "
    read service
    if [[ ! -z "$service" ] && [ ! -z "$serverName" ]]; then
        echo -e "startService $serverName $service"
        #echo -e " Atleast one input is null"
    else
        echo -e " Atleast one input is null"
    fi
    echo -e "Enter y to repeat this step. Enter n to exit :"
    read key
done
答案

你的while循环条件错误,如下所示: -

while [[ '$key' == 'y' ]];

要么

while [[ $key == y ]];

以上是关于Shell脚本if-else案例的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UNIX shell 脚本中执行临时存储过程?是不是可以在 sql-plus 中使用 IF-ELSE/WHILE 循环?

shell脚本的函数介绍和使用案例

shell 脚本 片段

用于确保在任何给定时间仅运行一个 shell 脚本的 shell 片段 [重复]

20个简洁的 JS 代码片段

常用python日期日志获取内容循环的代码片段