码迷,mamicode.com
首页 > 系统相关 > 详细

shell常见语法

时间:2016-12-07 22:55:03      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:first   image   let   cas   equal   print   bin   技术   语法   

#!/bin/bash
echo "**********argument**********"
echo script name: $0
echo first argument $1
echo second argument $2
echo num of argument $#
echo all argument $@

#function
equal()
{
  case $1 in
  $2)
    return 0
  ;;
  *)
    return 1
  ;;
  esac
}

#if
echo "**********test if**********"
if equal $1 $2;
then
  echo "equal"
else
  echo "not equal"
fi

if [ $# -gt 0 ];
then
  echo number of argument greater than 0
  echo $@
else
  echo number of argument is 0
fi

#for
echo "**********test for**********"
for i in $@
do
  echo $i
done
LIMIT=5
for (( a=1; a<=$LIMIT; a++ ))
do
  echo "$a"
done

#while
echo "**********test while**********"
a=0
LIMIT=10
while [ $a -lt $LIMIT ]
do
  echo $a
  a=$((a+1))
done

#case
echo "**********test case**********"
read -p "press some key, then press return:" KEY
case $KEY in
[a-zA-Z])
  echo "It‘s a letter!"
  ;;
[0-9])
  echo "It‘s a number!"
  ;;
*)
  echo "other key!"
  ;;
esac

#printf
echo "**********test printf**********"
x=abc; printf "x is now: %s, Enter new value:" $x; read x

运行./test.sh 1 1结果如下:

技术分享

技术分享

 

shell常见语法

标签:first   image   let   cas   equal   print   bin   技术   语法   

原文地址:http://www.cnblogs.com/fellow1988/p/6143068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!