linux bash shell.sh编程实例
2011-03-15 13:45
1066 查看
linux bash shell.sh编程实例(2009-04-25 14:20:05)
1.输出
#!/bin/bash
echo "Please type your number:"
read a
for ((i=1;i<=a;i++))
do
for ((p=1;p<=i;p++))
do
echo -n " $p "
done
echo
done
echo
2.九九乘法表
#!/bin/bash
for ((i=1;i<10;i++))
do
for ((p=1;p<=i;p++))
do
echo -ne " $p x $i= "`expr $i /* $p`
done
echo
done
echo
3.计算器
#!/bin/bash
s=0
while true
echo "..............+"
echo "..............-"
echo "..............x"
echo "............../"
echo "..............q"
echo "Please type your word:(e.g.1 + 2)"
read a b c
do
case $b in
+)
let s=a+c
echo " $a + $c =" $s;;
-)
let s=a-c
echo " $a - $c =" $s;;
/x)
let s=a*c
echo " $a x $c =" $s;;
//)
let s=a/c
echo " $a / $c =" $s;;
esac
case $a in
q) break ;;
esac
done
4.菱形
#!/bin/bash
echo "Please type a number:"
read num
for ((i=1;i<=num;i++))
do
for ((j=0;j<num-i;j++))
do
echo -n " "
done
for ((j=0;j<2*i-1;j++))
do
echo -n "*"
done
echo ""
done
for ((i=1;i<=num;i++))
do
for ((j=0;j<i;j++))
do
echo -n " "
done
for ((j=0;j<2*(num-i)-1;j++))
do
echo -n "*"
done
echo ""
done
5. 输出当前目录下所有文件,并输出文件总数和目录总数
#!/bin/bash
ls -al
filenum=0
dirnum=0
for q in `ls -a`
do
if [ -d $q ]
then
dirnum=`expr $dirnum + 1`
fi
filenum=`expr $filenum + 1`
done
echo "The number of dirctary is $dirnum"
echo "The number of file is $filenum"
6.菜单
#!/bin/bash
while true
echo "List Directory..........l "
echo "Change Directory........c "
echo "Edit File...............e "
echo "Remove File.............r "
echo "Exit Menu...............q "
read ch
do
case $ch in
l) ls;;
c) echo Enter target directory
read direc
cd "$direc" ;;
e) echo Enter file name
read file
vi $file ;;
r) echo Enter file name
read file
rm $file ;;
q|Q) echo Goodbye
break;;
*) echo illegal Option
esac
done
7. ./shuru a b c d e f
#!/bin/bash
for (( b=$#; b>0; b-- ))
do
echo $*;
shift;
done
8.电话本
#!/bin/bash
if [ ! -f /root/minda ]
then
touch /root/minda
fi
while true
echo "If you want to add a user plese type----------------------a"
echo "If you want to del a user plese type----------------------d"
echo "If you want to watch all users please type----------------l"
echo "If you want to bianji users please tyye-------------------e"
echo "If you want to exit please type---------------------------q"
read b
do
case $b in
a) echo "Please type your name :"
read name
echo "Please type your number :"
read number
echo $name $number >> /root/minda;;
d) echo "Please type which name do you want to del:"
read name
cat /root/minda|grep ^[^$name]> /root/minda;;
l) cat /root/minda;;
e) echo "Please type which name do you want to del:"
read name
cat /root/minda|grep ^[^$name]> /root/minda
echo "Please type your new name :"
read name
echo "Please type your new number :"
read number
echo $name $number >> /root/minda;;
q) exit;;
esac
done
9.判断当前用户的类型
#!/bin/bash
case $UID in
0) echo this is root;;
[0-9][0-9]) echo this is system;;
[1-4][0-9][0-9]) echo this is service;;
*) echo this is public;;
esac
10.计算1-100的和
#!/bin/bash
b=0
for ((a=1;a<=100;a++))
do
b=`expr $b + $a `
done
echo b= $b
11.用一个文件记录系统中所有rpm包名,如果文件不存在则创建,如果存在则显示文件内容。
#!/bin/bash
a=/root/file
if [ -f $a ]
then
cat $a
else
rpm -qa > $a
fi
12.用户登陆
#!/bin/bash
n=1
while [ $n -gt 0 ]
do
echo "xinjian------------------1"
echo "denglu-------------------2"
echo "exit---------------------3"
read n;
case $n in
1) echo "username"
read name
if [ -f $name ]
then
echo "same username"
else
touch /root/$name
echo "passwd"
read passwd
echo $passwd >> /root/$name
fi;;
2) echo "username"
read name1
if [ -f $name1 ]
then
echo "passwd"
else
echo "yonghu wrong"
fi
read passwd1
if [ $passwd1 -eq `cat /root/$name1` ]
then
echo "success"
break
else
echo "passwd wrong"
fi;;
3) break;;
esac
done
13.创建四个文件test1,test2,test3,test4,实现自动创建dir1,dir2,dir3,dir4四个目录,并将test1,test2,test3,test4四个文件分别拷贝到dir1,dir2,dir3,dir4相应的目录下。
#!/bin/bash
for a in 1 2 3 4
do
touch test$a
mkdir dir$a
cp test$a dir$a
done
14.输入任意数求和./a.out 1 2 3 4 5 6
#!/bin/bash
s=0
for i in $*
do
s=`expr $s + $i`
done
echo $s
标签:it | 分类:原创 |
#!/bin/bash
echo "Please type your number:"
read a
for ((i=1;i<=a;i++))
do
for ((p=1;p<=i;p++))
do
echo -n " $p "
done
echo
done
echo
2.九九乘法表
#!/bin/bash
for ((i=1;i<10;i++))
do
for ((p=1;p<=i;p++))
do
echo -ne " $p x $i= "`expr $i /* $p`
done
echo
done
echo
3.计算器
#!/bin/bash
s=0
while true
echo "..............+"
echo "..............-"
echo "..............x"
echo "............../"
echo "..............q"
echo "Please type your word:(e.g.1 + 2)"
read a b c
do
case $b in
+)
let s=a+c
echo " $a + $c =" $s;;
-)
let s=a-c
echo " $a - $c =" $s;;
/x)
let s=a*c
echo " $a x $c =" $s;;
//)
let s=a/c
echo " $a / $c =" $s;;
esac
case $a in
q) break ;;
esac
done
4.菱形
#!/bin/bash
echo "Please type a number:"
read num
for ((i=1;i<=num;i++))
do
for ((j=0;j<num-i;j++))
do
echo -n " "
done
for ((j=0;j<2*i-1;j++))
do
echo -n "*"
done
echo ""
done
for ((i=1;i<=num;i++))
do
for ((j=0;j<i;j++))
do
echo -n " "
done
for ((j=0;j<2*(num-i)-1;j++))
do
echo -n "*"
done
echo ""
done
5. 输出当前目录下所有文件,并输出文件总数和目录总数
#!/bin/bash
ls -al
filenum=0
dirnum=0
for q in `ls -a`
do
if [ -d $q ]
then
dirnum=`expr $dirnum + 1`
fi
filenum=`expr $filenum + 1`
done
echo "The number of dirctary is $dirnum"
echo "The number of file is $filenum"
6.菜单
#!/bin/bash
while true
echo "List Directory..........l "
echo "Change Directory........c "
echo "Edit File...............e "
echo "Remove File.............r "
echo "Exit Menu...............q "
read ch
do
case $ch in
l) ls;;
c) echo Enter target directory
read direc
cd "$direc" ;;
e) echo Enter file name
read file
vi $file ;;
r) echo Enter file name
read file
rm $file ;;
q|Q) echo Goodbye
break;;
*) echo illegal Option
esac
done
7. ./shuru a b c d e f
#!/bin/bash
for (( b=$#; b>0; b-- ))
do
echo $*;
shift;
done
8.电话本
#!/bin/bash
if [ ! -f /root/minda ]
then
touch /root/minda
fi
while true
echo "If you want to add a user plese type----------------------a"
echo "If you want to del a user plese type----------------------d"
echo "If you want to watch all users please type----------------l"
echo "If you want to bianji users please tyye-------------------e"
echo "If you want to exit please type---------------------------q"
read b
do
case $b in
a) echo "Please type your name :"
read name
echo "Please type your number :"
read number
echo $name $number >> /root/minda;;
d) echo "Please type which name do you want to del:"
read name
cat /root/minda|grep ^[^$name]> /root/minda;;
l) cat /root/minda;;
e) echo "Please type which name do you want to del:"
read name
cat /root/minda|grep ^[^$name]> /root/minda
echo "Please type your new name :"
read name
echo "Please type your new number :"
read number
echo $name $number >> /root/minda;;
q) exit;;
esac
done
9.判断当前用户的类型
#!/bin/bash
case $UID in
0) echo this is root;;
[0-9][0-9]) echo this is system;;
[1-4][0-9][0-9]) echo this is service;;
*) echo this is public;;
esac
10.计算1-100的和
#!/bin/bash
b=0
for ((a=1;a<=100;a++))
do
b=`expr $b + $a `
done
echo b= $b
11.用一个文件记录系统中所有rpm包名,如果文件不存在则创建,如果存在则显示文件内容。
#!/bin/bash
a=/root/file
if [ -f $a ]
then
cat $a
else
rpm -qa > $a
fi
12.用户登陆
#!/bin/bash
n=1
while [ $n -gt 0 ]
do
echo "xinjian------------------1"
echo "denglu-------------------2"
echo "exit---------------------3"
read n;
case $n in
1) echo "username"
read name
if [ -f $name ]
then
echo "same username"
else
touch /root/$name
echo "passwd"
read passwd
echo $passwd >> /root/$name
fi;;
2) echo "username"
read name1
if [ -f $name1 ]
then
echo "passwd"
else
echo "yonghu wrong"
fi
read passwd1
if [ $passwd1 -eq `cat /root/$name1` ]
then
echo "success"
break
else
echo "passwd wrong"
fi;;
3) break;;
esac
done
13.创建四个文件test1,test2,test3,test4,实现自动创建dir1,dir2,dir3,dir4四个目录,并将test1,test2,test3,test4四个文件分别拷贝到dir1,dir2,dir3,dir4相应的目录下。
#!/bin/bash
for a in 1 2 3 4
do
touch test$a
mkdir dir$a
cp test$a dir$a
done
14.输入任意数求和./a.out 1 2 3 4 5 6
#!/bin/bash
s=0
for i in $*
do
s=`expr $s + $i`
done
echo $s
相关文章推荐
- Linux + Shell bash / sh / ksh / csh / tcsh
- Linux下shell编程实例
- 对Linux下shell编程之for循环的实例讲解
- Bash 实例,第一部分Bourne again shell (bash) 基本编程
- Bash 实例,第 1 部分:Bourne again shell (bash) 基本编程
- Linux-SHELL中sh和bash的区别
- Shell(Bash)编程实例之获取某个文件夹下的所有文件名(含文件夹)
- Linux下shell编程实例
- Excel_VBA_编程教程(完整版)、开发环境、实例、函数大全、SVN、linux及shell学习
- linux bash shell中case语句的实例
- Linux-SHELL中sh和bash的区别
- linux云计算之Bashshell脚本编程视频教程!
- Bash/KSH/SH Shell: Find The Length Of a String On a UNIX, Linux, BSD, and OS X
- Bash 实例,第 1 部分:Bourne again shell (bash) 基本编程
- linux bash shell中case语句的实例
- Bash 实例,第一部分---Bourne again shell (bash) 基本编程
- LinuxC/C++编程基础(13) shell脚本简单实例
- Shell(Bash)编程实例之获取某个文件夹下的所有文件名(含文件夹)
- linux几种常见的Shell:sh、bash、csh、tcsh、ash
- 总结下linux中一些入门级shell编程实例