linux shell脚本实现与用户交互式命令

By SuperTaoer | 十月 10, 2008
Under: 程序 技术 周边
Views: 712 views

比如说用户执行一个shell脚本,用户去执行他,给用户列出四个ip地址,让用户从中选择一个,然后根据选择的登录到不同的ip地址。

其中shell脚本内容如下:

  1. #!/bin/sh
  2.  
  3. ipAddrList=('' '172.24.149.103' '172.24.149.104' '172.24.202.12' '172.24.202.13');
  4.  
  5. remoteIpAddr=${ipAddrList[$1]};
  6.  
  7. if [ "$remoteIpAddr" == "" ]
  8. then
  9.  
  10.     echo "Please input server number";
  11.  
  12.     i=0;   
  13.  
  14.     for ip in ${ipAddrList[*]}
  15.     do
  16.         i=$[i+1];
  17.  
  18.         if [ "$ip" != "" ]
  19.         then   
  20.             echo "[${i}]: ${ip}";
  21.         fi     
  22.     done   
  23.  
  24.     i=$[i+1];
  25.     echo "[${i}]: exit shell";
  26.     echo -n "Please input:";
  27.     read ANSWER;
  28.  
  29.     if [ "$ANSWER" == "$i" ]
  30.     then   
  31.         exit;   
  32.     fi
  33.  
  34.     $0 $ANSWER;
  35.     exit;   
  36. fi
  37.  
  38. echo "Connecting to ${remoteIpAddr} ...";
  39. ssh web@$remoteIpAddr;

其中主要的程序在

  1. read ANSWER;
  2.  
  3.     if [ "$ANSWER" == "$i" ]
  4.     then   
  5.         exit;   
  6.     fi
  7.  
  8.     $0 $ANSWER;

这一段儿,read ANSWER是指从交互模式读入用户输入,并将用户的输入再保存到$ANSWER变量中,下面的就是流程的判断了。。。

差..好文!! (还没评价)
正在加载....请稍候.... ... 正在加载....请稍候.... ...

Leave a Message

No Messages

No Messages

RSS feed for comments on this post. TrackBack URI

Leave a Message