码迷,mamicode.com
首页 > 系统相关 > 详细

shell练习

时间:2018-06-04 17:44:33      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:shell

1、查询当前目录下大于1M的文件


ll -h


find ./ -maxdepth 1 -type f -size +1M

####################################################


#!/bin/bash


cd /shell


for i in `find ./ -maxdepth 1 -type f -size +1M`

do

        echo "$i"

done



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

比如 本机的ip 192.168.137.134 255.255.255.0


ifconfig eno16777736 |sed -n '2p'|awk -F' ' '{print $2,$4}'

[root@localhost shell]# ifconfig eno16777736 |sed -n '2p'|awk -F'[ ]+' '{print $3"/" 


$5}'

192.168.137.134/255.255.255.0




3、创建10个用户和密码



#!/bin/bash


for i in `seq 10`

do

        useradd -s /sbin/nologin -M SB$i >/dev/null 2>&1

        echo "123456" |passwd SB$i --stdin >/dev/null 2>&1

done


4、打包


[root@localhost yang]# tar -zcvf /yang/etcpasswd.tar.gz /etc/passwd

tar: Removing leading `/' from member names

/etc/passwd (出现这个提示加P)

[root@localhost yang]# ls

etcpasswd.tar.gz      mysql-5.6.38.tar.gz  pcre-8.10.zip.1

httpd-2.4.29.tar.bz2  pcre-8.10.zip


或者


tar -zcvf /yang/etcpasswd`date +%F`.tar.gz /etc/passwd





5、设计一个脚本,在每月第一天备份并压缩/etc/目录下的所有内容,存放在/root/bak目录里,且文


件名为如下形式yymmdd_etc 


[root@localhost shell]# cat tar1.sh 

#!/bin/bash

dir=/yang

if [ !-d $dir ];then 

mkdir /$dir

fi


tar -zcPf `date +%y%m%d`_etc.tar.gz /etc/passwd

echo $?


[$? -eq 0 ] && cp `date +%y%m%d`_etc.tar.gz $dir



6、hello,root,your UID is 0

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


7、写一个脚本 

a.设定变量file的值为/etc/passwd

b.依次向/etc/passwd中的每个用户问好,并且说出对方的UID是什么,例如:Hello,root,your UID 


is 0

c.统计一共有多少个用户



[root@localhost shell]# cat  passwd.sh

#!/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

sum=`cat /etc/passwd|wc -l`

echo "sum is users:$sum"


shell练习

标签:shell

原文地址:http://blog.51cto.com/4259679/2124667

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!