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
时间: 2024-09-30 16:19:29