Linux练习(Part6.5:shell脚本编程)
目录
- 练习:用 for 实现
- 1、判断/var/目录下所有文件的类型
- 2、添加10个用户user1-user10,密码为8位随机字符
- 3、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
- 4、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
- 5、计算100以内所有能被3整除的整数之和
- 6、编写脚本,提示请输入网络地址,如100.0.0.0,判断输入的网段中主机在线状态
- 7、打印九九乘法表
- 8、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
- 9、打印等腰三角形
- 10、猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个。第二天早上又将剩下的桃子
练习:用 for 实现
1、判断/var/目录下所有文件的类型
[10:32:14 root@Centos8 /data/Mage]#cat var.sh
#!/bin/bash
#
#********************************************************************
#Author: chenchunyu
#QQ: 2504039287
#Date: 2021-01-26
#FileName: var.sh
#URL: https://www.cnblogs.com/chenchunyuBEyOND/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
DIR=/var
cd $DIR
for FILE in `ls` ;do
if [ -f $FILE ];then
echo "$FILE is Regular file"
elif [ -d $FILE ];then
echo "$FILE is Dir file"
elif [ -L $FILE ];then
echo "$FILE is Link file"
else
echo "$FILE is Other file"
fi
done
2、添加10个用户user1-user10,密码为8位随机字符
[11:28:54 root@Centos8 /data/Mage]#bash useradd10.sh
user1 is created successfully!
user2 is created successfully!
user3 is created successfully!
user4 is created successfully!
user5 is created successfully!
user6 is created successfully!
user7 is created successfully!
user8 is created successfully!
user9 is created successfully!
user10 is created successfully!
[11:29:02 root@Centos8 /data/Mage]#cat user.list
user1 cf3e5a72
user2 46240113
user3 b81c3937
user4 603be70e
user5 ea45fe54
user6 81a64896
user7 79205720
user8 b395247e
user9 ef426351
user10 4b52054e
[11:29:08 root@Centos8 /data/Mage]#cat useradd10.sh
#!/bin/bash
#
#********************************************************************
#Author: chenchunyu
#QQ: 2504039287
#Date: 2021-01-26
#FileName: useradd10.sh
#URL: https://www.cnblogs.com/chenchunyuBEyOND/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#添加10个用户user1-user10,密码为8位随机字符
for i in {1..10};do
USER=user$i
PASS=`echo $RANDOM|md5sum|cut -c 1-8`
useradd $USER &> /dev/null
echo $PASS | passwd --stdin $USER &> /dev/null
echo $USER $PASS >> /data/Mage/user.list
echo "$USER is created successfully!"
done
3、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
[02:37:02 root@Centos7 /data/Mage]#cat for_rc.sh
#!/bin/bash
#
#********************************************************************
#Author: chenchunyu
#QQ: 2504039287
#Date: 2021-01-26
#FileName: for_rc.sh
#URL: https://www.cnblogs.com/chenchunyuBEyOND/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#/etc/rc.d/rc3.d目录下分别有多个K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加 start,如K34filename stop S66filename start
for FILE in `ls /etc/rc.d/rc3.d/ |grep -E '^K|^S'`;do
if echo $FILE |grep '^K' &> /dev/null;then
echo "$FILE stop"
else
echo "$FILE start"
fi
done
[02:37:12 root@Centos7 /data/Mage]#bash for_rc.sh
K50netconsole stop
S10network start
4、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
[06:36:45 root@Centos7 /data/Mage]#cat sum.sh
#!/bin/bash
#
#********************************************************************
#Author: chenchunyu
#QQ: 2504039287
#Date: 2021-01-26
#FileName: sum.sh
#URL: https://www.cnblogs.com/chenchunyuBEyOND/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#提示输入正整数n的值,计算1+2+…+n的总和
read -p "请输入一个正整数n: " INT
for i in {1..$INT};do
echo "1+2+…+n的总和等于:`seq -s+ $INT |bc`"
done
[06:36:58 root@Centos7 /data/Mage]#bash sum.sh
请输入一个正整数n: 100
1+2+…+n的总和等于:5050
5、计算100以内所有能被3整除的整数之和
[06:53:36 root@Centos7 /data/Mage]#cat 100_3SUM.sh
#!/bin/bash
#
#********************************************************************
#Author: chenchunyu
#QQ: 2504039287
#Date: 2021-01-26
#FileName: 100_3SUM.sh
#URL: https://www.cnblogs.com/chenchunyuBEyOND/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#计算100以内所有能被3整除的整数之和
SUM=0
for i in {3..100..3};do
let SUM+=$i
done
echo $SUM
[06:56:08 root@Centos7 /data/Mage]#bash 100_3SUM.sh
1683
6、编写脚本,提示请输入网络地址,如100.0.0.0,判断输入的网段中主机在线状态
[06:58:15 root@Centos7 /data/Mage]#cat ping254.sh
#!/bin/bash
#
#********************************************************************
#Author: chenchunyu
#QQ: 2504039287
#Date: 2021-01-25
#FileName: ping254.sh
#URL: https://www.cnblogs.com/chenchunyuBEyOND/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#扫描一个网段:100.0.0.0/24(100.0.0.1---100.0.0.254),判断此网段中主机在线状态,将在线的主机的IP打印出来
NET=100.0.0
for ID in {1..254};do
{
ping -c3 -i0.2 -W2 $NET.$ID &> /dev/null && echo "$NET.$ID is up" || echo $NET.$ID is down
}&
done
wait
# {} 是把中间代码当做一段代码
# & 并行执行
# wait 并行执行完 , 不自动退出,等待下一步操作,wait 就退出了
7、打印九九乘法表
[16:24:26 root@Centos8 /data/Mage]#bash 99.sh
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
[16:24:32 root@Centos8 /data/Mage]#cat 99.sh
#!/bin/bash
#
#********************************************************************
#Author: chenchunyu
#QQ: 2504039287
#Date: 2021-01-25
#FileName: 99.sh
#URL: https://www.cnblogs.com/chenchunyuBEyOND/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#九九乘法表
#语法1
#for i in {1..9};do
# for j in `seq $i`;do
# echo -e "${j}x${i}=$[i*j]\t\c"
# done
# echo
#done
#语法2
for ((i=1;i<10;i++));do
for((j=1;j<=i;j++));do
echo -e "${j}x${i}=$((i*j))\t\c"
done
echo
done
8、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
[16:40:44 root@Centos8 /data/Mage]#cat for_testdir.sh
#!/bin/bash
#
#********************************************************************
#Author: chenchunyu
#QQ: 2504039287
#Date: 2021-01-26
#FileName: for_testdir.sh
#URL: https://www.cnblogs.com/chenchunyuBEyOND/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
cd /data/Mage/testdir
for i in {1..10};do
RAN=`tr -dc 'A-Za-z' </dev/urandom | head -c 8`
touch /data/Mage/testdir/${i}${RAN}.html
done
[16:41:13 root@Centos8 /data/Mage]#ll /data/Mage/testdir/
total 0
-rw-r--r--. 1 root root 0 Jan 26 16:40 10bKACWjnp.html
-rw-r--r--. 1 root root 0 Jan 26 16:40 1GoyhdKLG.html
-rw-r--r--. 1 root root 0 Jan 26 16:40 2JjvgFCvs.html
-rw-r--r--. 1 root root 0 Jan 26 16:40 3aCalJgiU.html
-rw-r--r--. 1 root root 0 Jan 26 16:40 4iHdEgTGC.html
-rw-r--r--. 1 root root 0 Jan 26 16:40 5YNBtOUKM.html
-rw-r--r--. 1 root root 0 Jan 26 16:40 6wIAMcSXh.html
-rw-r--r--. 1 root root 0 Jan 26 16:40 7xqCovZIF.html
-rw-r--r--. 1 root root 0 Jan 26 16:40 8HeRkOYgs.html
-rw-r--r--. 1 root root 0 Jan 26 16:40 9AMbXzNLc.html
9、打印等腰三角形
[16:42:42 root@Centos8 /data/Mage]#cat for_triangle.sh
#!/bin/bash
#
#********************************************************************
#Author: chenchunyu
#QQ: 2504039287
#Date: 2021-01-26
#FileName: for_triangle.sh
#URL: https://www.cnblogs.com/chenchunyuBEyOND/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#等腰三角形
read -p "请输入三角形的行数: " line
for((i=1;i<=line;i++));do
for((k=0;k<=line-i;k++));do
echo -e ' \c'
done
for((j=1;j<=2*i-1;j++));do
echo -e '*\c'
done
echo
done
[16:48:06 root@Centos8 /data/Mage]#bash for_triangle.sh
请输入三角形的行数: 10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
10、猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个。第二天早上又将剩下的桃子
吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只
剩下一个桃子了。求第一天共摘了多少?
[16:54:20 root@Centos8 /data/Mage]#cat monkey.sh
#!/bin/bash
#
#********************************************************************
#Author: chenchunyu
#QQ: 2504039287
#Date: 2021-01-26
#FileName: monkey.sh
#URL: https://www.cnblogs.com/chenchunyuBEyOND/
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子。求第一天一共摘了多少?
SUM=1
for i in {1..9};do
let SUM=(SUM+1)*2
done
echo "桃子的个数是: $SUM"
[17:07:26 root@Centos8 /data/Mage]#bash monkey.sh
桃子的个数是: 1534

浙公网安备 33010602011771号