2016-10-10 76 views
0

我有以下代码:shell脚本-s和-Sd代码工作不正常

#Option -s for each set of files with the same name, presents sorted by size# 

elif [ $1 = "-S" ] 
then 
    shift 
    for i in $* 
    do 
     find $i -type f -print|while read F1 
      do 

       basename "${F1}" 
      done | sort | uniq -d | while read F2 
      do 
        find $i -type f -name "${F2}" -exec ls -l -S -R '{}' \; 
      done   
     done 

#Option -d performs sorting in descending order. 

elif [ $1 = "-d" ] 
then 
    shift 
    for i in $* 
    do 
     find $i -type f -print|while read F1 
      do 

       basename "${F1}" 
      done | sort | uniq -d | while read F2 
      do 
        find $i -type f -name "${F2}" -exec ls -l '{}' \; 
      done   
     done 

我应该按大小用命令-S,然后与-Sd递减顺序打印出来。我的输出似乎没有这样做。在这一点上,我无法弄清楚我的代码有什么问题。

-rw-r--r-- 1 quetzal quetzal 33 Out 7 20:27 aa/cc/t1 
-rw-r--r-- 1 quetzal quetzal 8 Out 7 20:36 aa/t1 
-rw-r--r-- 1 quetzal quetzal 0 Out 8 08:16 aa/cc/t2 
-rw-r--r-- 1 quetzal quetzal 0 Out 8 08:15 aa/t2 
-rw-r--r-- 1 quetzal quetzal 16 Out 8 08:16 aa/cc/t4 
-rw-r--r-- 1 quetzal quetzal 0 Out 10 12:53 aa/dd/t4 
+0

你能描述 “不工作” 更详细?什么是一些预期的产出,什么是实际产出?见[mcve]。 –

+0

当你使用命令-S它应该输出这样的东西 -rw-r - r-- 1 quetzal quetzal 8 out 7 20:36 aa/t1 -rw-r - r-- 1 quetzal quetzal 33 Out 7 20:27 aa/cc/t1 -rw-r - r-- 1 quetzal quetzal 0 Out 8 08:16 aa/cc/t2 -rw-r - r-- 1 quetzal quetzal 0 Out 8 08:15 aa/t2 -rw-r - r-- 1 quetzal quetzal 0 Out 10 12:53 aa/dd/t4 -rw-r - r-- 1 quetzal quetzal 16 Out 8 08:16 aa/cc/t4 当使用-Sd时,它应该按降序输出文件 –

回答

0

反转排序是这样的:

elif [ $1 = "-d" ] 
then 
shift 
for i in $* 
do 
    find $i -type f -print|while read F1 
     do 

      basename "${F1}" 
     done | sort -r | uniq -d | while read F2 
     do 
       find $i -type f -name "${F2}" -exec ls -l '{}' \; 
     done   
    done