shell常见语法

Posted fellow_jing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell常见语法相关的知识,希望对你有一定的参考价值。

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

#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 [email protected]
else
  echo number of argument is 0
fi

#for
echo "**********test for**********"
for i in [email protected]
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常见语法的主要内容,如果未能解决你的问题,请参考以下文章

linux shell 基础语法A-2

Shell编程之三 —— shell script 脚本(未完待续)

Shell ❀ 条件判断语句

Shell ❀ 条件判断语句

Shell ❀ 条件判断语句

自动化运维必须要学的Shell脚本之——管道命令(sort/tr/cut等)