Shell基本语法

#!/bin/bash

#输出一句话

echo "what's your name?"

#从键盘获取输入内容,并将其赋值给name变量

read name

echo "hello ${name},good luck!"

echo "JAVA_HOME=${JAVA_HOME}"

#特殊变量

echo "current programe pid={$$}"

echo "current programe file name={$0}"

echo "param1={$1},param1={$2}"

echo "param length={$#}"

echo "params*={$*}"

echo "params@={$@}"

#`命令赋值给变量`

date=`date`

who=`who`

echo "system common-date=${date}"

echo "system common-who=${who}"

echo "pwd=`pwd`"

#expr基础运算

a=20

b=10

result=`expr $a + $b`

echo "a+b=$result"

result=`expr ${a} - ${b}`

echo "a-b=${result}"

result=`expr ${a} \* ${b}`

echo "a*b=${result}"

#if语句+等于运算符

if [ ${a} == ${b} ]

then

echo "a==b,true"

fi

#if语句+不等于运算符

if [ ${a} != ${b} ]

then

echo "a!=b,true"

fi

#if-else语句

if [ ${a} == ${b} ]

then

echo "a=b"

else

echo "a!=b"

fi

#if-elseif语句,使用\进行转移,否则会重定向一个名为${b}空文件。也可以使用-gt进行比较,类似的还有-eq -ne -gt -lt -ge -le

if [ ${a} \> ${b} ]

then

echo "a>b"

elif [ ${a} == ${b} ]

then

echo "a=b"

else

echo "a<b"

fi

#字符串

str="this is a boy"

echo "str.length=${#str[n]}"

#数组

colorArray=(black red blue)

echo "colorArray=${colorArray[*]}"

echo "colorArray1=${colorArray[1]}"

echo "colorArrayLength=${#colorArray[n]}"

#switch-case语句

echo "input type value:"

read type

case ${type} in

0) echo "type is 0"

;;

1) echo "type is 1"

;;

2|3)

echo "type is 2 or 3"

;;

*) echo "type error"

;;

esac

#for循环

for num in 1 2 3 4 5 6 

do

echo ${num}

done

#for循环

for ((i=1;$i<=5;i++))

do

echo "i=$i"

done

#while循环(条件为true时进入循环)

count=0

while [ ${count} -lt 5 ]

do

count=`expr ${count} + 1`

echo "count=${count}"

done

#until循环(条件为false时进入循环)

count=0

until [ ! ${count} -lt 5 ]

do

count=`expr ${count} + 1`

echo "count2=${count}"

done

#函数声明

sayHello() {

echo "hello man!"

}

#函数调用

sayHello

#函数声明+返回值

add() {

echo "input num1:"

read inputNum1

echo "input num2:"

read inputNum2

echo "user input content:num1=${inputNum1},num2=${inputNum2}"

return `expr ${inputNum1} + ${inputNum2}`

}

#函数调用

add

#函数返回值

returnValue=$?

echo "add function return value:${returnValue}"

#函数嵌套调用

login() {

echo "input username:"

read login

echo "input password:"

read passwod

auth

authReturnValue=$?

echo "authReturnValue=${authReturnValue}"

if [ ${authReturnValue} == 1 ] 

then

home

fi

}

auth() {

echo "auth ok"

return 1

}

home() {

echo "welcome index.html"

}

login

#函数传参

addList() {

echo "function param 1:$1"

echo "function param 2:$2"

echo "function param 3:$3"

#多余10个参数时,使用${n}获取参数值。使用$10会被解析为$1+0

echo "function param 10:${10}"

}

addList one two three four five six seven eight nine ten shiyi shier

#输出重定向

pwd > result.txt

echo "append content" >> result.txt

#禁止输出(屏蔽标准输出文件stdout)

ifconfig > /dev/null

#禁止输出(屏蔽标准输出文件stdout和标准错误文件stderr),ifconfig -all为错误命令

ifconfig -all > /dev/null 2>&1

#一般情况下,每个 Unix/Linux 命令运行时都会打开三个文件:

#标准输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据。

#标准输出文件(stdout):stdout 的文件描述符为1,Unix程序默认向stdout输出数据。

#标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。

相关推荐