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
写个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