简体   繁体   English

UNIX shell脚本-在if条件下使用mv命令

[英]unix shell scripting- Using mv command in if condition

I want to move a file to a folder based on its file extension. 我想根据文件扩展名将文件移动到文件夹。 example: if the file is .csv,it should move to COMPLETED folder , if the file has any extension other any .csv then it should move to REGULAR folder. 例如:如果文件是.csv,则应将其移至COMPLETED文件夹,如果文件具有任何扩展名而不是任何.csv,则应将其移至REGULAR文件夹。

Below is my shell script and its not working. 下面是我的shell脚本,它不起作用。 Can you let me know what is the problem with it? 您能告诉我这是什么问题吗?

#!/bin/bash
cd /apps/int/apd/$1/work

if ls /apps/int/apd/$1/work/*.csv &> /dev/null; then
    mv *.csv /apps/int/apd/$1/COMPLETED
else
    /apps/int/apd/$1/Regular
fi

Why do you have to check the existence of *.csv files? 为什么必须检查*.csv文件的存在?

#!/bin/bash
cd /apps/int/apd/$1/work

mv *.csv /apps/int/apd/$1/COMPLETED 2>/dev/null
mv * /apps/int/apd/$1/Regular

Here first .csv files are moved to COMPLETED folder. 在这里,第一个.csv文件被移至COMPLETED文件夹。 Then rest of the files are moved to Regular folder. 然后将其余文件移到“ Regular文件夹中。

I am assuming you have created COMPLETED and Regular folders. 我假设您已经创建了COMPLETEDRegular文件夹。

Change YOUR_PATH with your specific path and your path for /COMPLETED/ and /REGULAR/. 使用您的特定路径以及/ COMPLETED /和/ REGULAR /的路径更改YOUR_PATH。

If I got what you wanted to explain i think your variables look like theese: 如果我得到了您想要解释的内容,我认为您的变量看起来像theese:

/YOUR_PATH/ = /apps/int/apd/$1/work
/COMPLETED/ = /apps/int/apd/$1/COMPLETED
/REGULAR/ = /apps/int/apd/$1/Regular    

You can try this. 你可以试试看 :) :)

#!/bin/bash

for filename in /YOUR_PATH/*;
do
    Path="$filename"
    extension="${filename##*.}"
    if [ "${extension}" = 'csv' ]; then
            mv $Path /COMPLETED/
    else
            mv $Path /REGULAR/
    fi
done

If you need anything pls leave a comment. 如果您需要任何东西,请发表评论。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM