shell习题
发布时间:2020-12-16 01:39:06 所属栏目:安全 来源:网络整理
导读:shell习题:每天记录一道题 1、需求:--批量更改文件名 ? ? ? ? ?找到/123目录下所有后缀名为.txt的文件 批量修改.txt为.txt.bak 把所有.bak文件打包压缩为123.tar.gz 批量还原文件的名字,即把增加的.bak再删除? #!/bin/ bash##查找txt文件 find / 123 -typ
shell习题:每天记录一道题 1、需求:--批量更改文件名 ? ? ? ? ?找到/123目录下所有后缀名为.txt的文件
#!/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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |