Always keep a beginner's mind, don'|

C0rr3ct

园龄:1年粉丝:0关注:7

shell练习

几道Linux shell脚本题目

0x01

编写shell脚本将当前目录下大于200字节的文件复制到/tmp目录下

#! /bin/bash
file=`find ./ -maxdepth 1 -type f -size +200c`
for i in $file
do
cp $i /tmp
echo '#######success######'
done

0x02

编写shell脚本获取本机的网络地址

#! /bin/bash
# 网卡不一样,命令也会不一样
ip=`ifconfig eth0 | grep "inet " | awk -F '[ ]+' '{print $3"/"$5}'`
echo $ip

结果输出

192.168.0.107/255.255.255.0

0x03

用shell编程,判断一文件是不是字符设备文件,如果是将其复制到/dev目录下

#! /bin/bash
read -p "Input filename:" filename
[ -z $filename ] && echo "no input" && exit 1
[ ! -f $filename ] && echo "file not exists" && exit 1
[ -c $filename ] && cp $filename /dev && echo "done"

0x04

设计一个shell程序,备份并压缩/etc目录的所有内容,存放在/root/bāk目录里,且文件名为如下形式
yymmdd_etc,yy为年,mm为月,dd为日。

#! /bin/bash
dir=/etc
[ ! -d $dir ] && echo "dir not exist" && exit 1
tar zcvf `date +%Y%m%d`_etc.tar.gz $dir
[ $? -eq 0 ] && mv `date +%Y%m%d`_etc.tar.gz /root/bak

0x05

设计一个shell程序,建立10个目录,并设置权限,其中其他用户的权限为:读;文件所有者权限为:读写执行;文件所有者所在组权限为:读执行

#! /bin/bash
for i in `seq 10`
do
mkdir user$i
chmod 754 user$i
done

0x06

依次向每个用户打招呼,并说出对方的UID是什么,并统计用户个数

例如:hello root ,your UID is 0

cat /etc/passwd | awk -F':' '{print "hello,"$1",your uid is "$3}''END{print NR}'

或者

#! /bin/bash
cat /etc/passwd |
while read line
do
user=`echo $line | awk -F':' '{print $1}'`
uid=`echo line | awk -F':' '{print $3}'`
echo "hello $user,your uid is $uid"
done
echo `cat /etc/passwd|wc -l`

0x07

写一个脚本
1.换工作目录至/var
2.依次向/var目录中的每个文件或子目录问好,形如:

提示:for FILE in/var/*;或for FILE in`ls/var`;

Hello,log
3.统计/var目录下共有多个文件,并显示出来

#! /bin/bash
for i in ./*
do
echo "hello $i"
done
sum=`ls|wc -l`
echo "sum of file:$sum"

0x08

写一个脚本
使用循环读取文件第2,4,6,8行,并显示其内容打印到屏幕
把这些行保存至/tmp/4.txt文件中

#! /bin/bash
for i in 2 4 6 8
do
line=`cat 10.txt | head -$i | tail -1`
echo $line
echo $line >> /tmp/4.txt
done

0x09

写一个脚本
传递两个整数给脚本,让脚本分别计算并显示这两个整数的和,差,积,商

#! /bin/bash
read -p "num1:" num1
read -p "num2:" num2
echo "================="
#echo $num1 $num2
echo $(($num1+$num2))
echo $(($num1-$num2))
echo $(($num1*$num2))
echo $(($num1/$num2))

或者

#! /bin/bash
num1=$1
num2=$2
echo "================="
#echo $num1 $num2
echo $(($num1+$num2))
echo $(($num1-$num2))
echo $(($num1*$num2))
echo $(($num1/$num2))

0x10

写一个脚本
1.显示当前系统日期和时间,而后创建目录/tmp/1 stest
2.切换工作目录至/tmp/1stest
3.创建目录a1d,b56e,6test
4.创建空文件xy,×2y,732
5.列出当前目录下以a,x或者6开头的文件或目录
6.列出当前目录下以字母开头,后跟个任意数字,而后跟任意长度字符的文件或目录

#!/bin/bash
date
mkdir -pv /tmp/lstest
cd /tmp/lstest
mkdir ald b56e 6test
touch xy x2y 732
ls [ax6]*
ls [a-zA-Z][0-9]*

本文作者:C0rr3ct

本文链接:https://www.cnblogs.com/C0rr3ct/p/18030184

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   C0rr3ct  阅读(12)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起