shell习题

shell习题:每天记录一道题

1、需求:--批量更改文件

         找到/123目录下所有后缀名为.txt的文件

  1. 批量修改.txt为.txt.bak
  2. 把所有.bak文件打包压缩为123.tar.gz
  3. 批量还原文件的名字,即把增加的.bak再删除 
#!/bin/bash
##查找txt文件
find /123 -type f -name "*.txt" > /tmp/txt.list
##批量修改文件for f in `cat /tmp/txt.list`
do
    mv $f $f.bak
done
##创建一个目录,为了避免目录已经存在,所以要加一个复杂的后缀名
d=`date +%y%m%d%H%M%S`
mkdir /tmp/123_$d
##把.bak文件拷贝/tmp/123_$d
for f in `cat /tmp/txt.list`
do
    cp $f.bak /tmp/123_$d
done
##打包压缩
cd /tmp/
tar czf 123.tar.gz 123_$d/
##还原
for f in `cat /tmp/txt.list`
do
    mv $f.bak $f
done

 2、需求:--统计普通用户

 写个shell,看看你的Linux系统中是否有自定义用户(普通用户),若是有,一共有几个?并输出姓名!
假设所有普通用户都是uid大于1000的

#!/bin/bash
##找出含有普通用户的行并统计个数
n=`awk -F : $3>=1000 /etc/passwd|wc -l`
##判断普通用户数量
if [ $n -gt 0 ]
then
   ##输出又多少个普通用户
    echo "There are $n common users."

   ##找出普通用户的行并打印第一行(带用户名的行)
    uname=`awk -F : $3>=1000 /etc/passwd | awk -F : {print $1}`
    echo "the user were $uname ."
else
    echo "No common users."
fi

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...