要求如下:
写一段完整的shell脚本,完成如下功能:1 查找出/home/目录下的修改时间最新的文件并把这个文件名保存在/usr/temp/temp.txt文件中;2 把/home/目录下的名称为1. tar.gz文件(假定已经存在)copy到/usr/temp目录中并解压;3 然后删除/home/目录下的2.txt文件(假定已经存在)。
脚本如下:
#!/bin/bash
cd /home/
ls -t|head -1>/usr/temp/temp.txt
cp /home/1. tar.gz /usr/temp/
cd /usr/temp
tar xvfz 1.tar.gz
yes|rm /home/aa/2.txt
还有一个方法:
#!/bin/bash
cd /home/
$1=``find . -type f -exec stat -c "%y %s %n" {} \;|sort -r|head -1|awk '{print $5}'|awk -F / '{print $2}'
echo $1 > /usr/temp/temp.txt
cp /home/1. tar.gz /usr/temp/
cd /usr/temp
tar xvfz 1.tar.gz
yes|rm /home/2.txt
 
 在此感谢
广州-木易水瓶(407298685)提出的ls -t|head -1>/usr/temp/temp.txt
{广州}小丑鱼(422208864)得出的-t     sort by modification time

这个比我自己写的简单多了