shell常见语法

#!/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结果如下:

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