日期:2014-05-16  浏览次数:20763 次

Linux 一些简单的Shell实例

?

?

[root@localhost temp]# cat a.sh
#!/bin/bash
#hello
echo '你好'
echo 'Hello World!'
[root@localhost temp]# ./a.sh
你好
Hello World!
[root@localhost temp]# cat b.sh
#!/bin/bash
#bianliang b.sh
a=123
b=1.23
c=xyz
d=efgh xyz
e='efgh xyz'
echo $a
echo $b
echo $c
echo $d
echo $e
[root@localhost temp]# ./b.sh
./b.sh: line 6: xyz: command not found
123
1.23
xyz

efgh xyz
[root@localhost temp]# cat c.sh
#!/bin/bash
#c.sh
echo $1
echo $2
echo $3
echo $4
[root@localhost temp]# ./c.sh a b c d e
a
b
c
d
[root@localhost temp]# cat e.sh
#!/bin/bash
#e.sh
echo 开始
read a
i=$[ $a % 2 ]
echo -e $a"\c"
if test $i -eq 0
  then echo 是偶数
else
 echo 是奇数
fi
echo 结束
[root@localhost temp]# ./e.sh
开始
3
3是奇数
结束
[root@localhost temp]# cat f.sh
#!/bin/bash
#e.sh
echo 开始
i=$[ $1 % 2 ]
echo -e $1"\c"
if test $i -eq 0
  then echo 是偶数
else
 echo 是奇数
fi
echo 结束
[root@localhost temp]# ./f.sh 8
开始
8是偶数
结束
[root@localhost temp]# cat g.sh
#!/bin/bash
#g.sh
if test -z $1
  then echo "请输入一个文件名"
else
  if test -w $1
    then echo "可写"
  else
    echo "不可写"
  fi
  if test -x $1
    then echo "可执行"
  else
    echo "不可执行"
  fi
  if test -z $2
   then echo "参数2要输入"
  elif test $2 -eq 1
    then echo "输入1"
  elif test $2 -eq 2
    then echo "输入2"
  else
    echo "输入"$2
  fi
fi
[root@localhost temp]# ./g.sh a.sh 2
可写
可执行
输入2
[root@localhost temp]# cat h.sh
#!/bin/bash
#h.sh
for char in a b c d e
do
  echo $char
done

for str
do
  echo $str
done
[root@localhost temp]# ./h.sh
a
b
c
d
e
[root@localhost temp]# cat i.sh
#!/bin/bash
#i.sh
files=`ls *.sh`
for sh in $files
do
  txt=`echo $sh | sed "s/.sh/.txt/"`
  cp $sh $txt
  echo $txt
done
[root@localhost temp]# ./i.sh
a.txt
b.txt
c.txt
d.txt
e.txt
f.txt
g.txt
h.txt
i.txt
j.txt
k.txt
[root@localhost temp]# cat j.sh
#!/bin/bash
#j.sh
for i in 1 2 3 4 5 6 7 8 9
do
  for j in 1 2 3 4 5 6 7 8 9
  do
    if test $i -ge $j
      then echo -e $j \* $i = $[ $j * $i ]"  \c"
    fi
  done
  echo ""
done
[root@localhost temp]# ./j.sh
1 * 1 = 1  
1 * 2 = 2  2 * 2 = 4  
1 * 3 = 3  2 * 3 = 6  3 * 3 = 9  
1 * 4 = 4  2 * 4 = 8  3 * 4 = 12  4 * 4 = 16  
1 * 5 = 5  2 * 5 = 10  3 * 5 = 15  4 * 5 = 20  5 * 5 = 25  
1 * 6 = 6  2 * 6 = 12  3 * 6 = 18  4 * 6 = 24  5 * 6 = 30  6 * 6 = 36  
1 * 7 = 7  2 * 7 = 14  3 * 7 = 21  4 * 7 = 28  5 * 7 = 35  6 * 7 = 42  7 * 7 = 49  
1 * 8 = 8  2 * 8 = 16  3 * 8 = 24  4 * 8 = 32  5 * 8 = 40  6 * 8 = 48  7 * 8 = 56  8 * 8 = 64  
1 * 9 = 9  2 * 9 = 18  3 * 9 = 27  4 * 9 = 36  5 * 9 = 45  6 * 9 = 54  7 * 9 = 63  8 * 9 = 72  9 * 9 = 81  
[root@localhost temp]# cat k.sh
#!/bin/bash
#k.sh
echo -e "请输入一个1-100之间的整数:\c"
flag=0
until [ $flag -eq 1 ]
do
  read num
  expr $num "+" 10 &> /dev/null
  if test $? -ne 0 
    then echo -e $num"不是整数,请重新输入:\c"
  else
    if test $num -ge 1 -a $num -le 100
      then flag=1
    else
      echo -e $num"不在1-100之间,请重新输入:\c"
    fi
  fi
done
sum=0
i=1
until test $i -gt $num
do
  sum=$[$sum+$i]
  i=$[$i+1]
done
echo ""
echo "从1到"$num"的总和为:"$sum
[root@localhost temp]# ./k.sh
请输入一个1-100之间的整数:a
a不是整数,请重新输入:0      
0不在1-100之间,请重新输入:101
101不在1-100之间,请重新输入:100.01
100.01不是整数,请重新输入:20

从1到20的总和为:210
[root@localhost temp]# 

?

?