初学shell,给大家分享我的一下我的练习题目,有不对或者写的不好的地方还请大家指出。
1.写脚本模拟Linux登录shell。具体为:
Login:(输入用户名user)
Password:(输入密码123456)
如果都输入正确就换行显示:“Both the account and password are right!Login Success!”如果输入错误就换行显示:“Input is error!Login Failed!”
参考代码
#!/bin/bash
#var Login shell
read -p "Login:" uname
read -p "Password:" ukey
if [ ${uname} = "TAC" -a ${ukey} = "123456" ]; then
echo "Both the account and passwork are right! Login Success! "
else
echo "Inout is error! Login Failed!"
fi
测试
Login:TAC
Password:123456
Both the account and passwork are right! Login Success!
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w9.sh
Login:TAC
Password:123457
Inout is error! Login Failed!
2.先查找指定目录中所有大于1M的文件,然后把找到的文件写到一个文本文件中,并统计这样的文件有多少个。(find,wc)
#!/bin/bash
#select file >= y M
read -p "Please input a path: " x
read -p "Input size: " y
sudo find $x -type f -size +"$y"M > w10_text.txt ##结果输出到当前目录
echo "There have `cat w10_text.txt |wc -l` file"
for a in `cat ./w10_text.txt`
do
echo "${a:20}"
done
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w10.sh
Please input a path: /home/tanaocheng/下载
Input size: 50
There have 2 file
netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb
baidunetdisk_3.5.0_amd64.deb
tanaocheng@tanaocheng-TUF:~/linuxwork$
大于50M的文件名输入到了当前目录下的w10_test.txt中
tanaocheng@tanaocheng-TUF:~/linuxwork$ cat w10_text.txt
/home/tanaocheng/下载/netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb
/home/tanaocheng/下载/baidunetdisk_3.5.0_amd64.deb
ls -lh查看“下载”文件夹中文件的实际大小
tanaocheng@tanaocheng-TUF:~/下载$ ls -lh
总用量 218M
drwxrwxr-x 3 tanaocheng tanaocheng 4.0K 11月 4 21:13 201611031102021
-r-------- 1 tanaocheng tanaocheng 107M 10月 31 16:06 baidunetdisk_3.5.0_amd64.deb
-r-------- 1 tanaocheng tanaocheng 12M 10月 31 14:33 linuxqq_2.0.0-b2-1089_amd64.deb
-r-------- 1 tanaocheng tanaocheng 100M 10月 31 15:37 netease-cloud-music_1.2.1_amd64_ubuntu_20190428.deb
3.先在指定目录下创建1000个文件,要求其中要有200个文件其文件名以abc开头,然后在这个目录下再找出100个以abc开头的文件,把其文件名写到其文件内容的第一行。最后再把这些文件的第一行保存到文件new中。
参考代码,只生成一百个文件,20个一abc开头文件
#!/bin/bash
#Creat 1000 , 200is abc
i=0
path=w11_testdir
filenew=new.txt
if [ -d $path ]
then
echo "$path" is aready Creadted""
else
mkdir $path
touch $filenew
while (($i<=100))
do
if (( i<=20 ));then
filename="abc"$i".txt"
touch $path/$filename #mkdir $path/$filename
else
filename="TAC"$i".txt"
touch $path/$filename #mkdir $path/$filename
fi
let i=$i+1
done
for fn in `find $w11_testdir -type f -name "abc*"|head -n 100`
do
fn_t="${fn:14}" ##截取出文件名字段
echo "$fn_t" > $fn ##将字段写入对于的txt中
echo "`head -n 1 $fn`" >> $filenew ##将abc开头的文件名追加输入到new.txt中
echo "$fn" ##打印出操作对象
done
fi
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w11.sh
./w11_testdir/abc4.txt
./w11_testdir/abc2.txt
./w11_testdir/abc20.txt
./w11_testdir/abc8.txt
./w11_testdir/abc19.txt
./w11_testdir/abc6.txt
./w11_testdir/abc1.txt
./w11_testdir/abc0.txt
./w11_testdir/abc10.txt
./w11_testdir/abc17.txt
./w11_testdir/abc3.txt
./w11_testdir/abc13.txt
./w11_testdir/abc5.txt
./w11_testdir/abc9.txt
./w11_testdir/abc14.txt
./w11_testdir/abc18.txt
./w11_testdir/abc7.txt
./w11_testdir/abc15.txt
./w11_testdir/abc12.txt
./w11_testdir/abc16.txt
./w11_testdir/abc11.txt
查看在当前目录新下生成的new.txt与目录w11_testdir
生成的所有文件
tanaocheng@tanaocheng-TUF:~/linuxwork$ ls ./w11_testdir
abc0.txt abc15.txt abc20.txt abc7.txt TAC23.txt TAC29.txt TAC35.txt TAC41.txt TAC47.txt TAC53.txt TAC59.txt TAC65.txt TAC71.txt TAC77.txt TAC83.txt TAC89.txt TAC95.txt
abc10.txt abc16.txt abc2.txt abc8.txt TAC24.txt TAC30.txt TAC36.txt TAC42.txt TAC48.txt TAC54.txt TAC60.txt TAC66.txt TAC72.txt TAC78.txt TAC84.txt TAC90.txt TAC96.txt
abc11.txt abc17.txt abc3.txt abc9.txt TAC25.txt TAC31.txt TAC37.txt TAC43.txt TAC49.txt TAC55.txt TAC61.txt TAC67.txt TAC73.txt TAC79.txt TAC85.txt TAC91.txt TAC97.txt
abc12.txt abc18.txt abc4.txt TAC100.txt TAC26.txt TAC32.txt TAC38.txt TAC44.txt TAC50.txt TAC56.txt TAC62.txt TAC68.txt TAC74.txt TAC80.txt TAC86.txt TAC92.txt TAC98.txt
abc13.txt abc19.txt abc5.txt TAC21.txt TAC27.txt TAC33.txt TAC39.txt TAC45.txt TAC51.txt TAC57.txt TAC63.txt TAC69.txt TAC75.txt TAC81.txt TAC87.txt TAC93.txt TAC99.txt
abc14.txt abc1.txt abc6.txt TAC22.txt TAC28.txt TAC34.txt TAC40.txt TAC46.txt TAC52.txt TAC58.txt TAC64.txt TAC70.txt TAC76.txt TAC82.txt TAC88.txt TAC94.txt
查看new.txt内容
tanaocheng@tanaocheng-TUF:~/linuxwork$ cat new.txt
abc4.txt
abc2.txt
abc20.txt
abc8.txt
abc19.txt
abc6.txt
abc1.txt
abc0.txt
abc10.txt
abc17.txt
abc3.txt
abc13.txt
abc5.txt
abc9.txt
abc14.txt
abc18.txt
abc7.txt
abc15.txt
abc12.txt
abc16.txt
abc11.txt
查看其中一个文件的内容
tanaocheng@tanaocheng-TUF:~/linuxwork$ cat w11_testdir/abc16.txt
abc16.txt
4.先在当前目录下建立两个文件,名称分别为a和b,接着在两个文件中分别录入一些文本内容,然后再把文件b中有的,但是文件a中没有的所有行,保存为文件c,并统计c的行数。
参考代码
#!/bin/bash
#Realization fileb have but filea didnot linex
path=test_12
filea=a.txt
fileb=b.txt
filec=c.txt
if [ ! -d $path ];
then
mkdir ./$path ##在但前目录下新建文件夹进行测试
touch $path/$filea
touch $path/$fileb
let i=0
while ((i<=10)) ##写入内容,
do
if [ `expr $i \% 2` -eq 0 ];
then
echo "TAC+"$i"" >> $path/$filea
fi
echo "TAC+"$i"" >> $path/$fileb
let i+=1
done
diff $path/$filea $path/$fileb |grep \> >> $path/$filec
else
echo "File is already existed" ##这段代码用于调试
x="y"
read -p "Do you want to remove old file[y/n]" x
if [ $x = "y" ];then
rm -rf $path
fi
fi
if [ "$x"x != "y"x ];then
echo "This is a.txt:"
cat $path/$filea
echo "That is b.txt:"
cat $path/$fileb
echo "And this is Realization fileb have buf filea didnot for c.txt:"
echo "Have lines:`cat $path/$filec |wc -l`"
cat $path/$filec
fi
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w12.sh
File is already existed
Do you want to remove old file[y/n]y
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w12.sh
This is a.txt:
TAC+0
TAC+2
TAC+4
TAC+6
TAC+8
TAC+10
That is b.txt:
TAC+0
TAC+1
TAC+2
TAC+3
TAC+4
TAC+5
TAC+6
TAC+7
TAC+8
TAC+9
TAC+10
And this is Realization fileb have buf filea didnot for c.txt:
Have lines:5
> TAC+1
> TAC+3
> TAC+5
> TAC+7
> TAC+9
tanaocheng@tanaocheng-TUF:~/linuxwork$
5.判断一文件是不是块或字符设备文件,如果是将其拷贝到 /dev 目录下。
参考代码
#!/bin/bash
#Judge a file is chardev or blockdev,blockdev copy to /dev
path=" "
file=" "
read -p "Please input File and diretory " path file
if [ -c $path/$file ];then
echo "This is a Character file"
cp -p $path/$file /dev
elif [ -b $path/$file ];then
cp -p $path/$file /dev
echo "This ia a block file"
else
echo "cp failed"
fi
6.新建一个目录,然后在这个目录下建立100个后缀名为.sh的脚本文件,接着把这个目录下的所有脚本文件的后缀名全部改为bat,再以时间为文件名压缩打包存放到一个新建立的目录。
参考代码
#!/bin/bash
#1.create a new file
#2.make 100 shall file
#3.modify the shall to .bat,and named by date
path1="w14_test1"
path2="w14_test2"
tarname="w14`date +%Y%m%d%H%M%S`"
#read -p "Please input two file path :" path1 path2
if [ -d $path1 ];
then
echo "file path is already existed"
read -p "Do you want to rmove the old one?[y/n]" x
if [ $x = "y" ];then
rm -rf $path1
rm -fr $path2
rm w14*.tar.gz
fi
else
mkdir $path1
let i=0
while (( $i<=10 ))
do
touch $path1/$i.sh
let i+=1
done
cp -r $path1 $path2
let i=0
while (( $i<=10 ))
do
mv $path2/$i.sh $path2/$i.bat
let i+=1
done
tar -czf $tarname.tar.gz $path2
echo "Creat sucsess:"
echo "path1:"
ls $path1
echo "path2:"
ls $path2
echo "tar:"
ls ./*.tar.gz
fi
~
测试
Creat sucsess:
path1:
0.sh 10.sh 1.sh 2.sh 3.sh 4.sh 5.sh 6.sh 7.sh 8.sh 9.sh
path2:
0.bat 10.bat 1.bat 2.bat 3.bat 4.bat 5.bat 6.bat 7.bat 8.bat 9.bat
tar:
./w1420211115093719.tar.gz
查看当前目录
tanaocheng@tanaocheng-TUF:~/linuxwork$ ls
new.txt w10.sh w11.sh w12.sh w1420211115093719.tar.gz w14_test1 w15.sh w17.sh w1.sh w3.sh w5.sh w7.sh w9.sh
test_12 w10_text.txt w11_testdir w13.sh w14.sh w14_test2 w16.sh w18.sh w2.sh w4.sh w6.sh w8.sh
7.添加一个新组为class1,然后添加属于这个组的50个用户,用户名的形式为stdxx,其中xx从01到50。接下来实现自动删除这50个用户账号中偶数账号的功能,即账号名为std02,std04,std06,std08…stud50都要被删除。
参考代码
#!/bin/bash
#Create a user group name stdXX(01-50),and delete odd one
newgroup="class1"
let members=10
if [ `cat /etc/group |grep class1 |wc -l` -eq 0 ];then
sudo groupadd class1
echo "You create:"
for a in `seq -s ' ' -w 1 10`
do
sudo useradd -G class1 "std"$a""
echo "std"$a""
done
echo "In group $newgroup"
read -p "Please make a choice->
1.Del the even number user
2.Del the singular number user
~.Do nothing
:" y
echo -n "Kepp:"
for b in `seq -s ' ' -w 1 10`
do
numb=`expr ${b:0:1} \* 10 + ${b:1:1}`
if [ $y = "1" ];then
if [ `expr $numb % 2` -eq 0 ];then
sudo userdel "std"$b""
else
echo -n "std"$b" "
fi
elif [ $y = "2" ];then
if [ `expr $numb % 2` -ne 0 ];then
sudo userdel "std"$b""
else
echo -n "std"$b" "
fi
else
echo -n "std"$b" "
fi
done
echo ""
else
echo "The group is aready exist"
read -p "Do you want remove all the newgroup and newuser[y/~] " x
if [ $x = "y" ];then
for a in `seq -s ' ' -w 1 10`
do
if [ `cat /etc/passwd |grep "std"$a"" |wc -l` -ne 0 ];then
sudo userdel "std"$a""
fi
done
sudo groupdel class1
echo "Deleted:group class1" ##
echo "Deleted:user std01-std10"
fi
fi
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w15.sh
The group is aready exist
Do you want remove all the newgroup and newuser[y/~] y
Deleted:group class1
Deleted:user std01-std10
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w15.sh
You create:
std01
std02
std03
std04
std05
std06
std07
std08
std09
std10
In group class1
Please make a choice->
1.Del the even number user
2.Del the singular number user
~.Do nothing
:2
Kepp:std02 std04 std06 std08 std10
tanaocheng@tanaocheng-TUF:~/linuxwork$
这次运行选择保留了偶数结尾的用户,查看/etc/passwd验证结果
tanaocheng@tanaocheng-TUF:~/linuxwork$ cat /etc/passwd |tail -8
sssd:x:126:131:SSSD system user,,,:/var/lib/sss:/usr/sbin/nologin
tanaocheng:x:1000:1000:TanAocheng,,,:/home/tanaocheng:/bin/bash
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
std02:x:1002:1003::/home/std02:/bin/sh
std04:x:1004:1005::/home/std04:/bin/sh
std06:x:1006:1007::/home/std06:/bin/sh
std08:x:1008:1009::/home/std08:/bin/sh
std10:x:1010:1011::/home/std10:/bin/sh
tanaocheng@tanaocheng-TUF:~/linuxwork$
8.设计shell程序,在每月第一天压缩并备份/etc目录的所有内容,存放在/root/bak目录里,且文件名以年月日_etc命名,shell程序存放到/usr/bin目录下。
参考代码
#!/bin/bash
#Scheduled backup diretory /etc to /root/bak,and name yymmdd
Source_dir=/etc
Target_dir=/root/bak
Bake_name=$[date +%Y%m%d]_etc.tar.gz
[ -d ${Target_dir} ]||[ mkdir ${Target_dir} ]&&echo "Create ${Target_dir}"
cd $Target_dir $$ tar czvf $Bake_name $Source_dir > ${Target_dir} && echo "Back up success"
tanaocheng@tanaocheng-TUF:~/linuxwork$ crontab -e
增加一条定时任务,保存后退出
0 0 1 * * /bin/sh /home/tanaocheng/linuxwork/w16.sh
设置一个定时任务的详细讲解可以参考这篇博客,写地很清楚
使用shell脚本进行每月定时备份数据_Java鱼仔的博客-CSDN博客
出现no crontab for xxx提示的可以参考这篇博客
9.编写shell,根据Free命令计算内存占用率。
我安装时ubuntu分配的是8G内存,查看某时刻机器内存占用率
参考代码
#!/bin/bash
#According to a command result Free Calculate memory usage
free -m > w17test.txt
str=`cat -n w17test.txt |head -n 2 |tail -n 1`
let i=0
for a in $str
do
if [ $i -eq 2 ];then
all=$a
elif [ $i -eq 3 ];then
use=$a
elif [ $i -eq 7 ];then
nuse=$a
fi
let i+=1
done
let data1=$all
let data2=$use
#1
#data3=`echo "scale=2;$data2/$data1*100"|bc`
#2
data3=`echo $data2 $data1|awk '{printf "%.2f",$1/$2*100}'`
echo "availability:${nuse}MB,total:${all}MB. Use rate:$data3%"
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w17.sh
availability:4910MB,total:7810MB. Use rate:26.75%
10.编写shell,根据IP地址列表检查主机状态。
参考代码,假设ip列表是百度与C占
#!/bin/bash
#Check the condition of the host according to Ip address
#echo "`ifconfig`"
##test
testfile=w18_iplist.txt
let i=1
if [ ! -f $testfile ];then
touch $testfile
echo "www.baidu.com" &> $testfile ##host1
echo "www.csdn.net" &>> $testfile ##host2
fi
for host in `cat $testfile`
do
#echo $host
ping -c 2 $host &> /dev/null
if [ $? -eq 0 ]
then
echo "host$i is ok"
else
echo "host$i is down"
fi
let i+=1
done
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w18.sh
host1 is ok
host2 is ok