linux - 创建一个文件夹,然后在同一脚本中将文件移动到其中

标签 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

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

我尝试使用 sleep 5,但它只会减慢脚本速度并且对错误没有影响。

我真的很感激一些建议。

错误如下:

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

最佳答案

通过 1

您的支票:

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

应该是:

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

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

有问题的错别字

第 2 轮

您创建:

mkdir ${1%.dat}

您尝试移动文件:

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

注意与创建相比,移动中的前导斜线。使它们保持一致。

关于linux - 创建一个文件夹,然后在同一脚本中将文件移动到其中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18320691/

相关文章:

linux - 在通过 SSH 连接到另一个盒子时获取用户输入

shell - 在 emacs 中使用 ansi-term 时出现换行符问题

ruby - 使用 SSH 克隆存储库时出现 "permission denied"错误?

linux - 允许动态更改背景颜色的终端仿真器

linux - Fortran LAPACK : high CPU %sys usage with DSYEV - no parallelization - normal?

linux - 可以在 zsh 中结合 glob 排序和交替吗?

linux - 需要将 jenkins 日志文件推送到 Amazon S3 存储桶

shell - 防止 POSIX xargs 尝试运行空命令

linux - 在字符串之前提取 IP

linux - chmod : changing permissions of ‘my_script.sh’ : Operation not permitted