bash脚本练习

1.创建三个用户,并计算三个uid之和
bash脚本练习_第1张图片
2.判断用户不存在时,则创建用户
bash脚本练习_第2张图片
3.通过命令行参数给定两个数字,输出其中较大的数值
通过命令行参数给定一个用户名,判断其ID号是偶数还是奇数bash脚本练习_第3张图片
4.通过命令行参数给定一个用户名,判断其ID号是偶数还是奇数
bash脚本练习_第4张图片
5.通过命令行参数给定两个文本文件名,如果某文件不存在,则结束脚本执行;都存在时返回每个文件的行数,并说明其中行数较多的文件
bash脚本练习_第5张图片
6.如果当前主机名为空,或者为yuki,则将其修改为kasumi
bash脚本练习_第6张图片
7.写一脚本,通过命令传递两个文本文件路径给脚本,计算其空白行数之和
bash脚本练习_第7张图片
8.写一个脚本,完成如下功能:
(1) 列出当前系统识别到的所有磁盘设备;
(2) 如磁盘数量为1,则显示其空间使用信息;
否则,则显示最后一个磁盘上的空间使用信息
bash脚本练习_第8张图片
9.写一个脚本,交互模式,创建一个用户。若read模式没有输出,则显示需要一个用户后退出。否则,read模式用户输入密码,若没有输出,密码默认为passwd。若read模式输入的用户存在,则更改密码为$passwd后退出。否则创建新用户,更改其密码$passwd,输出已创建完成。
bash脚本练习_第9张图片
10.写一个脚本,交互模式,用户输入一个设备文件,若没有输入,显示错误。输入后,若在fdisk -l中找到此设备文件,则列出该设备相关信息。否则,显示无此设备文件。
bash脚本练习_第10张图片

11.脚本参数传递一个文件路径给脚本,判断此文件的类型

#!/bin/bash

if ! [ $# -eq 1 ];then
	echo "at least one file path" && exit 2
fi

if ! [ -e $1 ]; then
	echo "no such file" && exit 2
fi

if [ -f $1 ];then
	echo "this is common file"
elif [ -d $1 ];then
	echo "this is dirctor file"
elif [ -b $1 ];then
	echo "this is blocak file"
elif [ -c $1 ];then
	echo "thiis character file"
elif [ -L $1 ];then
	echo "this is symbolic link file"
elif [ -p $1 ];then
	echo "this is pipe file"
elif [ -S $1 ];then
	echo "this is socket file"
else
	echo "this is unknow file"
fi

12.写一个脚本:
(1) 传递一个参数给脚本,此参数为用户名;
(2) 根据其ID号来判断用户类型:
0: 管理员
1-999:系统用户
1000+:登录用户

#/bin/bash

if ! [ $# -eq 1 ];then
	echo "at least one user" && exit 2
fi

! id $1 &> /dev/null && echo "this is user no exitst" && exit 2

userid=$(id -u $1) 

if [ $userid -eq 0 ] ;then
	echo "this is root user"
elif [ $userid -ge 1000 ];then
	echo "this is common user"
else
	echo "this is system user"
fi

13.写一个脚本:
(1) 列出如下菜单给用户:
disk) show disks info;
mem) show memory info;
cpu) show cpu info;
*) quit;
(2) 提示用户给出自己的选择,而后显示对应其选择的相应系统信息;

#!/bin/bash


cat << EOF
disk) show disks info

mem) show memory info

cpu) show cpu info

*)quit
EOF

read -p "pleas input your option: " choice

if [ "$choice" = "disk" ];then
	fdisk -l /dev/sd[a-z]
elif [ "$choice" = "mem" ];then
	free -m
elif [ "$choice" = cpu ];then
	lscpu
else
	echo "unknow type"
	exit 2
fi

14.求100以内所有正整数之和

#!/bin/bash

declare -i sum=0

for integer in {1..100};do
 #	echo "\$sum is $sum, \$integer is $integer"
	sum=$[$sum+$integer]
done

echo "total $sum"

15.判断/var/log目录下的每一个文件的内容类型

#!/bin/bash

for filename in /var/log/*;do
	if [ -f $1 ];then
		echo "this is common file"
	elif [ -d $1 ];then
		echo "this is dirctor file"
	elif [ -b $1 ];then
		echo "this is blocak file"
	elif [ -c $1 ];then
		echo "thiis character file"
	elif [ -L $1 ];then
		echo "this is symbolic link file"
	elif [ -p $1 ];then
		echo "this is pipe file"
	elif [ -S $1 ];then
		echo "this is socket file"
	else
		echo "this is unknow file"
	fi
done

16.分别求100以内所有偶数之和,以及所有奇数之和

#!/bin/bash
declare -i sum=0 

for odd in $(seq 1 2 100);do
	sum=$[$sum+$odd]
done

echo "odd sum: $sum"


declare -i sumeven=0

for even in $(seq 2 2 100);do
	sumeven=$[$sumeven+$even]
done

echo "even sum: $sumeven"

17.计算当前系统上的所有用的id之和

#!/bin/bash

declare -i sum=0

for id in $(cut -d":" -f3 /etc/passwd);do
	sum=$[$sum+$id]
done

echo "idsum: $sum"

18.通过脚本参数传递一个目录给脚本,而后计算此目录下所有文本文件的行数之和;并说明此类文件的总数;

#!/bin/bash

if ! [ $# -eq 1 ];then
	echo "at least one dirname" && exit 2
fi

if ! [ -d $1 ];then
	echo "this is not dir" && exit 2
fi



declare -i sum=0
count=0

for filename in $(ls $1);do
	if [ -f $1/$filename ];then
		sum=$[$sum+$(wc -l $1/$filename | cut -d" " -f1)]
		count=$[$count+1]
	fi
done

echo "this is dir all file total line: $sum,  total $count file"

19.创建10个用户,user101-user110;密码同用户名

使用for循环
 1 #!/bin/bash
  2 
  3 id user101 &> /dev/null && echo "user exits" && exit 2
  4 
  5 for i in {101..110};do
  6     user=user$i
  7     useradd $user &> /dev/null
  8     echo "$user" | passwd --stdin $user &> /dev/null
  9     echo "user:$user already create,passwd:$user"
 10 done
使用while循环
 1 #!/bin/bash
  2 
  3 id user101 &> /dev/null && echo "the user exits" && exit 2
  4 
  5 declare -i i=101
  6 while [ $i -le 110 ];do
  7     user=user$i
  8     useradd $user &> /dev/null
  9     echo "$user" | passwd --stdin $user &> /dev/null
 10     echo "$user already create;passwd:$user"
 11     let i+=1
 12 done
使用until循环
#!/bin/bash
 15 
 16 id user101 &> /dev/null && echo "the user exits" && exit 2
 17 
 18 declare -i i=101
 19 until [ $i -gt 110 ];do
 20     user=user$i
 21     useradd $user &> /dev/null
 22     echo "$user" | passwd --stdin $user &> /dev/null
 23     echo "$user already create;passwd:$user"
 24     let i+=1
 25 done

20.分别求100以内所有偶数之和,100以内所奇数之和:

for循环
 1 #!/bin/bash
  2 
  3 declare -i sum=0
  4 
  5 for i in $(seq 2 2 100);do
  6     sum=$[$sum+$i]
  7 done
  8 
  9 
 10 declare -i sum1=0
 11 
 12 for j in $(seq 1 2 100);do
 13     sum1=$[$sum1+$j]
 14 done
 15 
 16 echo "Even numbers sum:$sum"
 17 echo "Odd  numbers sum:$sum1"
while循环
 1 #!/bin/bash
  2 
  3 declare -i i=2
  4 declare -i is=0
  5 
  6 while [ $i -le 100 ];do
  7     is=$[$is+$i]
  8     let i+=2
  9 done
 10 
 11 declare -i j=1
 12 declare -i js=0
 13 
 14 while [ $j -le 100 ];do
 15     js=$[$js+$j]
 16     let j+=2
 17 done
 18 
 19 echo "Even numbers sum:$is"
 20 echo "Odd  numbers sum:$js"

ps:until循环只需将对应条件改为:[ $i -gt 100 ]即可

21.打印九九乘法表;

for循环
 1 #!/bin/bash
  2 
  3 for j in {1..9};do
  4     for i in $(seq 1 $j);do
  5         echo -n -e "$i*$j=$[$i*$j]\t"
  6     done
  7     echo " "
  8 done
while循环
1 #!/bin/bash
  2 
  3 declare -i i=1
  4 declare -i j=1
  5 
  6 while [ $j -le 9 ];do
  7     while [ $i -le $j ];do
  8         echo -n -e "$i*$j=$[$i*$j]\t"
  9         let i+=1
 10     done
 11     let i=1
 12     let j+=1
 13     echo " "
 14 done
until循环
18 declare -i i=1
 19 declare -i j=1
 20 
 21 until [ $j -gt 9 ];do
 22     until [ $i -gt $j ];do
 23         echo -n -e "$i*$j=$[$i*$j]\t"
 24         let i+=1
 25     done
 26     let i=1
 27     let j+=1
 28     echo " "
 29 done
最终效果
[root@kasumi bashtest]# bash  99while.sh
1*1=1	 
1*2=2	2*2=4	 
1*3=3	2*3=6	3*3=9	 
1*4=4	2*4=8	3*4=12	4*4=16	 
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	 
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	 
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	 
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	 
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	 

22.打印逆序的九九乘法表:

for循环
 1 #!/bin/bash
  2 
  3 for j in {9..1};do
  4     for i in $(seq 1 $j);do
  5         echo -n -e "$i*$j=$[$i*$j]\t"
  6     done
  7     echo " "
  8 done
while循环
1 #!/bin/bash
2 
3 declare -i i=1
4 declare -i j=9
5 
6 while [ $j -gt 0 ];do
7     while [ $i -le $j ];do
8         echo -n -e "$i*$j=$[$i*$j]\t"
9         let i+=1
10     done
11     let i=1
12     let j-=1
13     echo " "
14 done
until循环
 1 #!/bin/bash
 2 
 3 declare -i i=1
 4 declare -i j=9
 5 
 6 until [ $j -le 0 ];do
 7     until [ $i -gt $j ];do
 8         echo -n -e "$i*$j=$[$i*$j]\t"
 9         let i+=1
10     done
11     let i=1
12     let j-=1
13     echo " "
14 done
最终效果
[root@kasumi bashtest]# bash 99xixuwhile.sh
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	 
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	 
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	 
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	 
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	 
1*4=4	2*4=8	3*4=12	4*4=16	 
1*3=3	2*3=6	3*3=9	 
1*2=2	2*2=4	 
1*1=1	 

你可能感兴趣的:(linux基础学习)