Shell script to add text at the end of Directory a

2019-06-12 15:08发布

问题:

I need some help with writing a Bash script. I have tried a lot but can't figure out how to finish it and I am new to programming.

I want to create a script that will take a parameter that should be a sub-directory of the current directory.

And then find if the parameter is a file or directory.

If it is a directory it should just add the extension text at the end of the name of the found directory DDD.

And if it is a file that has the following extension 'mpg or wmv or mov', it should add at the end of the same extension as the file extension at the end of the file name.

Example:

If the file name is: hello.mov, it will be renamed as hello.mov.mov

My Script:

#!/bin/bash
for file in ./$*
do
    find ./$1  -maxdepth 1
    if [ -d $file ]

        ?????????
    fi
    for ext in avi mpg wmv mov; 
    do
        ?????????????????

回答1:

#!/bin/bash
for file in ./* # */ This comment here just to fix broken syntax highlighting
do
    if [[ -d $file ]]
    then
        ext=DDD 
    else
        ext="${file##*.}" # Get extension
    fi
    mv "${file}" "${file}.$ext" # Rename file to $file + . + $ext
done


标签: bash shell