shel编程一点就通

1.一个简单的hello world例子

#!bin/bash
echo "hello wolrd"

其中#!bin/bash是告诉系统shell解释器所在的位置


2.shell变量

基本的赋值方式:name="XXX"。注意等号两边都不能带有空格,否则出现错误;字符串必须用引号括起。


3.shell输入

#!bin/bash
echo "please input your name and age"
read name age
echo "your name is "$name
echo "your age is "$age

直接从终端输入两个值,分别赋值给name和age变量


4.shell数组

http://bbs.chinaunix.net/thread-1779167-1-1.html


5. if 控制语句

#!/bin/bash
echo "please input your name"
read name
if test -n name
then
    echo "your name is "$name
else
    echo "name is null"
fi

#!/bin/bash
echo "please input num A and B"
read a b
if test $a -lt $b 
then
    echo "a < b"
elif test $a -gt $b
then
    echo "a > b"
else
    echo "a = b"
fi

6. case语句

#!/bin/bash
echo "please input your name"
read name
case $name in
tom)
    echo "hello tom";;
jane)
    echo "hello jane";;
*)
    echo "I don't know your name";;
esac

7.for 语句

#!/bin/bash
for i in 1 2 3 4 5 6 7
do 
    echo $i
done

8.while语句

#!/bin/sh
i=1
while test $i -lt 5
do 
   echo $i
   i=${{$i+1}}
done



你可能感兴趣的:(shell)