Shell脚本在文件和目录上添加扩展名

时间:2013-02-21 02:08:38

标签: shell

我需要帮助来编写一个脚本,该脚本将接收当前目录中的目录并浏览该目录。

如果找到,并且它是一个目录,脚本会添加以下扩展名:.aaa

但如果找到的文件是pdfzipmp3,则应添加以下扩展名:.bbb

我们假设这些文件还没有任何扩展名

实施例

如果找到目录hello,它应该将其重新保存为hello.aaa,如果找到pdf文件名myfile,则将其重新保存为myfile.pdf,

我不确定是否应该使用case ... in或其他内容:

#!/bin/sh
for dir in "$@"; do
    for file in "$dir"/*;
    do    
        if [[ -d $file ]]
        then
            ext=dir
        else
            file *
            if ???????? then ext=pdf; # am not sure how to set the condition so that if teh file found is pdf to add the extension PDF.
            else
                if ???????? ext=zip # same thing if teh file found is zip 
                else
                    if ?????? ext=mp3 # samething if the file found is mp3

    done
done

1 个答案:

答案 0 :(得分:0)

#!/bin/sh
for dir in "$@"; do
    for file in "$dir"/*; do
        # protect against empty dirs - the shell just passes a
        # literal asterisk along in this case
        case $file in
        "$dir/*")
             continue
             ;;
        esac
        if [ -d "$file" ]; then
            ext=aaa
            continue
        fi
        case $(file "$file") in
        "gzip compressed"*)
            ext=gzip
            ;;
        "whatever file(1) says for PDFs")
            ext=pdf
            ;;
        "MP3"*)
            ext=mp3
            ;;
        # et cetera
        esac
    done
done