shell 判断吗 可以多层if_shell 脚本if语句嵌套实例

设计一个提示用户选择菜单,根据用户选择进行操作和显示。

菜单:1 启动vi 编辑器 2 启动x-windows  ,进入图形界面 3.启动后台打印进程,4 提示退出ctrl +c

#!/bin/bash                        //表示调用执行环境

#filename:qiantao                //命名脚本文件名

echo “please choose:(1-3,or ^C)           //显示请选择

echo "1 vi "                                                    // 显示输出1 项

echo "2 x-windows"                                      //显示输出2  x-windows

echo "3 print-file"                                           //显示输出打印

echo "ctrl+c quit "                                            //显示结束快捷键

read xyz                                                      //将用户的输入保存在xyz 里

if  [ $xyz = 1]                                               //条件判断是否为1 项

then                                                            //if 结构语句

vi                                                              // 启用vi

exit 1                                                       //退出程序

elif [ $xyz = 2 ]                                       //判断是xyz 是否为2

then                                                      //嵌套if

startx                                                     //启动x-windows

exit 1             //退出

elif [ $xyz = 3 ]                       //退出是否等于3项

then                                         //if 语句嵌套

echo ""                                    //输入打印文件名

read filename                        //读取文件名

lp $filename &                     //打印文件

exit 1                                  //退出打印

else                                  //if语句

echo "---------------------"

/bin/sh qiantao.sh                  //调用嵌套脚本

fi

执行图:

下面 可以一步一步试都成功。

你可能感兴趣的