shell编程基础
2015-10-04 20:54
453 查看
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
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基础练习题讲解
- 第一部分shell编程1基础知识
- 第二部分shell编程2正则(grepegrepsedawk)
- 第三部分shell编程3(shell脚本编写1)
- 第三部分shell编程3(shell脚本2)
- 第四部分shell编程5项目二分发系统
- 第五部分shell项目一监控脚本
- shell练习题讲解
- Shell基础-Bash变量-用户自定义变量
- 队列——PowerShell版
- runtime.exec()
- linux 编写自己的shell
- /bin/bash^M: bad interpreter: 没有那个文件或目录
- Shell Script学习笔记1:循环表达(for, while, until)
- shell 生成指定范围随机数与随机字符串
- linux shell学习 记录2 if elif case for while until 语法
- [20150925]Linux之文件系统与SHELL
- 快速排序——PowerShell版
- Hadoop学习笔记(六)启动Shell分析
- centos 正则,grep,egrep,流式编辑器 sed,awk -F 多个分隔符 通配符 特殊符号. * + ? 总结 问加星 cat -n nl 输出文件内容并加上行号 alias放~/.bash_profile 2015-4-10 第十三节课