问题创建一个文件夹,然后在同一个脚本中将文件移入其中

时间:2013-08-19 18:35:05

标签: linux shell

我在shell脚本中创建文件后,将文件移动到文件夹中时出现问题。

我的脚本如下:

#!/bin/bash
echo -e "Processing\033[36m" $1 "\033[0mwith the German script";
if [ ! -d ${1%.dat} ]; then
   echo -e "making directory\033[33m" ${1%.dat} "\033[0msince it didn't exist..."; 
   mkdir ${1%.dat};
fi

...处理发生在这里......(与问题无关)

if [ -d ${1%.dat} ]; then
   mv useragents_$1 /${1%.dat}/useragents_$1;
   mv summary_$1 /${1%.dat}/summary_$1;
   more /${1%.dat}/useragents_$1;
else
   echo -e "\033[31mERROR: cannot move files to folder.\033[0m";
fi

如你所见,我创建了文件夹,如果它在顶部不存在,然后如果它存在我将文件移动到底部的文件夹中,问题是它不会创建文件夹是时候移动文件(我假设)所以当它到达较低的代码时,我只得到错误。

我尝试使用,睡眠5,但它只会减慢脚本速度并对ERROR没有影响。

我真的很感激一些建议。

以下错误:

mv: cannot move `useragents_100_stns2_stns6.dat' to `/100_stns2_stns6/useragents_100_stns2_stns6.dat': No such file or directory
mv: cannot move `summary_100_stns2_stns6.dat' to `/100_stns2_stns6/summary_100_stns2_stns6.dat': No such file or directory
/100_stns2_stns6/useragents_100_stns2_stns6.dat: No such file or directory

2 个答案:

答案 0 :(得分:2)

通过1

您的支票:

if [ ! -d ${1%.dat} ]; then

应该是:

if [ -d ${1%.dat} ]; then

您创建了目录;如果是目录,请将内容移入其中。

有问题的错字

通过2

您创建:

mkdir ${1%.dat}

您尝试移动文件:

mv useragents_$1 /${1%.dat}/useragents_$1;

注意移动中的前导斜杠与创建相比。使这些一致。

答案 1 :(得分:2)

你确定这部分吗?它使用根目录。

/${1%.dat}/summary_$1;

你可能想要这样做:

${1%.dat}/summary_$1;

它允许您将文件移动到当前目录中的目录。

相关问题