shell脚本实例集合
2014-06-06 00:31
239 查看
1. 求某个目录下普通文件的个数
#!/bin/bash
path=/home/chenguolin
count=0
for file in $(ls $path)
do
if [ -f $file ]
then
let count++
fi
done
echo "count = $count"
exit 0
2. 写一个脚本,利用循环求10!
#!/bin/bash
sum=1
for num in $(seq 10)
do
let sum=sum*num
done
echo "sum = $sum"
exit 0
3. 写一个脚本,执行后打印一行提示“please input a number: ”,要求用户输入数值,然后打印数值,然后再次提示用户输入,直到“end”停止
#!/bin/bash
while [ 1 -gt 0 ]
do
echo -n "please input a numner: "
read number
if [ $number == "end" ]
then
break
fi
echo "input number is $number"
done
exit 0
4. 写一个脚本,利用循环计算100以内能被3整除的数的和
#!/bin/bash
sum=0
for num in $(seq 100)
do
let mod=num%3
if [ $mod -eq 0 ]
then
let sum=sum+num
fi
done
echo "sum = $sum"
exit 0
5. 写一个函数,利用shift计算所有参数的乘积,假设参数都是整数
#!/bin/bash
function GetResult(){
sum=1
while [ $# -gt 0 ]
do
let sum=sum*$1
shift
done
echo "sum = $sum"
}
GetResult 1 2 3 4 5
exit 0
6. 写一个脚本模拟Linux登录
#!/bin/bash
echo -n "login:"
read username
echo -n "passwd:"
read passwd
if [ $username == "cgl" -a $passwd == "123" ]
then
echo "login successfuly"
else
echo "login error"
fi
exit 0
7. 删除给定目录下大小为0的文件
#!/bin/bash
path=/home/chenguolin
for file in $(ls $path)
do
num=$(ls -l $file | cut -f5 -d" ")
if [ $num -eq 0 ]
then
$(rm $path/$file)
fi
done
exit 0
8. 写一个脚本,把给定目录下的所有普通文件更改名字为1,2,3.....
#!/bin/bash
path=/home/chenguolin
index=1
for file in $(ls $path)
do
if [ -f $file ]
then
$(mv $file $index)
let index++
fi
done
exit 0
9. 写一个脚本,随机生成32位密码
#!/bin/bash
psd="/proc/sys/kernel/random/uuid"
echo $(cat $psd)
exit 0
10. 写一个脚本,实现批量添加20个用户,用户名为user1-20,密码为user后面跟5个随机字符。
#!/bin/bash
for num in $(seq 20)
do
pwd=$(cat /dev/urandom | head -1 | md5sum | head -c 5)
$(useradd user$num)
echo "user$num$pwd" | passwd --stdin user$num
done
exit 0
11. 写一个脚本,实现判断192.168.1.0/24网络里,当前在线的IP有哪些,能ping通则认为在线
#!/bin/bash
for num in $(seq 25)
do
let num--
$(ping 192.168.1.$num 2>&1 /dev/null)
if [ $? -eq 0 ]
then
echo "192.168.1.$num up"
else
echo "192.168.1.$num down"
fi
done
exit 0
12. 编写个shell 脚本将/usr/local/test 目录下大于100K 的文件转移到/tmp 目录下
#!/bin/bash
path="/usr/local/test"
for file in $(ls $path)
do
if [ -f $file ]
then
if [ $(ls -l $file | cut -f5 -d" ") -gt 100000 ]
then
$(mv $file /tmp)
fi
fi
done
exit 0
#!/bin/bash
path=/home/chenguolin
count=0
for file in $(ls $path)
do
if [ -f $file ]
then
let count++
fi
done
echo "count = $count"
exit 0
2. 写一个脚本,利用循环求10!
#!/bin/bash
sum=1
for num in $(seq 10)
do
let sum=sum*num
done
echo "sum = $sum"
exit 0
3. 写一个脚本,执行后打印一行提示“please input a number: ”,要求用户输入数值,然后打印数值,然后再次提示用户输入,直到“end”停止
#!/bin/bash
while [ 1 -gt 0 ]
do
echo -n "please input a numner: "
read number
if [ $number == "end" ]
then
break
fi
echo "input number is $number"
done
exit 0
4. 写一个脚本,利用循环计算100以内能被3整除的数的和
#!/bin/bash
sum=0
for num in $(seq 100)
do
let mod=num%3
if [ $mod -eq 0 ]
then
let sum=sum+num
fi
done
echo "sum = $sum"
exit 0
5. 写一个函数,利用shift计算所有参数的乘积,假设参数都是整数
#!/bin/bash
function GetResult(){
sum=1
while [ $# -gt 0 ]
do
let sum=sum*$1
shift
done
echo "sum = $sum"
}
GetResult 1 2 3 4 5
exit 0
6. 写一个脚本模拟Linux登录
#!/bin/bash
echo -n "login:"
read username
echo -n "passwd:"
read passwd
if [ $username == "cgl" -a $passwd == "123" ]
then
echo "login successfuly"
else
echo "login error"
fi
exit 0
7. 删除给定目录下大小为0的文件
#!/bin/bash
path=/home/chenguolin
for file in $(ls $path)
do
num=$(ls -l $file | cut -f5 -d" ")
if [ $num -eq 0 ]
then
$(rm $path/$file)
fi
done
exit 0
8. 写一个脚本,把给定目录下的所有普通文件更改名字为1,2,3.....
#!/bin/bash
path=/home/chenguolin
index=1
for file in $(ls $path)
do
if [ -f $file ]
then
$(mv $file $index)
let index++
fi
done
exit 0
9. 写一个脚本,随机生成32位密码
#!/bin/bash
psd="/proc/sys/kernel/random/uuid"
echo $(cat $psd)
exit 0
10. 写一个脚本,实现批量添加20个用户,用户名为user1-20,密码为user后面跟5个随机字符。
#!/bin/bash
for num in $(seq 20)
do
pwd=$(cat /dev/urandom | head -1 | md5sum | head -c 5)
$(useradd user$num)
echo "user$num$pwd" | passwd --stdin user$num
done
exit 0
11. 写一个脚本,实现判断192.168.1.0/24网络里,当前在线的IP有哪些,能ping通则认为在线
#!/bin/bash
for num in $(seq 25)
do
let num--
$(ping 192.168.1.$num 2>&1 /dev/null)
if [ $? -eq 0 ]
then
echo "192.168.1.$num up"
else
echo "192.168.1.$num down"
fi
done
exit 0
12. 编写个shell 脚本将/usr/local/test 目录下大于100K 的文件转移到/tmp 目录下
#!/bin/bash
path="/usr/local/test"
for file in $(ls $path)
do
if [ -f $file ]
then
if [ $(ls -l $file | cut -f5 -d" ") -gt 100000 ]
then
$(mv $file /tmp)
fi
fi
done
exit 0
相关文章推荐
- if嵌套语句 shell脚本实例 测试是否闰年
- 以TXT为载体的数据交换利用SHELL生成导数脚本实例
- linux 5.4 shell脚本实例
- LinuxC/C++编程基础(13) shell脚本简单实例
- [shell应用进阶]:限制同时运行脚本实例的个数 -- 串行化:换一个思路。
- Unix/Linux Shell脚本入门实例[00原创]
- Shell脚本防攻击的方法与实例
- Shell 编程--本文结合大量实例阐述如何编写一个shell脚本。
- Shell 编程--本文结合大量实例阐述如何编写一个shell脚本
- 用shell脚本实现杨辉三角的4个实例!
- 方老师shell脚本实例系列讲座下载地址
- Shell操作数据库流控脚本实例
- 脚本shell中的tr 命令使用与实例
- shell-脚本集合2(monitor)
- shell脚本实例解析
- shell脚本实例 使用exit语句和if
- 本文结合大量实例阐述如何编写一个shell脚本
- shell 脚本判断文件类型实例
- Shell 编程--本文结合大量实例阐述如何编写一个shell脚本
- shell-脚本集合3