SHELL 入门学习
shell 变量
vim shellViable.sh
#! /bin/bash
# shell demo
string="abcd"
echo ${#string}
echo $string
echo "Sheel "
echo "$0"
echo "$1"
echo "$2"
echo "$3"
val=`expr 2 + 2`
echo "number sum:$val"
[root@alliyun cainiaoSheelScript]# chmod +x shellViable.sh
[root@alliyun cainiaoSheelScript]# ./shellViable.sh
4
abcd
Sheel
./shellViable.sh
number sum:4
shell echo
vim shellEcho.sh
#! /bin/bash
# echo
echo "It is a test"
echo It is a test
read name
echo $name It is a test
echo It is a test >myfile
[root@alliyun cainiaoSheelScript]# chmod +x shellEcho.sh
[root@alliyun cainiaoSheelScript]# ./shellEcho.sh
It is a test
It is a test
look
look It is a test
shell printf
vim shellPrintf.sh
#! /bin/bash
# printf
echo Hello,sheel
printf Hello,sheel "\n"
printf "%-10s %-8s %-4s \n"
printf "%-10s %-8s %-4.2f \n"
printf "%-10s %-8s %-4.2f \n"
printf "%-10s %-8s %-4.2f \n"
[root@alliyun cainiaoSheelScript]# chmod +x shellPrintf.sh
[root@alliyun cainiaoSheelScript]# ./shellPrintf.sh
Hello,sheel
Hello,sheel
0.00
0.00
0.00
shell test
vim shellTest.sh
#! /bin/bash
# sheel test
num1=100
num2=101
if test $num1 -eq $num2
then
echo true
else
echo false
fi
[root@alliyun cainiaoSheelScript]# chmod +x shellTest.sh
[root@alliyun cainiaoSheelScript]# ./shellTest.sh
false
shell if then
vim shellIfThen.sh
#! /bin/bash
# if then else then
num1=100
num2=200
if [ $num1 -eq 100 ]
then
echo num1 is 100
else
echo num1 is not 100
fi
if [ $num2 -eq 100 ]
then
echo num2 is 100
elif [ $num2 -eq 200 ]
then
echo num2 is 200
else
echo num2 is not 200
fi
~
[root@alliyun cainiaoSheelScript]# chmod +x shellIfThen.sh
[root@alliyun cainiaoSheelScript]# ./shellIfThen.sh
num1 is 100
num2 is 200
shell While
vim shellWhile.sh
#! /bin/bash
# sheel while
int=1
while (($int<=5))
do
echo $int
let int++
done
[root@alliyun cainiaoSheelScript]# chmod +x shellWhile.sh
[root@alliyun cainiaoSheelScript]# ./shellWhile.sh
1
2
3
4
5
shell function
vim shellFunction.sh
#! /bin/bash
# function
domoFun(){
echo this is my first sheel fun!
}
echo fun start..
domoFun
echo fun end...
[root@alliyun cainiaoSheelScript]# chmod +x shellFunction.sh
[root@alliyun cainiaoSheelScript]# ./shellFunction.sh
fun start..
this is my first sheel fun!
fun end...