码迷,mamicode.com
首页 > 系统相关 > 详细

shell编程基础

时间:2015-10-04 22:08:56      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

for c in a b c d e f g;do echo $c; done 按顺序输出


for c in a b c d e f g;do tar czvf install.log.$c.tar.gz install.log ; done

for c in {a..g}; do echo $c; done 和上面的一样


seq 1 10 列数字的

for i in {1..10}; do echo $i; done

 

 

字符串变量实例
str=‘I love linux. I love unix too.‘

echo ${str} 计算字符串长度

echo ${str:5} 截取str从第五个字符到串尾

echo ${str:7:5} 截取str从第七个字符开始的第五个字符


echo ${str#I love} 删除开始的字符串

echo ${str#I*.} 删除开始I到.的所有字符,最短匹配

echo ${str##I*} 删除开始I到.的所有字符,最长匹配

 

test 是测试的意思

shell 编程:
单分支
#!/bin/bash
filename=/myshell/test1
if test -f $filename; then
echo ‘The file is exist.‘
fi


双分支
#!/bin/bash
filename=/myshell/test1
if test -f $filename; then
echo ‘The file is exist.‘
else
echo ‘The file is not exist.‘
fi

 

#!/bin/bash
filename=/myshell/test2
if [ -f $filename ] ; then
echo ‘The file is exist.‘
else
echo ‘The file is not exist.‘
fi

 

 


判断
#!/bin/bash
a=this
b=there
[ $a != $b ]&&echo Yes || echo No

 

#!/bin/bash
filename=/myshell/test3
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‘

 

多分支
#!/bin/bash
echo ‘Please input your number.‘
read number
if [ $number == 1 ] ; then
echo ‘Your input number is 1 .‘
elif [ $number == 2 ] ; then
echo ‘Your input number is 2 .‘
elif [ $number == 3 ] ; then
echo ‘Your input number is 3 .‘
else
echo ‘I dont know what you input.‘
fi

 

echo ‘Please input your hardware.‘
read hd
if [ $hd == cpu ] ; then
echo ‘Your cpu info is .‘
cat /proc/cpuinfo
elif [ $hd == mem ] ; then
echo ‘Your mem 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

 

echo ‘Please input your 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

 

echo ‘Please input kemu.‘
read kemu
case $kemu in
shuxue)
echo ‘Your input is shuxue.‘;;
english)
echo ‘Your input is english .‘;;
yuwen)
echo ‘Your input is yuwen .‘;;
*)
echo ‘I dont know what you input.‘;;
esac

 

 


while循环:

#!/bin/bash
# [] -eq -ne -gt -ge -lt -le
#( () ) == != > >= < <=
#program summary

i=10
#while (($i>5)) ;do
while [ $i -gt 5 ] ;do
echo $i;
#if [ $i -eq 8 ] ;then
# echo ‘The number is 8.‘
#fi
((i--)) ;
done;


until循环:

#!/bin/bash

a=10
until (($a<0)) ;do
echo $a;
((a--)) ;
done;

 

 

 


for循环:
#!/bin/bash
for((i=1;i<=10;i++));do
echo $i;
done;


#!/bin/bash
echo ‘Please input an numer.‘
read number
for((i=1;i<=$number;i++));do
echo $i;
done;

 


函数:

#!/bin/bash
function print(){
echo "Your input is $x"
}

echo "This program will print you selection!"

case $x in
"one")
print 1;;
"two")
print 2;;
"three")
print 3;;
*)
print "Usage $0 one|two|three";;
esac

 

 

 

shell编程基础

标签:

原文地址:http://www.cnblogs.com/chenshoubiao/p/4854995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!