0

全新的.sh,我正在尝试从旧 iPhone 备份图像,其中图像被放置到新目录中以供日期。有数百个目录,我需要从每个目录中获取图片并将它们全部转储到一个目录中。

我最好的尝试:

#!/bin/bash
function process () {

a=1
for i in *
do
  cp -r i ${dir}
  let a=a+1
done

}

#Interview
echo "This script will take the contents of each directory in the current directory and move it's contents into a new directory that you will specify"
echo "Name your new directory"
read dir

#Confirm
read -p "Your new directory will be ${dir}. Continue?" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
  then
  process
fi

收到的错误:

massmove.sh: line 1: $'\r': command not found
massmove.sh: line 2: $'\r': command not found
massmove.sh: line 3: $'\r': command not found
massmove.sh: line 4: syntax error near unexpected token `$'{\r''
'assmove.sh: line 4: `function process () {

更新:通过 deefff 的回答改进:

function process () {

for i in */*.*
do
  cp -r $i ${dir}

done

}

echo "This script will take the contents of each directory in the current directory and mv it's contents into a new directory that you will specify"
echo "Name your new directory"
read dir
mkdir ${dir}

echo process

仍然抛出这些错误:

massmove.sh: line 2: syntax error near unexpected token `$'{\r''
'assmove.sh: line 2: `function process () {

这可能是 WSL 错误吗?

我明白那个

 cp */*.* ${dir}

是完成我的任务的一种快速而强大的方法,但我也对导致错误的原因非常感兴趣。

4

3 回答 3

1

*将匹配每个条目,包括目录,因此以当前形式,它将复制整个目录结构,我猜这不是你想要的。此外,您应该在命令i中引用变量。此外,该变量似乎毫无意义。试试这样:$icpa

function process () {

for i in */*.*
do
  cp $i ${dir}
done

}

事实上,我刚刚意识到这也可以解决问题:

cp */*.* ${dir}

于 2016-10-04T12:37:53.157 回答
1

请在下面查看我的脚本。

它基本上有 3 个功能:menu、prnt(用于打印带日期的行)和 process(用于处理文件)。它有一个菜单,可与选项一起使用。希望你喜欢。

干杯!!

高拉夫

#!/bin/bash


curdir=`pwd`            # Take the current directory


# To print out logs on the screen, with date and time !!!
prnt ()
{
        d=`date +"%d-%m-%y "%T`     # Take the date string using bash date function.
        echo "$d|$1"      # Whenever prnt is called, it will echo back, date plus the line passed to it, in nice date|line format.
}

# Menu for the whole operation. This is just the definition. Its being called from the bottom of the script.
menu()
{

        # Specially made for you my friend !!

        echo ; echo ; prnt "    <<!!*** .. params_noob's file copier .. ***!!>>" ; echo ; echo
        echo ; prnt "Currently we are in $curdir" ; echo
        prnt "Enter the directory, where you would like to move files (Please enter full path and system will create it): " ; echo

        read dir  ; mkdir $dir 2>/dev/null # Read directory and make it. I am putting all errors to /dev/null. If directory is there, its there, don't want blabber. If not, then create it.

        echo ;prnt "Entered directory is \"$dir\"" ; echo
        prnt "Type y to go ahead OR n to start over again.." ; echo

        read ans        # I have read the answer here

        # A simple menu using case function of bash.

        case $ans in

        y)
                echo ; prnt "You have entered yes, to move ahead" ; echo
                prnt "processing files now...."
                process $dir     # Here i am calling the process function.
                ;;
        n)
                echo ; prnt "Starting over.. " ; echo
                menu             # Here i am calling the menu itself, again.
                ;;
        0)
                echo ; prnt "Exiting now.. " ; echo
                exit             # Now exiting the script.
                ;;
        *)
                # This one is just to let you know, if you enter anything else than y,n or 0.
                echo ; prnt "Invalid input. Please enter y or n or 0 !!" ; echo
                ;;

        esac    # Menu using case function ends with esac here.

}

# Function to process and move the files to the desired location.
process()
{
        # Took the argument passed to this function into variable "td" [target dirctory].

        td="$1"

        # Using find command here to find all files. You an replace '-type f' with '-name "*.pic"' or '-name "*.jpg"' -- you get the picture - right?

        find $curdir -type f | while read file  # Feeding output of find to a while loop.
        do
                cp $file $td/
        done

        a=`ls -lrt $td/* |wc -l`        # Now taking word count of all the files copied.

        # Some more echo and priting.. just for aesthetics.. :)

        echo ; echo ; prnt "       **** COPIED $a FILES from \[$curdir\] to \[$td\] ****" ; echo ; echo

        echo ; echo ; ls -lrtha $td|head -10 ; echo ; echo

        exit            # Script exits after processing the files. You can replace exit with menu, to go back to menu again.

}
#
##
###
####
#####
#################################################################
        clear ; menu    ### The main menu is being called from this line. I clear the screen first. Looks more professional :)
#################################################################

# FINISH # All done !!
enter code here

这是图片格式的输出

于 2016-10-04T16:11:50.600 回答
0

尝试这样的事情:

function process () {
    for file in *; do
        if [[ ${file} != ${dir} ]]; then
            cp -r ${file} ${dir}
        fi
    done
}

另外,请记住在调用“进程”函数之前创建目录:

echo "Name your new directory: "
read dir
mkdir ${dir}
于 2016-10-04T12:39:34.367 回答