简单Shell例子:shelltest
#!/bin/bash
echo -e "Hello World.\n"
exit 0
保存退出
输入Shell
sh shelltest
./shelltest //这个要复权
变量
a=aaaa
变量相加
a=10
b=20
echo $(($a+b))
计算字符串长度
v2="aaaaa"
echo ${#v2}
字符串变量实例
str='I love linux.Ilove unix too.'
#计算字符串长度
echo ${str}
#截取str从第5个字符到串尾
echo ${str:5}
#截取str从第7个字符开始的第5个字符
echo ${str:7:5}
截取字符串
v2="zhaoliang831224"
echo ${v2:2}
输入:aoliang831224
#删除开始的字符串
echo ${str#I love}
#删除开始I到.的所有字符
echo ${str#I*.}
#删除开始I到.的所有字符
echo ${str##I*}
test与[]
-f 文件是否存在
-d 目录是否存在
-r 文件是否有读权限
-w 文件是否有写权限
-x 文件是否有执行权限
例子:
test -f $filename && echo 'exist' || echo 'not exist'
test -d $filename && echo 'exist' || echo 'not exist'
test -r $filename && echo 'exist' || echo 'not exist'
test -w $filename && echo 'exist' || echo 'not exist'
test -x $filename && echo 'exist' || echo 'not exist'
filename=/home/zl
test -f $filename && echo 'exist' || echo 'no'
[]判断注意
*在中括号中必须都要使用空格来分隔
*在中括号中的变量,最好都要以双引号括起来
*在中括号中的常数,最好都以单引号括起来
例如:
[ "$a" == "$b" ]&&echo 'Yes' || echo "No"
[ "12" == "10" ]&&echo 'Yes' || echo "No"
a=this
b=there
[ $a == $b ]&&echo 'Yes' || echo "No"
#单分支判断
if [];then
echo statement
fi
#双分支判断
if [];then
echo statement
else
echo statement
fi
#多分支判断
if [];then
echo statement
elif
echo statment
elif
echo statment
fi
==========
【用if [] 来判断】
filename=/home/zhansan
if [ -f $filename ];then
echo 'The file is exist.'
fi
【用if test来判断】
filename=/home/zhansan
if test -f $filename; then
echo 'The file is exist.'
else
echo 'The file is not exist.'
fi
【多分支】
echo 'Please input your number.'
read number
if [ $number == 1 ];then
echo 'Your input numebr is 1.'
elif [ $number == 2 ];then
echo 'Your input numebr is 2.'
elif [ $number == 3 ];then
echo 'Your input numebr is 3.'
else
echo 'I dont know what you input.'
fi
【判断显示电脑信息,用多分支】
#!/bin/bash
echo 'Please input your hardware.'
read hd
if [ $hd == cpu ];then
echo 'You cpu info is.'
cat /proc/cpuinfo
elif [ $hd == mem];then
echo 'Your memory info is.'
cat /proc/meminfo
elif [ $hd == hard ];then
echo 'Your harddisk info is.'
df -h
else
echo 'I dont know what you input.'
fi
【case判断】
#!/bin/bash
echo 'Please input an number.'
read number
case $number in
1)
echo 'Your input number is 1';;
2)
echo 'Your input number is 2';;
3)
echo 'Your input number is 3';;
*)
echo 'I dont know what you input';;
esac
【循环语句】
说明:
#[] -eq -ne -gt -ge -lt -le
#(()) == != > >= < <=
while condition; do
done;
例如:
i=10
while (($i+.5));do
echo $i;
((i--));
done;
===========
i=10
while [ $i -gt 5 ];do
echo $i;
((i--));
done;
untile condition;do
done;
例如:
a=10
until (($a<0));do
echo $a;
((a--));
done;
=========
a=10
until [ $a -lt 0 ];do
echo $a;
((a--));
done;
for (());do
statement
done;
例如:
#!/bin/bash
for((i=1;i<=10;i++));do
echo $i;
done;
【function】
例如:
#!/bin/bash
function print(){
echo "Your input is $1"
}
echo "This program will print your selection!"
case $1 in
"one")
print 1;;
"two")
print 2;;
"three")
print 3;;
*)
print "Usage $0 {one|two|three}";;
esac
#!/bin/bash
echo -e "Hello World.\n"
exit 0
保存退出
输入Shell
sh shelltest
./shelltest //这个要复权
变量
a=aaaa
变量相加
a=10
b=20
echo $(($a+b))
计算字符串长度
v2="aaaaa"
echo ${#v2}
字符串变量实例
str='I love linux.Ilove unix too.'
#计算字符串长度
echo ${str}
#截取str从第5个字符到串尾
echo ${str:5}
#截取str从第7个字符开始的第5个字符
echo ${str:7:5}
截取字符串
v2="zhaoliang831224"
echo ${v2:2}
输入:aoliang831224
#删除开始的字符串
echo ${str#I love}
#删除开始I到.的所有字符
echo ${str#I*.}
#删除开始I到.的所有字符
echo ${str##I*}
test与[]
-f 文件是否存在
-d 目录是否存在
-r 文件是否有读权限
-w 文件是否有写权限
-x 文件是否有执行权限
例子:
test -f $filename && echo 'exist' || echo 'not exist'
test -d $filename && echo 'exist' || echo 'not exist'
test -r $filename && echo 'exist' || echo 'not exist'
test -w $filename && echo 'exist' || echo 'not exist'
test -x $filename && echo 'exist' || echo 'not exist'
filename=/home/zl
test -f $filename && echo 'exist' || echo 'no'
[]判断注意
*在中括号中必须都要使用空格来分隔
*在中括号中的变量,最好都要以双引号括起来
*在中括号中的常数,最好都以单引号括起来
例如:
[ "$a" == "$b" ]&&echo 'Yes' || echo "No"
[ "12" == "10" ]&&echo 'Yes' || echo "No"
a=this
b=there
[ $a == $b ]&&echo 'Yes' || echo "No"
#单分支判断
if [];then
echo statement
fi
#双分支判断
if [];then
echo statement
else
echo statement
fi
#多分支判断
if [];then
echo statement
elif
echo statment
elif
echo statment
fi
==========
【用if [] 来判断】
filename=/home/zhansan
if [ -f $filename ];then
echo 'The file is exist.'
fi
【用if test来判断】
filename=/home/zhansan
if test -f $filename; then
echo 'The file is exist.'
else
echo 'The file is not exist.'
fi
【多分支】
echo 'Please input your number.'
read number
if [ $number == 1 ];then
echo 'Your input numebr is 1.'
elif [ $number == 2 ];then
echo 'Your input numebr is 2.'
elif [ $number == 3 ];then
echo 'Your input numebr is 3.'
else
echo 'I dont know what you input.'
fi
【判断显示电脑信息,用多分支】
#!/bin/bash
echo 'Please input your hardware.'
read hd
if [ $hd == cpu ];then
echo 'You cpu info is.'
cat /proc/cpuinfo
elif [ $hd == mem];then
echo 'Your memory info is.'
cat /proc/meminfo
elif [ $hd == hard ];then
echo 'Your harddisk info is.'
df -h
else
echo 'I dont know what you input.'
fi
【case判断】
#!/bin/bash
echo 'Please input an number.'
read number
case $number in
1)
echo 'Your input number is 1';;
2)
echo 'Your input number is 2';;
3)
echo 'Your input number is 3';;
*)
echo 'I dont know what you input';;
esac
【循环语句】
说明:
#[] -eq -ne -gt -ge -lt -le
#(()) == != > >= < <=
while condition; do
done;
例如:
i=10
while (($i+.5));do
echo $i;
((i--));
done;
===========
i=10
while [ $i -gt 5 ];do
echo $i;
((i--));
done;
untile condition;do
done;
例如:
a=10
until (($a<0));do
echo $a;
((a--));
done;
=========
a=10
until [ $a -lt 0 ];do
echo $a;
((a--));
done;
for (());do
statement
done;
例如:
#!/bin/bash
for((i=1;i<=10;i++));do
echo $i;
done;
【function】
例如:
#!/bin/bash
function print(){
echo "Your input is $1"
}
echo "This program will print your selection!"
case $1 in
"one")
print 1;;
"two")
print 2;;
"three")
print 3;;
*)
print "Usage $0 {one|two|three}";;
esac