Chinaunix首页 | 论坛 | 博客
  • 博客访问: 74738
  • 博文数量: 47
  • 博客积分: 1230
  • 博客等级: 中尉
  • 技术积分: 525
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-22 13:59
文章分类

全部博文(47)

文章存档

2012年(9)

2011年(38)

分类: Python/Ruby

2011-04-25 22:00:00

问题1:putty中普通用户(除了root之外)在vi编辑器中字体的颜色怎么调的

http://blog.csdn.net/zhongjiekangping/archive/2010/09/03/5861971.aspx

 

shell句式

条件判断式

 

if  [  条件判定一 ]; then

        程序段

   elif  [ 条件判定二 ]; then

        程序段

   elif  [ 条件判定二 ]; then

        程序段

   else

        程序段

   fi

注意:中括号里有空格,if和[之间也有空格,‘;’之后也有空格

示例一如下:主要用了if then和for loop、数组。

ifthen-test.sh,至少输入两个参数,根据参数的不同输出不同的结果;

如果少于2个参数,输出“short of parameter”;

如果大于等于2个参数:

前两个参数为moon和tree,输出:03grade:+所有输入参数 ;

前两个参数为golden和flower,输出:04grade:+所有输入参数;

前两个参数不是case1和case2,那么输出:not grade

 

 

#!/bin/bash
#Function:practise if then
#Date:2011 04 21
#Version:1.0.0.0
LANG=C
export LANG

declare -i intnum=$#
declare -a var[10]
declare result=""

if [ $intnum -lt 2 ]; then
  echo "short of parameter"
else
  for (( b=1; b<=$intnum; b=b+1 ))
  do
    eval var[$b]=\$$b
    result="$result${var[$b]} "
  done
  if [ "${var[1]}" == "moon" ] && [ "${var[2]}" == "tree" ]; then
    echo "03grade: $result"
  elif [ "${var[1]}" == "golden" ] && [ "${var[2]}" == "flower" ]; then
      echo "04grade: $result"
  else
    echo "not grade"
  fi
fi
echo "output successfully!"
exit 0

 

运行结果如下:

 

 

示例二如下:

port-test.sh

#!/bin/bash
#Function:using "if then" to show host's service.
#Date:2011 04 21
#Version:1.0.0.0
#1.print the program's work
 echo "Now,the service of your linux system will be detect!"
 echo "the www,ftp,ssh, and send mail+pop3 will be detect!"
 echo " "
#2.www
  www='netstat -an|grep LISTEN|grep :80'
  if [ "$www" != ""]; then
    echo "WWW is running"
  else
    echo "WWW is NOT running"
  fi

#3.ftp
  ftp='netstat -an|grep LISTEN|grep :21'
  if [ "$ftp" != ""]; then
    echo "FTP is running"
  else
    echo "FTP is NOT running"
  fi

#4.ssh
  ssh='netstat -an|grep LISTEN|grep :22'
  if [ "$ssh" != ""]; then
    echo " SSH is running"
  else
    echo "SSH is NOT running"
  fi
#5.smtp+pop3
   smtp='netstat -an|grep LISTEN|grep :25'
   pop3='netstat -an|grep LISTEN|grep :110'
   if [ "$smtp" != "" ] && [ "$pop3" != "" ]; then
     echo "sendmail is OK!"
   elif [ "$smtp" != "" ] && [ "$pop3" == "" ]; then
     echo "sendmail have some proplems of your pop3!"
   elif [ "$smtp" == ""] && [ "$pop3" != "" ]; then
     echo "sendmail have some proplems of your SMTP!"
   else
     echo "sendmail is not running!"
   fi

运行结果:

 

 

②case句式:基本语法如下

 case $变量名称 in

  “第一个变量内容”)

    程序段

    ;;

 

   “第二个变量内容”)

    程序段

    ;;

 

    “第三个变量内容”)

     程序段

     ;;

     ……

     ……

     *

     程序段

     ;;

 esac

 

示例如下:case-test.sh

只能输入1个参数,否则输出"too more parameters"
参数是Ruying,那么输出05;

参数是moontree,那么输出03;

参数是goldenflower,那么输出04;

都不是,就输出not known。

 

源码:

#!/bin/bash
#Function:practise  usage of case
#Date:2011 04 21
#Version:1.0.0.0

declare -i a=$#
if [ $a -gt 1 ]; then
   echo "too more parameters"
else
   case $1 in
     "Ruying")
       echo "05"
       ;;
     "moontree")
       echo "03"
       ;;
     "goldenflower")
       echo "04"
       ;;
     *)
        echo "not known"
       ;;
    esac

fi
exit 0

 

运行结果:

 

 

③for循环(还有while until等不定循环,用法大同小异)

 

示例一:forlist-test.sh

#!/bin/bash
#Function:pratise the usage of " for loop " limited 10
#Date:2011 04 21
#Version:1.0.0.0

#fuction begin

for animal in dog cat elephant monkey horse duck bear
do
  echo "there are ${animal}s in the zoo"
done

exit 0

运行结果如下:

 

 

示例二:输入一个数字 计算阶乘,当然有不够严谨的地方,自己练习,随便写写。

for-n-test.sh

#!/bin/bash
#Function:practise the usage of "for loop" unlimited
#Date:2011 04 21
#Version:1.0.0.0

#1.deal parameters
declare -i count=$#

if [ $count -gt 1 ]; then
  echo "too more parameters!"
else
  declare -i n=$1
  declare -i result=1;
  for j in $(seq 1 $n)
  do
   result=$result*j
  done

  echo $result
fi

运行结果如下: 

 

 

④read:从键盘读取数据,赋给变量。

示例一:

read-test.sh

#!/bin/bash
#Funtion:practise read values for keyb
#Date:2011 04 21
#Version:1.0.0.0

#tips for uses to input
echo "please enter your name and then press ENTER:"
read name

#print the key in data
echo "this is your name that you key in :" $name
exit 0

运行结果如下:


示例二

 

⑤declare用法:用declare声明出整型变量,后面的字符串就会计算。

示例:

declare-test

#!/bin/bash
#Function:for practise "declare"
#Date:2011 04 21
#Version:1.0.0.0
number1=2*3*4*5+90+21
declare -i number2=2*3*4*5+90+21

echo "the result of number1: $number1"
echo "the result of number1: $number2"

exit 0

 

示例二

 

示例三

 

 

 

⑥shell下如何处理接收参数呢?

示例:parameter-test.sh

#!/bin/bash
#Function:practise how the program define what are parameters
#Date:2011 04 21
#Version:1.0.0.0
echo "the scripts'name is: $0"
echo "the parameters are: $1 $2 $3"
exit 0

运行结果如下:

 

 

阅读(758) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~